[Issue 23521] New: Bad conversion in double.to!long for values around long.max

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Nov 30 13:21:07 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=23521

          Issue ID: 23521
           Summary: Bad conversion in double.to!long for values around
                    long.max
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: minor
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: hagai at weka.io

trying to convert some values around long.max from double to long yields
negative values instead of something sensible (an overflow exception, or a
large value in the order of magnitude of long.max).

import std;
void main()
{
    writeln(9223372036854775296.0.to!long); // output -9223372036854775808 -
bad
        writeln(9223372036854776832.0.to!long); // output -9223372036854775808
- bad

    writeln(9223372036854775295.0.to!long); // output 9223372036854774784 - ok
    try {
        writeln(9223372036854776833.0.to!long);
    } catch (ConvOverflowException) {
                writeln("exception"); // ok
    }
}

--


More information about the Digitalmars-d-bugs mailing list