Tech Support > Computers & Technology > Programming > Calculate the precision of a floating point number (ie: the number of decimal places)
Calculate the precision of a floating point number (ie: the number of decimal places)
Posted by Lori on October 30th, 2007


Hi all

I'm trying to write a reliable and efficient library function that
will calculate the precision of an input number (floating point).

Please excuse my lame mathematics skills! Here is my first attempt at
a function:

int dec_places(double d)
{
int places = 0;
while (!feq(d - (int)(d + 0.5), 0.0)) // where feq returns
true if inputs differ by less than epsilon 0.0000005
{
d *= 10;
++places;
}
return places;
}

The problem I have with the above solution is that it is possible to
get caught in an infinite loop.

One solution would be to have a max iterations, and break out of the
loop if this is reached (thereby causing an error condition, and the
caller must process the return value as an invalid response), but this
is not ideal.

I have searched for answers on the web and in newsgroups to no avail.

Does anyone know of a better algorithm? Perhaps there is an open
source solution available that I am not aware of?

Thanks for all your help
Regards
Lori

Posted by Ed Prochak on October 30th, 2007


On Oct 30, 10:21 am, Lori <steve.lori...@gmail.com> wrote:

First, can you tell me how many significant digits exist in these
numbers
0.01e-21
0.00
0.000000
???????

because you haven't thought about what precision really means.

To use a New hampshire proverb:
you can't get there from here

What you need is to better define your requirements.
Ed


Posted by user923005 on October 30th, 2007


On Oct 30, 7:21 am, Lori <steve.lori...@gmail.com> wrote:
Sure,
return DBL_EPSILON; /* for double */
return FLT_EPSILON; /* for float */
return LDBL_EPSILON; /* for long double */

If you are bound and determined to compute it yourself, look at
paranoia.c which computes this value from first principles.



Posted by CBFalconer on October 30th, 2007


Ed Prochak wrote:
Yes. Just count the digits typed. The answers are 3, 3, 7.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com


Posted by Logan Shaw on October 31st, 2007


Ed Prochak wrote:
How are the last two going to look any different from each other in
floating point?

- Logan

Posted by Mike on October 31st, 2007


In article <4727ee83$0$20603$4c368faf@roadrunner.com>, lshaw-usenet@austin.rr.com says...
particular size are usually stored/represented internally with approximately the same number of significant digits.

Mike

Posted by Lori on October 31st, 2007


On Oct 30, 7:28 pm, Ed Prochak <edproc...@gmail.com> wrote:
I guess

1. 0.01e-21 = 0.00000000000000000000001: so 23 decimal places
2. 0.00 = 0: so 0 decimal places
3. 0.000000 = 0: so 0 decimal places

For sure. So the requirement is this: represent the decimal number as
an integer and a "format indicator", which is '0' - '9' for a positive
number with 0 to 9 decimal places respectively, or 'A' to 'J' for a
negative number with 0 to 9 decimal places respectively

Therefore: 34.125 would be 334125, and -10.0005 would be D100005

So my function is to take any "double" input, calculate the number of
dec places / precision / sig digits (not sure of correct terminology,
of if indeed there is any difference there), and return an integer.

ie: for
input 34.125 output 3
input -10.0005 output 4
input 0.0000 output 0



Posted by Richard Harter on October 31st, 2007


On Wed, 31 Oct 2007 11:33:44 -0000, Lori
<steve.lorimer@gmail.com> wrote:


Your definition of precision does not correspond to standard
usage. In scientific work the precision of a number is the
number of places in decimal format using exponent notation with
no leading digits. Thus your examples are:

..34125e+2 -- precision = 5
-.100005e+2 -- precision = 6
..0000e+0 -- precision = 4

Briefly, the precision of a number (measurement or calculated
result) is the number of significant digits. In their internal
form floating point numbers all have approximately the same
precision (modulo binary to decimal conversion jitter).

Your "precision" is the number of places after the decimal point
when the number is converted from floating format (basically
exponential form with a fixed precision) to a fixed point format.
You might think about why you want to do this.

According to your description you wanted the "precision" of
floating point input. This doesn't make a whole lot of sense.
By your rules it is some constant minus the the exponent of
the input.

The impression I get from your description is that you want the
precision of a number specified by an input character string.
You might try to clarify what it is that you are after.





Richard Harter, cri@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die

Posted by Ed Prochak on October 31st, 2007


On Oct 31, 7:33 am, Lori <steve.lori...@gmail.com> wrote:
I did not think you wanted the precision (since with internal floating
point you can't get it anyway)

You still have a problem. 10.0005 might be stored internally as
10.0004999 (not all real number values can be represented precisely in
binary floating point). so would you want the result to be 4 or 7 in
that case?

If you are going to count "decimal places", then I suggest you do
that. a simple approach would be to convert the internal binary
floating point to a string representing the decimal numbeer (IOW a
string as if you were going to print it out. Then count the digits in
that string.

I'm assuming this is home work, so I'll leave you to take that hint
and run with it.

Let us know how you do.
Ed


Posted by user923005 on October 31st, 2007


On Oct 30, 7:21 am, Lori <steve.lori...@gmail.com> wrote:
You cannot know the precision of a value by examination.
You need to ask the user for the precision (and hope that they know
the answer).
The user input '1' may have infinite precision (there is exactly one
item). E.g. 'How many "Empire State Buildings in New York, New York
are there?"
The user input 3.1425936535 may have 3 digits of precision because the
digits will not be repeatable on the next measurement and they are
simply reporting all the digits they see when measuring or
calculating.