Thread: Double Accuracy

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    19

    Double Accuracy

    How accurate are the results of this app?
    Code:
    #include <iostream>
    #include <conio>
    
    using namespace std;
    
    void _In_Out()
    {
    	int sides;
    	cout << "How many dimensions(2-255) does the hypercube have? ";
    	cin >> sides;
    	
    	double area = 1, hypercube[255];
    
    	for(short i = 1; i <= sides; i++)
    	{
    		if(hypercube[i] == 0) break;
    		cout << "Enter dimension units(u)" << i << ": ";
    		cin >> hypercube[i];
    		area = area * hypercube[i];
    	}
    
    	cout << "\n\n\tThe hypercube has an area of: " << area << "u^" << sides;
    	
    }
    
    
    int main()
    {
    	clrscr();
    	
    	_In_Out();
    
    	return 0;
    }
    Assuming the user enters 255 values of 9999999u each, would the answer be accurate?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rickyoswaldiow
    How accurate are the results of this app?

    Assuming the user enters 255 values of 9999999u each, would the answer be accurate?
    Why not write a function to do just that and compare your expectations to the result?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You should drop the line

    Code:
      if(hypercube[i] == 0) break;
    since at this point it hasn't even been initialized yet.

    Edit: Also, i should go from 0 to sides-1.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That depends entirely on the particular architecture this is running under. But if I were to venture a guess, I'd say no: on the typical x86, double is a 64-bit-wide type. If it was integral (it isn't), it could represent at most 2^64 with integral accuracy. A hypercube in 255 dimensions with 99999999 side length each would have a hypervolume (or whatever it's called) of 99999999^255. Obviously, that's quite a bit more than can possibly be represented accurately.

    There are arbitrary precision math libraries that can do what you want.

    Note that the program is actually malformed, because names starting with an underscore followed by an uppercase letter are reserved for the compiler and standard library implementation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    Dave : I would do that only I have absolutely no idea what to 'expect' the result to be :P

    robatino : I think that line is okay, when the array is initialised it has some random value in it, only when it reaches the end of the array (or if the user entered 0 for one of dimensions by accident) will it break.

    CornedBee : I see I'll just have to make a note to the user in the programs introduction.
    Code:
    #include <iostream>
    #include <conio>
    
    using namespace std;
    
    void in_out()
    {
    	short sides;
    	cout << "How many dimensions(1-255) does the hypercube have? ";
    	cin >> sides;
    	
    	double area = 1, hypercube[255];
    
    	for(short i = 1; i <= sides; i++)
    	{
    		if((sides > 255) || (hypercube[i] == 0)) break;
    		cout << "Enter dimension units(u)" << i << ": ";
    		cin >> hypercube[i];
    		area = area * hypercube[i];
    	}
    
    	cout << "\n\n\tThe hypercube has an area of: " << area << "u^" << sides;
    	
    }
    
    
    int main()
    {
    	clrscr();
    	
    	in_out();
    
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The random value could be (and often is) 0.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    I see, this seems to run fine for me every time though. What should I replace that with? Can I check against Null instead of 0?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If a variable is uninitialized, you can't make any assumptions about what it is, or isn't, equal to. Period. It could be equal to any possible value. If you want to guarantee that it's not 0, you need to explicitly set it to a nonzero value.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Assuming the user enters 255 values of 9999999u each, would the answer be accurate?
    No, it would not. The result is bigger than a double can represent and would hence come out as infinity. (#INF)

    You would need to use a long double in order to not overflow to infinity. But even then the result isn't even accurate to the nearest billion.


    Note, you don't need to store 255 values if you process each one after reading it and never refer to it again.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by rickyoswaldiow
    I see, this seems to run fine for me every time though. What should I replace that with? Can I check against Null instead of 0?
    You remove it without replacement. Then you correct the loop index (array indices go from 0 to length-1, but your array loops from 1 to length).
    If you want to ensure that the user doesn't enter more than 255 sides, you validate the value of sides before the loop. Truncate it to 255 and inform the user that it has been truncated. Or skip the entire loop, because if the user wanted a 300-dimensions cube, he won't be happy at being able to calculate a 255-dimensions cube.

    Speaking of which, the term "side" is slightly inappropriate, I think. Though I've never formally learned about the terminology.

    Good point by iMalc about not storing all the values, too.
    But in Visual C++, double and long double have the same precision.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    I understand. Thanks for all the help but it's just a simple app to display how an array can be used, it doesn't need to be fool proof

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Another method to avoid overflow is to use the fact that the log of the product is equal to the sum of the logs. If you add up the base 10 logs of each side, the sum is equal to the base 10 log of the product. Then the integer part of this number is equal to the exponent, and 10 raised to the fractional part is the mantissa. For example, if the sum is 127.653, the mantissa is 10^(0.653) = 4.4977... and the exponent is 127, so the product is 4.4977 * 10^127.

    Edit: For this to work, each side has to be a positive number. Also, the fractional number you use to get the mantissa has to be between 0 and 1 to get your output in standard scientific notation, so for example if the sum of the logs was -5.3, you would have to write this as 0.7 + (-6). Since 10^(0.7) = 5.0118..., the product would be 5.0118 * 10^(-6).
    Last edited by robatino; 10-23-2006 at 02:59 AM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > cin >> hypercube[i];
    On the last iteration of the loop (when i is 255), this results in an out of bound array access.

    Here be dragons!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM