D float types operations vs C++ ones

drug via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 17 05:30:11 PST 2015


On 17.12.2015 16:09, Nicholas Wilson wrote:
>
> 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
> I.e.
> 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 )
>
Thanks for answer. My C++ version is tracing D version so commutativity 
and distributivity aren't requred because order of operations is the 
same (I guess so at least), so I hoped for bit to bit equality (it 
simplifies comparing serialized data). But I found that error between D 
and C++ versions grows if more data processed so I should investigate it 
anyway.


More information about the Digitalmars-d-learn mailing list