Search code examples
dfloating-accuracyfloating-point-precision

Why isn't to!int() working properly?


Why does this assertion fail?

import std.conv;

void main()
{
    auto y = 0.6, delta=0.1;
    auto r = to!int(y/delta);
    assert(r == 6);
}

r's value should be 6 and yet it's 5, Why?


Solution

  • This is probably because 0.6 can't be represented purely in a floating point number. You write 0.6, but that's not exactly what you get - you get something like 0.599999999. When you divide that by 0.1, you get something like 5.99999999, which converts to an integer of 5 (by rounding down).

    Examples in other languages:

    C#: Why is (double)0.6f > (double)(6/10f)?

    Java: Can someone please explain me that in java why 0.6 is <0.6f but 0.7is >=0.7f