I have tried to multiply large integers (also tried double) in xcode. I used NSLog to track errors in my calculation (simple calculations) and I noticed that when I mult开发者_运维技巧iply variables with large values such as (I looked them up with NSLog): 31536000 * 91 = 2869776000 I get 1610612736, which is wrong. What do I do wrong?
/D
What type of data do you use for your numbers? If it is NSInteger, or something like that, it's close to the standard int, so it has some limitation.
For example, on standard 64 bits architecture you can declare a long long int which permits you to have a 64 bits integer. If your multiplication requires more place, the left most bits may be truncated, which changes your result.
You could also use some long double, which permits you to handle 80 bits numbers which extended double precision, but it still loose precision on big numbers.
You need to make sure you're using the right string format specifier when you output the results. For large numbers, you probably want to be using %lld
(or %llx
) for long long
or %llu
(or, again, %llx
) for unsigned long long
.
What string format specifier are you using to output your value?
I tried using long longm which initially did not seem to work, but when I used the string format specifier %11u in my NSLog I got the right output from my calculation!
Thank's a lot for the answers!
/D
精彩评论