Re:Printing IEEE Floating Point numbers
Posted: Tue Dec 09, 2003 12:21 pm
Hi
I've been thinking of other ways to print floats and I've found a simpler and faster way! ;D
If you use my method please give me Credit!I'll be happy and grateful (Andrew I. Kabakwu is my name
)
Algorithm To Print IEEE FLOATS of any size
Thanks
I've been thinking of other ways to print floats and I've found a simpler and faster way! ;D
If you use my method please give me Credit!I'll be happy and grateful (Andrew I. Kabakwu is my name
![Smile :)](./images/smilies/icon_smile.gif)
Algorithm To Print IEEE FLOATS of any size
Thats IT! It has no shifts or divisions and only 1 multiplication. Comments are welcomed.1. Declare
a string -> number_str
unsigned intergers -> number,count,denominator,exponent
a boolean -> leading_zero
Initialize number_str=" ",all integers to 0 and leading_zero=TRUE;
2. Get the unbiased exponent from IEEE float into exponent eg 0.25 has biased exponent of 125.
So assign exponent = ABSOLUTE(125 - 127) = 2 which is unbiased
3. Make denominator = 2^exponent (2 to the power of exponent).For our example of
0.25,denominator=2^2=4
4. Get mantissa from IEEE format and add Implied 1. assign this to number i.e
number = mantissa OR IMPILED_ONE (set the 24th bit to 1. IMPLIED_ONE = 0x0080 0000 in single precision)
5. Check number > denominator.
a. if false and leading_zero equal TRUE
append '0' and '.' to number_str
set leading_zero = FALSE
b. multiply number by 10 i.e. number = number * 10
ELSE
c. if true
if leading_zero=TRUE
set leading_zero = FALSE
i. subtract denominator from number
ii. add 1 to count
repeat i. and ii. until number < denominator
d. convert count to a string and append to number_str
e. set count = 0
6. if number > 0 go to step 5
7. print number_str ! ;D
Thanks