Interesting result in c++


  • 0
    A

    When printing out the value of some calculation:

    log(243)/log(3) gives 5

    floor(log(243)/log(3)) gives 4

    floor(5) gives 5

    Why???


  • 1
    S

    I tried in python 2.7.10. I think the result is the same in c++.

    In [2]: import math

    In [3]: math.log(243)
    Out[3]: 5.493061443340548

    In [4]: math.log(3)
    Out[4]: 1.0986122886681098

    In [5]: math.log(243)/math.log(3)
    Out[5]: 4.999999999999999


  • 0
    A

    Maybe you can try "round" instead of "floor".


  • 1
    A

    It possibly turns out to be 4.9999999999 due to round off errors in floating point arithmetic, floor() of which is 4.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.