fn main() {
t := 0.1 + 0.2
println(t == 0.30000000000000004)
}
Output: false
Expected: true
V running in the playground compiles to C, that's how it works there.
@medvednikov
Same result as in Rust:
https://onlinegdb.com/BkdNxHYtV
Right now floating-point constants default to f32, instead of f64. Will be fixed today.
I changed it to f64, the comparison still doesn't work because it doesn't work in C (and in C++).
Comparing floats like this is a bad idea:
https://www.cs.technion.ac.il/users/yechiel/c++-faq/floating-point-arith.html
https://www.boost.org/doc/libs/1_61_0/libs/math/doc/html/math_toolkit/float_comparison.html
Actually 0.1 + 0.2 == 0.30000000000000004 works now. 0.1 + 0.2 == 0.3 doesn't.
the comparison still doesn't work because it doesn't work in C (and in C++).
comparison 0.1 + 0.2 == 0.30000000000000004 should works exactly in C/C++:

Current in V:
fn main() {
t := 0.1 + 0.2
println(t == 0.30000000000000004)
}
Output:
It still not fixed!
Forgot to deploy the playground after the fix:
t := 0.1 + 0.2
println(t == 0.30000000000000004)
// true
Now it works like C/C++.
I tried to run on playground today, output as false
t := 0.1 + 0.2
println(t == 0.30000000000000004)
// t is 0.300000
Can confirm. This issue still not fixed!
it should be open, because this is not fixed
@whoizit fixed again.
t == 0.30000000000000004
Just for completion, https://0.30000000000000004.com/
Most helpful comment
In Go:
0.1 + 0.2 == 0.3and it's true. Btw it's wrong! In all mature languages which follow IEEE 754 standard this should befalse!truefalsefalseBut in your case even
0.1 + 0.2 == 0.3is false:Output:
falseHow you explain this?