# determine how many 0's are in a number (value)

• 07-06-2005
pyazarian
determine how many 0's are in a number (value)
Does anybody know how I can determine how many 0's are in a number to determine decimal value accuracy?

here is my function:

Code:

``` //detrmn_accuracy function double detrmn_accuracy (double calc_pi, double pi) {         //declare local variables         double accuracy;         double diff;         //function         diff = calc_pi - pi;         accuracy = fabs(diff);         return accuracy; }```
The function is supposed to subtract an estimated value of pi from actual pi and determine how close the estimated value is to actual value. Right now, it only outputs the difference between the two values. Thanks.
• 07-06-2005
dwks
So are you trying to figure out how many digits to the right of the decimal point a double supports? Or:
Code:

```#include <math.h> double diff(double guess) {     return fabs(PI-guess); } if(diff(guess) < .0001) cout << "close";```
• 07-06-2005
pyazarian
well, your suggestion is close to what i need. I am not really trying to determine how many digits to the right of the decimal point a double supports, but rather how close estimated pi's value is to actual pi's value by looking at how many 0's are in the difference. For example, if pi is 3.1415926535897932384 and estimate pi is 3.144967310538, the difference is .0033746518. Therefore, the estimated pi value is 2 decimal places accurate to the true value of pi. I hope this make sense.

so, in your example above, I like how you :

if(diff(guess) < .0001)

but, I am not going to be able to use a predetermined decimal precision. it could be 0, or ,.01, .001, or .0001, etc.

accuracy needs to output a decimal place (ex. 2 decimal places) , not really a string like "closer".

sorry i can't explain better, but I am a beginner. thanks again for your help.
• 07-06-2005
hk_mp5kpdw
Quote:

Originally Posted by pyazarian
The function is supposed to subtract an estimated value of pi from actual pi and determine how close the estimated value is to actual value. Right now, it only outputs the difference between the two values.

Aren't those the same thing? ;)

Maybe you could do something like:

Code:

```1.  Declare a counter initialized to 0 2.  While difference*10 < 1   a.  Increment counter variable by 1   b.  Multiply difference by 10.0 3.  Return counter```
Thus if PI = 3.1415926 and your estimate is 3.14 then the diff is 0.0015926 and the number of 0's is two right? But this method doesn't really help distinguish the accuracy of that estimate versus something like 3.1400026 which has a difference of 0.00159 and is therefore closer to the actual value than 3.14 and yet has the same number of zero's (2). I would just use the actual difference itself as a measure of accuracy.
• 07-06-2005
Dave Evans
Quote:

Originally Posted by pyazarian
Does anybody know how I can determine how many 0's are in a number to determine decimal value accuracy?

You can use the function log10. The logarithm of a number smaller than one is a negative number, and the integer part tells the number of zeros.

Here is an example

Code:

```#include <stdio.h> #include <math.h> int main() {   double x, y, z;   x = 3.14159265358979323846; /* more than enough digits */   y = x + .00000001;   printf("x    =  %.20f\n", x);   printf("y    =  %.20f\n", y);   printf("diff =  %.20f\n", fabs(y - x));   if (y == x) {     printf("exact\n");   }   else {     z = log10(fabs(y-x));     printf( "z    = %.20f\n", z);   }   return 0; }```
You may see something like this:
Quote:

x = 3.14159265358979311600
y = 3.14159266358979305522
diff = 0.00000000999999993923
z = -8.00000000263941224432
I see eight leading zeros after the decimal point, and the integer part of z is 8.

This is just an experiment. You should run it with various values of errors to make sure you understand what's happening. Put in different values of error (or make a program loop that lets you enter different values) to see the result using log10.

Note that doubles are represented internally to about 16 significant decimal digits.

Regards,

Dave