pow() seems to be out by one here

前端 未结 4 489
青春惊慌失措
青春惊慌失措 2020-12-01 13:26

What\'s going on here:

#include 
#include 
int main(void) {
    printf(\"17^12 = %lf\\n\", pow(17, 12));
    printf(\"17^13 = %l         


        
4条回答
  •  余生分开走
    2020-12-01 14:08

    The numbers you get are too big to be represented with a double accurately. A double-precision floating-point number has essentially 53 significant binary digits and can represent all integers up to 2^53 or 9,007,199,254,740,992.

    For higher numbers, the last digits get truncated and the result of your calculation is rounded to the next number that can be represented as a double. For 17^13, which is only slightly above the limit, this is the closest even number. For numbers greater than 2^54 this is the closest number that is divisible by four, and so on.

提交回复
热议问题