tags:

views:

190

answers:

5

additional please convert this exponential no. into int no. 5.52794e+15

+2  A: 

It won't fit in an int, but a long long will do:

long long x = 5.52794e+15;
Andreas Brinck
A: 
long long x = static_cast<long long>(5.52794e+15);
skwllsp
This forces the compiler to convert the double constant into a long long. It works, but the conversion should be noted.
Jonathan Leffler
is static_cast that what you mean?
skwllsp
+2  A: 

Alternative: you want to print it as an integer.

double d = 5.52794e+15;
printf("%15.0lf\n",d );

Gives:

5527940000000000
Pim
A: 

long long x = 5.52794e+15; where the exponent "15" has to be an integer, and the coefficient 5.52794 is any real number or could even be integer. The maximum value of the mantissa is just over 32,000,000 Any value above this will be truncated. This becomes significant where you have a running total and are adding in small values and keeping an accurate total. - for example power usage where the value that you are adding in is not a whole number.

wrapperm
Although I didn't give you the downvote, I'm not surprised you got one, I'm afraid. I don't understand where you get the idea that the mantissa is limited to 32 million; it sort of makes sense for a 4-byte float value, but the 5.52794e+15 is a double, and you normally get many more digits of precision than implied by your 32M comment. The idea about keeping totals in double _is_ something to worry about, but not clearly relevant to this specific context.
Jonathan Leffler
+1  A: 

Besides using long longs, you could also:

Use a packed decimal library.

You could use logarithmic (or geometric, etc) scaling in a standard int or long.

Use a structure of ints, representing the significand and exponent, and do floating point math manually (or with a good library).

I've used all three methods, they all have their ups and downs. Packed Decimal is slowest and most accurate. Logarithmic scaling is by far the fastest and easiest to implement, and least accurate. Reproducing floating point via integers is in-between in performance, essentially the same in accuracy as "real" floating point, and hardest to implement.

All 3 are slower than using floating point hardware- assuming your hardware has floating point!

kmarsh