Reputation: 31
I'm creating a change counter program for my C++ class. The numbers we are using are in the 10s of trillions and I was wondering if there was an easy way to store that into a floating point type variable then cast that into an integer type. It isn't an integer literal, it's accepted as an input and I expect possible change.
Upvotes: 0
Views: 2174
Reputation: 3221
If you use floating point math to represent your change counter you'll get in serious troubles. Why? - You are a victim of accuracy problems that lead to problems representing values differing in the 1s, 10s and 100s and so on up to (IIRC) 10^6 of the values. (assuming you are referring to 10^12 version of the term 'trillion'. See H. Schmidt's IEEE 754 Converter page and the Wikipedia article about thisif you want deeper insight into this)
So if you need a precision that goes higher than a several million (and I assume you do), you'll really get in hot water if you use such a beast like floating points. You really need something like the (multiple precision library from GNU in order to be able to calculate the numbers. Of course you are free to implement the same functionality yourself.
In your case mabye a 64-bit integer could do it. (Note that long long is not always 64 bit and nonstandard for C89) Just parse the user input yourself by doing something like this (untested, just to illustrate the idea):
const char input[] = "14.5"
uint64_t result = 0;
uint64_t multiplier = 1000000000000;
unsigned int i = 0;
/* First convert the integer part of the number of your input value.
Could also be done by a library function like strtol or something
like that */
while ((input[i] != '.')
&& (input[i] != '\0'))
{
/* shift the current value by 1 decimal magnitude and add the new 10^0 */
result = (result * 10) + (input[i] - '0');
i++;
}
/* Skip the decimal point */
if (input[i] == '.')
{
i++;
}
/* Add the sub trillions */
while (input[i] != '\0')
{
/* shift the current value by 1 decimal magnitude and add the new 10^0 */
result = (result * 10) + (input[i] - '0');
multiplier /= 10; // as this is just another fraction we have added,
// we reduce the multiplier...
i++:
}
result = result * multiplier;
Of course there are a several exceptions that need to be handled seperatly like overflows of the result or handling non numeric characters properly but as I noted above, the code is only to illustrate the idea.
P.S: In case of signed integers you have to handle the negative sign too of course.
Upvotes: 0
Reputation: 104589
Don't use floats. Keep it as an integer and use 64-bit longs. Use "long long" or "int64_t" as the type for storing these integers. The latter can be used by #include <stdint.h>
int main()
{
long long x = 1450000000000LL;
printf("x == %lld\n", x);
return 0;
}
Upvotes: 3
Reputation: 2322
Uhm. No :D
You can however use matrices and write functions for the mathematical operations you need to use. If you're doing a lot or arithmetic with very large numbers, have a look at http://gmplib.org/
Upvotes: 0