Friday, 15 July 2011

floating point - C++ double to long long -



floating point - C++ double to long long -

if write code in c++:

long long d = 999999998.9999999994; cout<<d;

i output: 999999999 (rounding up)

but output of code:

long long d = 999999998.9999994994; cout<<d;

is 999999998 (rounding down)

is precision. there way can alter precision. floor() function gives same output.

i noticed if assign value 8.9999994994 or 8.9999999994 d(above variable). output 8.

999999998.9999999994 not representable in double, actual value 1 of 2 representable numbers on either side of 999999998.9999999994 - either 999999998.99999988079071044921875 or 999999999 (assuming ieee-754 binary64 format), selected in implementation-defined manner. systems default round nearest, producing 999999999.

the net result on systems when write 999999998.9999999994 ends having exact same effect writing 999999999.0. hence subsequent conversion yields 999999999 - conversion floating point number integer truncates, here there nil truncate.

with 999999998.9999994994, closest representable numbers 999999998.999999523162841796875 , 999999998.99999940395355224609375. either 1 produces 999999998 after truncation. similarly, 8.9999999994, closest representable numbers 8.999999999399999950355777400545775890350341796875 , 8.9999999994000017267126168007962405681610107421875, , either 1 produce 8 after truncation.

c++ floating-point double floating-point-conversion

No comments:

Post a Comment