V: Imprecise double arithmetics?

Created on 8 Apr 2019  路  16Comments  路  Source: vlang/v

fn main() {
    t := 0.1 + 0.2
    println(t == 0.30000000000000004)
}

Output: false
Expected: true

Most helpful comment

In Go: 0.1 + 0.2 == 0.3 and it's true. Btw it's wrong! In all mature languages which follow IEEE 754 standard this should be false!

  1. Go true
  2. Rust false
  3. C false

But in your case even 0.1 + 0.2 == 0.3 is false:

fn main() {
    t := 0.1 + 0.2
    println(t == 0.3)
}

Output: false

How you explain this?

All 16 comments

In Go: 0.1 + 0.2 == 0.3 and it's true. Btw it's wrong! In all mature languages which follow IEEE 754 standard this should be false!

  1. Go true
  2. Rust false
  3. C false

But in your case even 0.1 + 0.2 == 0.3 is false:

fn main() {
    t := 0.1 + 0.2
    println(t == 0.3)
}

Output: false

How you explain this?

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++:
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/

Was this page helpful?
0 / 5 - 0 ratings