# Double Accuracy

• 10-22-2006
rickyoswaldiow
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?
• 10-22-2006
Dave_Sinkula
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?
• 10-22-2006
robatino
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.
• 10-22-2006
CornedBee
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.
• 10-22-2006
rickyoswaldiow
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; }```
• 10-22-2006
robatino
The random value could be (and often is) 0.
• 10-22-2006
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?
• 10-22-2006
robatino
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.
• 10-23-2006
iMalc
Quote:

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-23-2006
CornedBee
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.
• 10-23-2006
rickyoswaldiow
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:rolleyes:
• 10-23-2006
robatino
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).
• 10-23-2006
Salem
> 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!