weird behavior for machine epsilon in float, double and real

BCS none at anon.com
Sat Apr 11 09:23:12 PDT 2009


Hello Andrew,

> So, for the following code, I get something I think is a little off.
> I get the same value for all three variable types.  I'm kind of new to
> this, but I would think that a 32 bit would give me a different
> "smallest value" than a 64 bit or 80 bit (if real even evaluates to 80
> bit on my machine).
> 
> What am I doing wrong, or is this a bug?
> 

This is a classic case of the optimizer getting in your way by doing all 
the math in the FPU (the other classic cases is it noting that as long as 
ep!=0, ep+n "can't" equal n). If you are on x86 than the only type the FPU 
uses inside is 80bit-real.


> import std.stdio;
> 
> void main() {
> 
> //Find Machine Epsilon:
> 
> double ep = 1.0;
> double n = 1.0;
> while (ep + n != n) {
> ep = ep / 2;
> }
> writefln(ep);
> 
> real epr = 1.0;
> real nr = 1.0;
> while (epr + nr != nr)
> epr = epr / 2;
> writefln(epr);
> 
> float epf = 1.0;
> float nf = 1.0;
> while (epf + nf != nf)
> epf = epf / 2;
> writefln(epr);
> }





More information about the Digitalmars-d mailing list