www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23521] New: Bad conversion in double.to!long for values

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 puremagic.com
          Reporter: hagai 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
    }
}

--
Nov 30 2022