double vs real
Marco Leise
Marco.Leise at gmx.de
Thu May 30 15:00:50 PDT 2013
Am Thu, 30 May 2013 17:47:14 +0530
schrieb Shriramana Sharma <samjnaa at gmail.com>:
> Hello. I like that D exposes to me the real type to maximally utilize
> the machine's numerical precision. Since I am writing a program
> (currently in C++ but I am thinking of moving to D) that has to handle
> lots of fractional numbers (calculating offset curves and such) I am
> wondering whether/when I should real instead of double or even any
> arguments in favour of staying with double (compatibility with
> C/C++?). Any pointers appreciated please? I checked the FAQ but it
> doesn't seem to mention this.
>
> Thanks!
If you have large amounts of values and want to work on them
fast use floats for storage since currently computers have
more processing power than memory bandwidth.
If you return numbers from functions use real. This allows the
compiler to return them as-is from the FPU, whereas double and
float require an additional rounding step. The same may apply
to temporary variables.
Where the amount of numbers is smaller and they fit into CPU
cache, the memory bandwidth doesn't matter. 32-bit CPUs work
the fastest with floats, whereas 64-bit CPUs work efficiently
with doubles.
My tip is to use real everywhere in calculations until it comes
to storing the results for later use where doubles are more
compact.
--
Marco
More information about the Digitalmars-d-learn
mailing list