Parsing Float32 literals

This caught me by surprise:

julia> parse(Float32, "1.0f0")
ERROR: ArgumentError: cannot parse "1.0f0" as Float32

Float32 seems special

julia> x = UInt8(1)
0x01

julia> parse(typeof(x), repr(x))
0x01

julia> x = UInt16(1)
0x0001

julia> parse(typeof(x), repr(x))
0x0001

ulia> x = Float64(1)
1.0

julia> parse(typeof(x), repr(x))
1.0

julia> x = Float32(1)
1.0f0

julia> parse(typeof(x), repr(x))
ERROR: ArgumentError: cannot parse "1.0f0" as Float32

Float16 also fails, but that doesn’t have a proper literal representation:

julia> x = Float16(1)
Float16(1.0)

julia> parse(typeof(x), repr(x))
ERROR: ArgumentError: cannot parse "Float16(1.0)" as Float16

Is this intentional?

3 Likes

https://github.com/JuliaLang/julia/issues/5690

4 Likes

Oh, and oldie. But, if I understand Jeff’s comment, it seems like parse ought to work?

It does:

julia> Meta.parse("1f0")
1.0f0
2 Likes