**0**

# Getting number of significant digits in float

Started by Ectara, May 04 2012 07:49 PM

6 replies to this topic

###
#1
Crossbones+ - Reputation: **2764**

Posted 04 May 2012 - 07:49 PM

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.

Sponsor:

###
#2
Moderators - Reputation: **14260**

Posted 04 May 2012 - 08:35 PM

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.

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, 04 May 2012 - 08:36 PM.**

Maker of Machinery

[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]

[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]

###
#3
Crossbones+ - Reputation: **2764**

Posted 04 May 2012 - 10:08 PM

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.

###
#4
Members - Reputation: **1777**

Posted 05 May 2012 - 01:48 AM

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."

Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

###
#5
Crossbones+ - Reputation: **2764**

Posted 05 May 2012 - 09:52 AM

...

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."

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.

###
#6
Moderators - Reputation: **18836**

Posted 05 May 2012 - 10:07 PM

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.

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.

Check out my personal indie blog at bryanwagstaff.com.