D float types operations vs C++ ones

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 17 05:09:44 PST 2015

On Thursday, 17 December 2015 at 11:58:35 UTC, drug wrote:
> On 17.12.2015 14:52, Andrea Fontana wrote:
>> You should publish some code to check...
> Too much code to public - operations are simple, but there are 
> many branches and reducing may take much time . In fact I asked 
> to understand _in general_ if it worth diving into code to find 
> the source of the difference or take it easy and go on...
> If D and C++ use the same things to process float types then it 
> worth to dive, but if there is some issues, then it doesn't.

Yes the float types are the same. floats doubles are identical 
long double == real ( at least for x86)

The only difference is that float are default initialised to NaN 
in D.
The sources of difference are likely to occur from
- const folding (varying by compiler e.g. gcc uses libgmp for 
high precision floating point for constant folding where as other 
compliers may not)
- type promotions and truncations
double a = ... ,b = ... ;
float c = a+b;
float d = cast(float)a + cast(float)b;

c is unlikely to be bit for bit equal to d.

floating point operations are NOT commutative nor are they 
distributive. Order of operations does matter.
i.e. a+(b+c) !=(a+b) +c and a*(b+c) != a*b + a*c (!= means not 
necessarily equal)

you should always compare floats for equality with an epsilon 
(unless 0.0 +-inf )

More information about the Digitalmars-d-learn mailing list