Sign in to follow this  

Getting number of significant digits in float

This topic is 2047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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. Edited by ApochPiQ

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1336185352' post='4937525']
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.
[/quote]

Ignoring denormalized floats, I think there's 24 bits of precision, since there's an [url="http://en.wikipedia.org/wiki/Single-precision_floating-point_format#IEEE_754_single-precision_binary_floating-point_format:_binary32"]implicit leading one[/url] that is not stored.

That aside, Bruce Dawson digs into floats in a very nice manner, and has got a [url="http://randomascii.wordpress.com/2012/03/08/float-precisionfrom-zero-to-100-digits-2/"]blog post about float precision in base 10[/url]. The TL;DR simplified answer is "... range from 6-9 decimal digits" and "Use 9 to play it safe."

Share this post


Link to post
Share on other sites
[quote name='clb' timestamp='1336204083' post='4937564']
[quote name='ApochPiQ' timestamp='1336185352' post='4937525']
...
[/quote]

Ignoring denormalized floats, I think there's 24 bits of precision, since there's an [url="http://en.wikipedia.org/wiki/Single-precision_floating-point_format#IEEE_754_single-precision_binary_floating-point_format:_binary32"]implicit leading one[/url] that is not stored.

That aside, Bruce Dawson digs into floats in a very nice manner, and has got a [url="http://randomascii.wordpress.com/2012/03/08/float-precisionfrom-zero-to-100-digits-2/"]blog post about float precision in base 10[/url]. 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this