Getting number of significant digits in float

Started by
5 comments, last by Ectara 11 years, 11 months ago
Is it possible to predict or count the number of significant decimal digits in a floating point number? For instance, if I were to output as a simple decimal number string, how would I count the number of digits before and after the radix point without actually counting and printing them? I'd like to count the number of decimal digits before actually parsing and outputting them. This would be useful for things like the %g fomat mark in a printf() format string, which decides whether %f or %e would be shorter, then outputs the shortest one, or deciding the exact size of a buffer to be written into before so that it can be preallocated. Simply not doing it is not an option.
Advertisement
Significant decimal digits in base 10, or base 2?

For base 2, yes, you can tell how many significant digits are present trivially: there are 23 in single precision IEEE floats, and 52 in double-precision. You can use simple bit-counting algorithms to count how many of them are relevant in a particular value.

For base 10... well, not really. You have to convert the fraction to base 10 to know how many significant digits it represents (if it can even represent the base 10 value precisely!) at which point you've basically done a string format anyways.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Yeah, I was afraid of that. In base 10 is for what I am looking; I can easily extract the components of a float in an endian-safe manner. However, I was hoping to avoid having to do a dry run to calculate the digit count; if I hit the maximum precision and it can't be represented exactly, that's fine too. I'm mostly going for parsing to a string, so what data comes in and its accuracy is of little concern at this stage. If there is no other way, then I'll do that.

For base 2, yes, you can tell how many significant digits are present trivially: there are 23 in single precision IEEE floats, and 52 in double-precision. You can use simple bit-counting algorithms to count how many of them are relevant in a particular value.


Ignoring denormalized floats, I think there's 24 bits of precision, since there's an implicit leading one that is not stored.

That aside, Bruce Dawson digs into floats in a very nice manner, and has got a blog post about float precision in base 10. The TL;DR simplified answer is "... range from 6-9 decimal digits" and "Use 9 to play it safe."

[quote name='ApochPiQ' timestamp='1336185352' post='4937525']
...


Ignoring denormalized floats, I think there's 24 bits of precision, since there's an implicit leading one that is not stored.

That aside, Bruce Dawson digs into floats in a very nice manner, and has got a blog post about float precision in base 10. The TL;DR simplified answer is "... range from 6-9 decimal digits" and "Use 9 to play it safe."
[/quote]

Right, I know the maximum decimal precision for a given float. I am looking for the number of significant digits in any given float. 1.0 would be one, 12321.4535 would be nine, 0.145 would be three, etc.
In that case I believe the FLT_DIG constant is the one you are looking for, found in float.h. It is platform dependant, on most PCs it is 6.

That is the best precision that you can convert to decimal digits and back without loss.

Otherwise, the answer is that you ALWAYS have 23 bits of precision. The FPU doesn't arbitrarily stop midway, it carries out the entire operation.

If you want to keep track of how much of that answer you care about, you'll need to track it on your own in some other variable.
Okay, I will count it myself. Thank you for your time.

This topic is closed to new replies.

Advertisement