Type Conversion - IEEE Float to Scaled Decimal

Imported from previous forum

I noticed the Fast 1.1 specification does not have any rules for converting from either a 4 or 8 byte IEEE floating point representation to a scaled decimal. Is there a best practice for this ? The conversion rules from a string to a decimal do not explicitly define how the exponent should be determined. In practice, I assume when converting a string to a decimal, one would count the number of digits to the right the decimal point and use that as the exponent. You would then create the mantissa by multiplying the original ASCII number by 10^exponent. You could reduce the scaled number either by removing non significant trailing zeros from the string before the conversion, or using the mod operator on the scaled decimal after the conversion to discover the optimal exponent.

What is the most efficient way to determine the optimal scaled decimal exponent from a IEEE float ?

Daniel May

[ original email was from Glenn McClements - glenn@wombatfs.com ]
Hi Daniel,
What you have described is a way to normalize decimals to have the smallest mantissa. You’re right in that FAST doesn’t specify that you should do this, as it leaves the implementation down to you. In some environments you may know that you are always dealing with a fixed number decimal places, so the exponent could be a constant field. (Maybe one of the FIX over FAST docs would be a better place to specify how to normalize floats, specifically for FIX?)

The normalization algorithm you describe is essentially one which I have used, though in converting straight from doubles to decimals without using strings.

One thing to be careful of is the inherent inaccuracies of floating points when dealing with large numbers or lots of decimal places. If you are converting from double → string → decimal then be aware that not all the decimal places may be significant. FIX has a 15 significant digit rule because the first 15 digits of a double are guaranteed to be accurate.

In practice, if the precision of a particular system is known before hand then you can use this a hint to optimize your conversion algorithm, or have a constant or default exponent.

Glenn

I noticed the Fast 1.1 specification does not have any rules for
converting from either a 4 or 8 byte IEEE floating point representation
to a scaled decimal. Is there a best practice for this ? The conversion
rules from a string to a decimal do not explicitly define how the
exponent should be determined. In practice, I assume when converting a
string to a decimal, one would count the number of digits to the right
the decimal point and use that as the exponent. You would then create
the mantissa by multiplying the original ASCII number by 10^exponent.
You could reduce the scaled number either by removing non significant
trailing zeros from the string before the conversion, or using the mod
operator on the scaled decimal after the conversion to discover the
optimal exponent.

What is the most efficient way to determine the optimal scaled decimal
exponent from a IEEE float ?

Daniel May