about float & double

Ali Çehreli acehreli at yahoo.com
Tue Jan 18 15:00:02 PST 2011


spir wrote:

 > Is there somewhere a (clear) doc about float/double internals?

A very good read is:

   http://digitalmars.com/d/2.0/d-floating-point.html

 > Some more particuliar questions:
 >
 > What is the internal bit layout? (mantissa, sign, exponent)

IEEE floating point format. This page has links to different 
representations:

   http://en.wikipedia.org/wiki/Ieee_floating_point

Specifically:

   http://en.wikipedia.org/wiki/Single_precision_floating-point_format
   http://en.wikipedia.org/wiki/Double_precision_floating-point_format

 > What are the values used to represent thingies like NaNs, inf, error?
 > (Or are there not represented as values?)

They are available on the documents above.

 > How would you get a float's integral and fractional parts without
 > performing arithmetic? (I think at bit ops, indeed)

Here is a function with endianness "issues" that I had used with 
different types:

import std.stdio;

void display_bytes(T)(ref T variable)
{
     const ubyte * begin = cast(ubyte*)&variable;

     writefln("type          : %s", T.stringof);
     writefln("value         : %s", variable);
     writefln("address       : %s", begin);
     writef(  "representation: ");

     foreach (p; begin .. begin + T.sizeof) {
         writef("%02x ", *p);
     }

     writeln();
     writeln();
}

void main()
{
     auto d_nan = double.nan;
     auto d_inf = double.infinity;
     display_bytes(d_nan);
     display_bytes(d_inf);
}

Ali


More information about the Digitalmars-d-learn mailing list