-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Mixing 0x
number prefix with float suffix results in unexpected behavior
#12662
Comments
Ah it's effectively parsing it as 0xF32 crystal/src/compiler/crystal/syntax/lexer.cr Line 1533 in c60bcca
I guess this is a little ambiguous since the |
0x
number prefix with floats results in unexpected behavior0x
number prefix with float suffix results in unexpected behavior
I'm curious: Why are trying to use hexadecimal notation for floating point numbers? |
I have no real-world use for this. I just stumbled across it while writing a test for something else in the compiler |
I don't think we can do anything about that. The fn main() {
println!("{}", 0x00_f32); // => 3890
} Hexadecimal notation is very unusual for floating point numbers. That really doesn't make much sense because hex notation (as well as octal and binary) in integers is used to express the byte representation. But that's completely different for floats. When floats are written in hex, that's usually also for the internal representation. But then you can't just use ordinary literals for that. I checked with Rust and it does not accept octal and binary notation for floating point numbers. Crystal does, though. I think we should remove support for that in Crystal as well. It's not even documented: https://crystal-lang.org/reference/1.6/syntax_and_semantics/literals/floats.html |
Hexfloat literals exist, can be produced by using "%a" % 125.0 # => 0x1.f4p+6
0x1.f4p+6 # equivalent to (0x1).f4p().+(6) To my knowledge, of the mainstream languages only C(++) and Julia support them in plain code. |
@HertzDevil Yeah but this is a specific format for floating point numbers based on their internal representation. That's different from what's actually an integer literal with a |
Given that Crystal doesn't require a unique character to delineate type suffixes (i.e. |
I'd expect this to simply result in 0, as intuitively this number is "0x00 represented as a 32-bit float".
This seems to only act incorrectly with the
0x
prefix and both float sizes. Binary and octal prefixes work as expected, as do integer numbersThe text was updated successfully, but these errors were encountered: