PDA

View Full Version : double rounding


dukie4lif
03-09-2006, 06:55 PM
I have an C++ programming question. I am trying to convert a large integer to double by multiplying by .01 My integer is something like this 2173079778, so to convert to double I am multiplying by .01 and the result should be 21730797.78. however when I do this the result is 2.1308e+009. I am thinking perhaps I have reached the limits of the double datatype though I beleive the double datatype should be able to handle numbers of 15 significant digits and 6 significant decimal digits. For some reason though it is rounding, so I am not sure if it is because number is too large for double or it is just rounding. I can not have it round at all, I am dealing with financial data and rounding is unacceptable. Any ideas?

mentalhorse
03-13-2006, 12:23 PM
This happens on my calculator all the time. I'm trying to do a calculation for chemistry and its something like 1X10^-6 and it gives me that e+# bullcrap. I havn't found a solution.

NancyJ
03-13-2006, 12:41 PM
That "that e+# bullcrap" is called standard index notation
http://en.wikipedia.org/wiki/Standard_index_notation

a E^b means a x 10 to the power of b and is used to represent extremely large (in number of digits) numbers.
Particularly useful on calcualtors that can only display 8-12 digits.

@dukie4lif it could just be a display issue. It may be that the number is being calculated and stored correctly but cannot be displayed in such a long form.
Instead of multiplying by 0.1 to get a double from an int why not cast the int as a double? This avoids the issue completely. You say you're dealing with financial data and it has to be accurate, then why multiply by 0.1 to start with? You're introducing inaccuracies there.

Melon00
03-13-2006, 06:32 PM
You are passed the limits for a signed int. Use an unsigned int and then do an explicit caste to a double. You should be good then. BTW, limit for an unsigned int (4 bytes) is 2^16-1 = 2147483647, and an unsigned int (4 bytes) has a max value of 2^32-1 = 4294967295.

Melon00
03-13-2006, 06:49 PM
One more thing, whoever's account that is...tell them to share the love...

hyperbole
03-14-2006, 05:38 PM
The values you can store in various types differ from system to system and from compiler to compiler. To accurately answer your question we need to know the number of bits in the integer variable and the number of bits used by your floating point value.

Since you say you have an integer value of 2173079778 stored as an integer, I'm assuming you are using a 'long long' with 64bits of storage since, as Melon00 pointed out, 2173079778 won't fit into a 32bit integer value. And, since the title of your thread is "double rounding", I'll assume you are using a double with at least 64bits of storage. Note: if you are converting a 64bit integer to a floating point value, you really should be using a floating point variable with at least 80bits of storage so that you don't round the integer value on conversion.

In that case, you are not getting rounding when you do the multiplication. At least, not as much as your figures indicate. However, the value is being rounded when you print it, as NancyJ pointed out. I would guess that you are doing something like "cout << double_value" and the compiler assumes you want the value printed in standard notation.

Try the following:

long long integer_value = 2173079778;
double real_value = (double)integer_value;

printf("%11.1lf\n", real_value);


That should print out your value as you expect to see it. If you are not familiar with printf notation, come back and ask some more questions and I will try to help you with it.



.