JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
44.97k stars 5.42k forks source link

Feature request: have a non-lossy parsing of `Float64` literals #37975

Open rfourquet opened 3 years ago

rfourquet commented 3 years ago

Default floating point literals are interpreted as Float64 and is lossy:

julia> 1.0000000000000001 === 1.0
true

I'm not proposing to change the above behavior by default, but to allow macros to have access to the original string, i.e. "1.0000000000000001" here, similar to how the AST stores a string for Int128, UInt128 and BigInt literals. Currently we get

julia> dump(:1.0000000000000001)
Float64 1.0

julia> dump(:0x11111111111111111)
Expr
  head: Symbol macrocall
  args: Array{Any}((3,))
    1: GlobalRef
      mod: Module Core
      name: Symbol @uint128_str
    2: Nothing nothing
    3: String "0x11111111111111111"

This means that a macro has access to the original typed string for Int128, but not for Float64. Changing that would allow macros (like in the SwapLiterals package) to reinterpret a float literal to a more exact type than Float64, be it BigFloat or an exact decimal format.

simeonschaub commented 3 years ago

I think that's a difficult one. I don't think it can be done in a non-breaking way for 1.x, because I am pretty sure a lot of macros and the like assume homoiconicity of floating point literals. Theoretically, it would be possible to either just parse this as a string macro, or as a special expression type that gets transformed to a literal during lowering in 2.0, but it would likely also make any other kind of metaprogramming with floating point literals harder.