printf("%.0lf\n", x);
is not required to print the exact value of (double) x
.
A compliant C compiler/standard C library will generate code that will print about DBL_DECIMAL_DIG
(typically 17), correct significant decimal digits. Other may print more digits correctly. (Mine printed 9223372036854775808
). See <float.h>
// 45678901234567
9223372036854775800 // answer
9223372036854775808 // true answer:
Extreme example
DBL_MAX
may have the exact value of
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
Yet many results of printf("%.0lf\n" ,DBL_MAX);
will report zeros after a certain point
179769313486231570814527423731704356798070600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
OP later added that the reference pow()
function was long long int pow(long long int a, long long int b)
. pow_ll_version(2,63)
would be expected to result in 9223372036854775808
, which is 1 passed many LLONG_MAX
- certainly undefined behavior. Let us assume it wrapped to -9223372036854775808
, and was printed with printf("%llu\n" , long_long_int_result);
(more UB as mis-matched specifier and type). This may have given 9223372036854775808
to OP.
printf()
output of integers is exact, not so with floating point values.