Real Close to the Machine: Floating Point in D

Fawzi Mohamed fmohamed at mac.com
Sat May 9 15:57:12 PDT 2009


On 2009-05-09 01:14:56 +0200, Walter Bright <newshound1 at digitalmars.com> said:

> Don Clugston writes about floating point programming in D:
> 
> http://www.digitalmars.com/d/2.0/d-floating-point.html
> 
> http://www.reddit.com/r/programming/comments/8j10v/real_close_to_the_machine_floating_point_in_d/

Nice 
> 
article Don!

on comparing floating points if you have complex operation in between 
(like diagonalizations,... or even a simple dot product) I often use

int feqrel2(T)(T x,T y){
    static if(isComplexType!(T)){
        return min(feqrel2(x.re,y.re),feqrel2(x.im,y.im));
    } else {
        const T shift=ctfe_powI(0.5,T.mant_dig/4);
        if (x<0){
            return feqrel(x-shift,y-shift);
        } else {
            return feqrel(x+shift,y+shift);
        }
    }
}

because if you sum (as you say later) the absolute error is added, and 
the relative one can become very large.
If the result is close to 0 (where the density of floats is very high), 
or even 0 by chance, you cannot expect to keep the relative error to be 
small, so I cap the relative error close to 0 with an absolute one...

Fawzi




More information about the Digitalmars-d mailing list