Hi guys,
Spent some time yesterday evening on some 'proof of concept' for
floats. The official float standards is based on an binary exponent,
but this makes representation of the numer in decimal quite complex.
So i decided to use a 3-byte mantissa which holds a number normalized
between +/- 100_000 and 999_999 (or zero). This means I does (worst
case) only uses 16.6 of the 23 bits for accuracy. The advantage is
conversion to decimal is a lot easier. Because unlike my first
assumtion, this seems to be the major issue (not the normalization
itself). The remaining 8 bits are used as a decimal exponent. It all
fits in one dword.
Currently I have:
var dword Float1
AssignFloat(Float1, 1, 5)
PrintFloat(Float1)
The first line assigns 1 * 10 ^ 5 to float1 and the second one prints
the number. I intend to spent some time on calculation functions and I
expect this will be pretty straight-forward.
The most obvious candidates:
FloatAdd
FloatSubtract
FloatMultiply
FloatDivide
FloatToDword (with an 10^x offset as parameter)
And I guess we need a FloatCompare function, to see if two numbers are
significant close, say within 10^4 or so.
What does need substantial work is the printing (conversion) function.
It now prints numers as decimals and switches to power of 1000^x when
it does not fit otherwise. Examples:
Mantissa: 4294867296, Exponent: 0, Float: -1.0000
Mantissa: 100000, Exponent: 0, Float: 1.00000
Mantissa: 4294867296, Exponent: 3, Float: -1000.00
Mantissa: 100000, Exponent: 3, Float: 1000.00
Mantissa: 4294867296, Exponent: 4, Float: -10000.0
Mantissa: 100000, Exponent: 4, Float: 10000.0
Mantissa: 4294867296, Exponent: 5, Float: -100000
Mantissa: 100000, Exponent: 5, Float: 100000
Mantissa: 4294867296, Exponent: 6, Float: -1.00000
Mantissa: 100000, Exponent: 6, Float: 1.00000E6
Mantissa: 4294867296, Exponent: 7, Float: -10.0000
Mantissa: 100000, Exponent: 7, Float: 10.0000E6
Mantissa: 4294867296, Exponent: 8, Float: -100.000
Mantissa: 100000, Exponent: 8, Float: 100.000E6
Mantissa: 4294867296, Exponent: 9, Float: -1.00000
Mantissa: 100000, Exponent: 9, Float: 1.00000E9
(the number behind 'Float:' is the real output, rest is debug)
More control on the output is needed (space limitation, nr of digits,
decimal point offset etc) to make it practical usefull. Let me know if
anybody wants to step in on this ;)
It seems possible to make this work. It is not very efficient, since
it eats quite a lot of cpu cycles and 2 to 6 bits of accuracy are
sacrificed by the decimal-exponent. And the code is pretty large:
about 3k to assign & print one numbers (inlcuding serial stuff,
printing stuff etc).
But it will provide a means to automagicly handle the point position,
with simple functions and uses standard dwords to store the values.
Regards,
Joep
--
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en.