Why shouldn't the parse function accept all representation of numeric literals in Julia? #46414
Replies: 7 comments 1 reply
-
Even Float32s are printed with julia> print(1f10)
1.0e10 The julia> Meta.parse("1f0")
1.0f0 |
Beta Was this translation helpful? Give feedback.
-
I am not worried or talking about the function print(). What I'm more concerned about is: All other cases are supported by Talking about Julia specific, I don't think you're utterly right. Other languages like Java and many others uses the So, yes it might not be supported, but Why should a language reject its own internal representation of numeric literals? |
Beta Was this translation helpful? Give feedback.
-
Another reason why this of vital importance to either spell out in the docs (with a good reason), or allowed to run without an error is because developers using Julia tends to use more of julia> convert(Float64, 0.1E-2) # figures this works out
0.001
julia> parse(Float64, "0.1E-2") # figures this works out as well
0.001
julia> convert(Float64, 1.2f-2) # then he tries this and sees same behaviour
0.012000000104308128
#= hopefully he writes code with this 👇 expecting same behaviour but an error
is raised and he can't figure out where the subtle bug is, because he doesn't expect
this to throw an error and when he finally figures out the subtle bug, he can't figure
out why that should throw an error from the docs or anywhere. =#
julia> parse(Float64, "1.2f-2")
ERROR: ArgumentError: cannot parse "1.2f-2" as Float64
[...] |
Beta Was this translation helpful? Give feedback.
-
How do you end up with wanting to parse a float that is written with the
|
Beta Was this translation helpful? Give feedback.
-
@KristofferC you keep emphasising on the print functionality which is not what my issue is. parse says this help?> parse
search: parse tryparse partialsortperm partialsortperm! pairs skipchars
parse(type, str; base)
Parse a string as a number. For Integer types, a base can be specified
(the default is 10). For floating-point types, the string is parsed as
a decimal floating-point number. Complex types are parsed from decimal
strings of the form "R±Iim" as a Complex(R,I) of the requested type;
"i" or "j" can also be used instead of "im", and "R" or "Iim" are also
permitted. If the string does not contain a valid number, an error is
raised.
So is this string a valid numeral literal in Julia? julia> "1f-2"
"1f-2"
julia> string(1f-2)
"0.01"
julia> parse(Float64, "1f-2")
RROR: ArgumentError: cannot parse "1f-2" as Float64
[...] If You keep emphasising on the print, which in my view can be |
Beta Was this translation helpful? Give feedback.
-
This is another example where surprisingly I fell into today, just a day after the julia> 0x1 + 0x2
0x03
julia> 1 + 12f0im
1.0f0 + 12.0f0im
julia> 1 + 0x2p4im
1.0 + 32.0im
julia> 1 + 1.2e-2im
1.0 + 0.012im
julia> 1 + 0x23im
ERROR: syntax: invalid numeric constant "0x23i"
[...] Just as it makes no sense on why julia> parse(Float64, "1f-2")
ERROR: ArgumentError: cannot parse "1f-2" as Float64
[...] So it makes no sense here for this not to juxtapose as a valid complex number: julia> 1 + 0x23im
ERROR: syntax: invalid numeric constant "0x23i"
[...] But this then works: julia> 1 + UInt8(0x23)im
1 + 35im
julia> UInt8(0x23)
0x23 On different occasions, I just find myself getting confused on why Julia refused to accepts its own numeric literal representation |
Beta Was this translation helpful? Give feedback.
-
I agree with some shortcomings in
That could be separated into 2 issues and PRs, IMO. @udohjeremiah if I missed cases you found, please add them. |
Beta Was this translation helpful? Give feedback.
-
I was doing a test on the
parse
function, and I came across something I don't know if I should call a bug yet, or if it was the intended design by the core developers of Julia.The Julia doc says this:
So it makes intuitive sense for this 👇 to work (and it works):
Again, the doc says this:
So it makes intuitive sense for this 👇 to work as well (and it works):
As the doc continues, it goes further to say this:
So, again it makes intuitive sense for this 👇 to work too (and it works):
THIS IS NOW MY ARGUMENT
Finally, the doc says this:
So, it makes intuitive sense for this 👇 to work too (BUT IT THROWS AN ERROR):
My question is:
Beta Was this translation helpful? Give feedback.
All reactions