-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hh/general numbers #52
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still changes that need to be addressed, esp. with the _pfmt* code.
src/fmt.jl
Outdated
@@ -79,6 +79,9 @@ default_spec(::Type{<:AbstractString}) = DEFAULT_FORMATTERS[AbstractString] | |||
default_spec(::Type{<:AbstractChar}) = DEFAULT_FORMATTERS[AbstractChar] | |||
default_spec(::Type{<:AbstractIrrational}) = DEFAULT_FORMATTERS[AbstractIrrational] | |||
default_spec(::Type{<:Number}) = DEFAULT_FORMATTERS[Number] | |||
default_spec(::Complex{T} where T<:Integer) = DEFAULT_FORMATTERS[Complex{T} where T<:Integer] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have Type{<:Complex}
to handle complex numbers that use other numeric types as a fallback.
I still don't think you should have specific defaults for more specific types, but if so, they should be written as following:
These should be Type{Complex{<:Integer}}
, Type{Complex{<:AbstractFloat}}
, Type{Complex{<:Rational}}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I had tried exactly that before, but I received
julia> hh(x::Type{Complex{<:Integer}}) = show(typeof(x))
hh (generic function with 1 method)
julia> hh(2 + 3im)
ERROR: MethodError: no method matching hh(::Complex{Int64})
Closest candidates are:
hh(::Type{Complex{#s12} where #s12<:Integer}) at REPL[114]:1
Stacktrace:
[1] top-level scope at REPL[115]:1
julia> gg(x::Complex{<:Integer}) = show(typeof(x))
gg (generic function with 1 method)
julia> gg(2 + 3im)
Complex{Int64}
So I accepted that Type{Complex{<:Integer}}
is a type of a type, which is what we don't want ...
I am happy to accept any prettier solution.
- In much the same way, I think, the fallback should be
<:Complex
instead ofType{<:Complex}
julia> typeof(2 + im) <: Type{<:Complex}
false
julia> typeof(2 + im) <: Complex
true
But I consider myself still a beginner in type programming, so please fell free to jump in, if you have a better way of putting it.
- I wrote
Complex{T} where T<:Integer
because, strangely theDict
does not test on identity before adding a new type:
julia> d = Dict{Type{T} where T, String}()
Dict{Type,String} with 0 entries
julia> d[Complex{T} where T <: Integer] = "test"
"test"
julia> d[Complex{S} where S <: Integer] = "test"
"test"
julia> d[Complex{<:Integer}] = "test"
"test"
julia> d
Dict{Type,String} with 3 entries:
Complex{#s15} where #s15<:Integer => "test"
Complex{T} where T<:Integer => "test"
Complex{S} where S<:Integer => "test"
julia> (Complex{T} where T<:Integer) == (Complex{S} where S<:Integer)
true
So default_fmt!(Complex{<:Integer}, 's')
will just add another entry to the dict instead of overwriting. That is also the reason, why - in the first run - I chose to define the type ComplexInteger = Complex{T} where T<:Integer
.
Codecov Report
@@ Coverage Diff @@
## master #52 +/- ##
==========================================
- Coverage 95.79% 95.73% -0.06%
==========================================
Files 6 6
Lines 499 539 +40
==========================================
+ Hits 478 516 +38
- Misses 21 23 +2
Continue to review full report at Codecov.
|
oops, one commit was not sent. Coming shortly ... |
Project.toml
Outdated
@@ -23,7 +23,7 @@ keywords = ["Strings", "Formatting"] | |||
license = "MIT" | |||
name = "Format" | |||
uuid = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8" | |||
version = "1.0.1" | |||
version = "1.1.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to update the version as well, to "1.2.0"
src/fmt.jl
Outdated
default_spec(::Type{<:AbstractIrrational}) = DEFAULT_FORMATTERS[AbstractIrrational] | ||
default_spec(::Type{<:Rational}) = DEFAULT_FORMATTERS[Rational] | ||
default_spec(::Type{<:Number}) = DEFAULT_FORMATTERS[Number] | ||
default_spec(::ComplexInteger) = DEFAULT_FORMATTERS[ComplexInteger] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, you are confusing passing an instance of a particular type, with passing the type itself.
You need to have ::Type{<:Complex}
.
I think it's best to work on making any complex type work at first, before trying to add the capability of having separate defaults for different parameterized Complex types.
(AbstractFloat,'f'), | ||
(AbstractChar,'c'), | ||
(AbstractString,'s'), | ||
(ComplexInteger,'d'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, please remove the separate ComplexInteger
, ComplexFloat
, ComplexRational
default specs.
src/fmtspec.jl
Outdated
_pfmt_i(io, fs, ix, _Bin()) | ||
try | ||
ix = Integer(x) | ||
ty == 'd' || ty == 'n' ? _pfmt_i(io, fs, ix, _Dec()) : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code smell here - shouldn't have a bunch of copy-paste.
The try
should only be around the Integer
conversion also.
Also, if the value cannot be converted to some Integer type, it should probably go directly to _pfmt_s
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am afraid that I didn't completely get what you have in mind. We can't go directly to _pfmt_s
, because we still need the formatting of the number part of the Number
.
If we have a composed type like Complex{Int64}
and we want to format it with 'd'
, this information needs to get passed on to the user-defined fmt_Number()
.
So my idea was:
- we have
_pfmt_i(out::IO, fs::FormatSpec, x::Integer, op::Op) where {Op}
which does the normal formatting. - we add
_pfmt_i(out::IO, fs::FormatSpec, x::Number, op::Op) where {Op}
as a fallback which finally callsfmt_Number(x, f)
where f contains_pfmt_i()
with all the necessary parameters, so that the user simply needs to applyf()
to the number part, e.g.f(real(x))
.
Due to this fallback, I think, it is ok to accept ix
in printfmt()
to be either Integer or a number type with Integer number parts. Then we would get rid of the "code smell".
Example:
Integer(2.0)
is 2.
Integer(2.0 + 3.0im)
fails, so pyfmt("d", 2.0 + 3.0im)
would fail. Therefore, fmt_Number()
calls _pfmt_i(out, fs, x::Integer, op)
on the real and imaginary part separately. So pyfmt("d", 2.0 + 3.0im)
will finally result in 2 + 3im
. If I would go directly to _pfmt_s
, I would receive 2.0 + 3.0im
.
Same story with _pfmt_f
, but even more important, as e.g. float(2 - im)
already acts on the number parts individually and results in 2.0 - 1.0im
. So the type of fx
in that case is always not predictable.
EDIT:
One comment on passing _pfmt_i
in _pfmt_Number_i
:
This would not be necessary, as the subtypes are covered by the op
argument. whereas in the float case, where we have _pfmt_f
, _pfmt_e
and possibly later _pfmt_g
. The subtype. Maybe, it's better to remove that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For complex numbers, you could use the defaults for "d" based on the type T
in Complex{T}
,
no need to have to define a potentially unlimited number of "ComplexXXXX" types, just for setting defaults.
Because I added the capability of storing keyword information along with the formats, that also could be used to set how particular types of complex numbers are printed.
For example: in the default dictionary, have simply an entry Complex
,
and add support for a keyword that control whether integers should be printed as integers.
I don't think anything more would be necessary.
I'm looking for methods that don't just simply solve your problem for printing complex numbers, but also handle it in a more generic, extensible fashion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing you may not have noticed, the C-style formatted strings (i.e. f"\%f(var)"
don't use the pfmt*
functions, they use cfmt
, which generates a formatter using @printf
, so it won't use the pfmt
functions you've been modifying.
if isfinite(fx) | ||
ty == 'f' || ty == 'F' ? _pfmt_f(io, fs, fx) : | ||
ty == 'e' || ty == 'E' ? _pfmt_e(io, fs, fx) : | ||
try |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above - if it can't get converted to something that is AbstractFloat
, then it should output as string, or give an error.
@@ -262,3 +263,42 @@ function _pfmt_specialf(out::IO, fs::FormatSpec, x::AbstractFloat) | |||
end | |||
end | |||
|
|||
function _pfmt_Number_f(out::IO, fs::FormatSpec, x::Number, _pf::Function) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These still have the issues that I mentioned before.
I must admit, that I am a bit lost, how you would like to have the code modified. I commented the parts where I have the most difficulties. Also, I don't know how to modify the type part where you said I am confusing passing an instance of a particular type, with passing the type itself. For private reasons, I don't have the time to dig deeper. Would you mind, either accepting the PR and submitting your own corrections, or to submit a PR to my branch? I think that way will be faster and I will learn for my next PR ;-) EDIT: |
93b960d
to
d07f241
Compare
I am very interested in getting this cleaned up - right now, there are about 4 separate code paths for formatting (all of which I inherited), which are rather inconsistent, and I dislike having the code depend on the code in First off, before jumping into code changes, it might be good to write down a set of goals, for capabilities that you feel should be handled by an improved formatter, remembering that Thanks for your work and interest in improving formatting! |
Sounds like a good idea. When I have some inspiration, I will let you know here ;-) |
I just checked with Python, and it's actually a bug that the Formatting (and my Format) packages don't truncate strings if a precision is specified, something you don't want to do for numeric formats (numeric formats should only round off based on prec, not chop off the end totally, that's also important for the Unitful types. |
665bf32
to
c9bb7b8
Compare
c9bb7b8
to
ab0bdac
Compare
To bring this PR to a reasonable level, I have at least updated all formatting routines to use the same handling for general number types, i.e. calling |
Sounds good.
That is as it should be.
I don't think that they even should use those sorts of formats options for composed numeric types. |
I'll try to take a look at this further tomorrow night (I'm getting ready to drive to Florida for Xmas vacation right now!) |
Now the tests are back. There is still a problem with setting and resetting [format defaults]. This is probably due to my misimplementation of the types ... |
Well, I think I understand types a bit better now ... |
Allow for Complex, Rational and user-defined
Number
s, e.g. unitful quantities.Superseding #50 on top of #51