How do you print the EXACT value of a floating point number?

后端 未结 10 1303
自闭症患者
自闭症患者 2020-11-27 03:45

First of all, this is not a floating point newbie question. I know results of floating point arithmetic (not to mention transcendental functions) usually cannot be represent

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 04:29

    Off the top of my head, why not break the exponent down into a sum of binary exponents first, then all your operations are loss-less.

    I.e.

    10^2 = 2^6 + 2^5 + 2^2
    

    Then sum:

    mantissa<<6 + mantissa<<5 + mantissa<<2
    

    I'm thinking that breaking it down would be on the O(n) on the the number of digits, the shifting is O(1), and the summing is O(n) digits...

    You would have to have an integer class big enough to store the results, of course...

    Let me know - I'm curious about this, it really made me think. :-)

提交回复
热议问题