Problem with 9999999999999999999 as a long double or long int
Hi
I have a problem trying to add 9999999999999999999 and -1 for example.
My result is
The program is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char c;
long long sum = 0LL;
long long num = 0LL;
bool diff = false;
bool suma = false;
while ((c = getchar()) != EOF)
{
if(c == '-')
diff = true;
if(c == '\n')
{
if(diff)
sum-=num;
else
sum+=num;
printf("%lld\n", sum);
sum = 0;
num = 0;
suma = false;
diff = false;
}
else if((c >= '0') && (c <= '9'))
{
suma = false;
num*=10;
num += c - '0';
}
else if((suma == false)&&(c ==' ')){
if(diff)
sum-=num;
else
sum+=num;
num = 0;
suma = true;
diff = false;
}
}
sum+=num;
printf("%lld\n", sum);
return EXIT_SUCCESS;
}
|
Thanks a lot.
Maria
Last edited on
Run this code and see what is the maximum value long long can have. You will probably find that is is lower than 9999999999999999999.
1 2 3 4 5 6 7
|
#include <limits>
#include <iostream>
int main()
{
std::cout << std::numeric_limits<long long>::max() << std::endl;
}
|
Thank you very much. Finally had to use a long double and print it with %.0Ld.
Regards,
Maria
Topic archived. No new replies allowed.