Separate Printing Mantissa and Exponent of a Floating Point
Era Scarecrow via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Aug 13 02:14:48 PDT 2014
On Wednesday, 13 August 2014 at 07:51:30 UTC, Nordlöw wrote:
> Could someone briefly outline the algorithm that converts the
> fraction part of FloatRep into a string in base 10?
Walter Bright broke down the Floating point to explain how it
works and why as i recall..
http://dlang.org/d-floating-point.html
http://en.wikipedia.org/wiki/Floating_point
http://en.wikipedia.org/wiki/Single-precision_floating-point_format
As for the fraction part to floating point... You kinda have to
think in reverse. If we have say a 4 bit floating point. So...
1000 +0.5
0100 +0.25
0010 +0.125
0001 +0.0625
With this in mind you might be better off using hex, but it
depends on the application.
On Wednesday, 13 August 2014 at 07:58:40 UTC, Nordlöw wrote:
> Can somebody shortly explain why it prints
>
> 3623111, 160, false
> 1945142772629504, 1056, false
Because they are raw int/uint types. But i'm sure that's not all
that useful...
> x = 1.23e10;
so 12300000000 or 12,300,000,000 or 2DD231B00
they might make more sense in hex, but with the exponent really
just shifting where the whole/fraction parts are separated.
160-128 = it's shifted to 32/33 bits as it's starting point. and
the 1056-1024 at the 32/33 bit starting point. Same place. The
raw values they hold in hex are: 3748c7 & 0000d8000000
Let's back up then. there is 23 bits of useful data we can use,
and 34 bits present. So let's take our number and convert it to
hex
2DD231B00 and lower that until it fits in 23 bits. Or shift it
about 11 to the right..
And doing this I don't get very good results to feel like I'm
helping much... I guess refer to the wiki on floating point is
probably the best you can do...
Although... I do recall seeing and having a hold of a formula
for manually printing a floating point when I was looking at raw
data before.. That formula was (don't ask where from, I don't
remember.. might be on the wiki. This is from an AHK script):
HexToFloat(d) {
Return (1-2*(d>>31)) * (2**((d>>23 & 255)-127)) * (1+(d &
8388607)/8388608) ; 2**23
}
More information about the Digitalmars-d-learn
mailing list