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

This is a discussion on determine how many 0's are in a number (value) within the C++ Programming forums, part of the General Programming Boards category; Does anybody know how I can determine how many 0's are in a number to determine decimal value accuracy? here ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    2

    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.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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";
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    2
    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.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    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:
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21