wrong rounding

Oleg B via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 1 14:29:39 PDT 2015


Hello. I found unexpected (for me) behavior of rounding double 
values at casting to ulong:

$ cat roundtest.d
import std.stdio;

void main()
{
     double a = 10, b = 0.01;

     writeln( "int:   ", cast(int)(a/b) );
     writeln( "uint:  ", cast(uint)(a/b) );
     writeln( "long:  ", cast(long)(a/b) );
     writeln( "ulong: ", cast(ulong)(a/b) );
}

$ rdmd roundtest.d
int:   1000
uint:  1000
long:  1000
ulong: 999   <----- WTF?? -------

$ dmd --version
DMD64 D Compiler v2.067.1
Copyright (c) 1999-2014 by Digital Mars written by Walter Bright

$ ldc2 roundtest.d && ./roundtest
int:   1000
uint:  1000
long:  1000
ulong: 1000  <------- Ok --------

$ ldc2 --version
LDC - the LLVM D compiler (0.15.2-beta1):
   based on DMD v2.066.1 and LLVM 3.6.0
   Default target: x86_64-unknown-linux-gnu
   Host CPU: core-avx-i
   http://dlang.org - http://wiki.dlang.org/LDC

   Registered Targets:
     x86    - 32-bit X86: Pentium-Pro and above
     x86-64 - 64-bit X86: EM64T and AMD64

$ uname -a
Linux lenovo 4.0.4-201.fc21.x86_64 #1 SMP Thu May 21 15:58:47 UTC 
2015 x86_64 x86_64 x86_64 GNU/Linux

have it behavior explain? can I use casting in dmd for rounding 
values or it not safe?


More information about the Digitalmars-d mailing list