Printing IEEE Floating Point numbers

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Slasher

Re:Printing IEEE Floating Point numbers

Post by Slasher »

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
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
Thats IT! It has no shifts or divisions and only 1 multiplication. Comments are welcomed.
Thanks
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Printing IEEE Floating Point numbers

Post by Candy »

Can't concentrate on the algorithm right now, but the numero uno thing I'm thinking about now is infinities, positive and negative zeroes, negative numbers, denormal numbers and NaN's. Did you do anything about them?
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Printing IEEE Floating Point numbers

Post by Solar »

Code: Select all

#include "math.h"
Gives you macros for isinf(), isnan(), isnormal(). You need math.h sooner or later anyway, so implement it now and use it for your float printing.

<shameless plug>

Better yet, help working on http://pdclib.sourceforge.net and we get implement once, use multiple. ;-)

</shameless plug>
Every good solution is obvious once you've found it.
Slasher

Re:Printing IEEE Floating Point numbers

Post by Slasher »

Candy,
the algorithm is to call after you have done the necessary processing to detect infinities, positive and negative zeroes, negative numbers, denormal numbers and NaN's.

To handle infinities, positive and negative zeroes, negative numbers, denormal numbers and NaN's the best is to perform the TESTS adviced by the IEEE format spec.

I'm working on them ;)
IMPORTANT INFO
there are VERY SERIOUS FLAWS in my initial algorithm post :-[. I coded it and discovered that I was making VERY BAD assumptions which I can only explain with an example.
for example, I was thinking that the float 70.457 looks like
1000110.01110100111111100 (70.457) in memory which is not 100% true because the CPU has no notion of a decimal or binary point so 70.457 is 0x8CE9FC.If you use my first algorithm it will print 9234940 and not 70.457 :-[
I've fixed the problems and will post a new algorithm below so that they can be compared.I am still going to perform further debugging tests. :)
Post Reply