Why use float and double instead of real?

Witold Baryluk baryluk at smp.if.uj.edu.pl
Tue Jun 23 06:27:13 PDT 2009


Dnia 2009-06-23, wto o godzinie 14:44 +0200, Lars T. Kyllingstad pisze:
> Is there ever any reason to use float or double in calculations? I mean, 
> when does one *not* want maximum precision? Will code using float or 
> double run faster than code using real?

yes they are faster and are smaller, and accurate enaugh.

they also can be used in SSE.

reals can be very undeterministic. like

if (f(x) != f(y)) { assert(x != y, "Boom!"); } // it will explode

> I understand they are needed for I/O and compatibility purposes, so I am 
> by no means suggesting they be removed from the language. I am merely 
> asking out of curiosity.

float and double types conforms to IEEE 754 standard. real type not.
and many application (scientific computations, simultions, interval
arithmetic) absolutly needs IEEE 754 semantic (correct rounding, known
error behaviour, and so on). additionally
real have varying precission on multiple platforms, and varing size,
or are just not supported.

if you need very high precision (and still have some knowledge about
what is maximal error), you can use double-double, or quad-double
(structure of 2 or 4 doubles).

I have implemented them in D, but are quite slow.

> -Lars




More information about the Digitalmars-d-learn mailing list