Arithmetic in ruby

后端 未结 6 1442
日久生厌
日久生厌 2020-12-16 06:25

Why this code 7.30 - 7.20 in ruby returns 0.0999999999999996, not 0.10?

But if i\'ll write 7.30 - 7.16, for examp

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 06:58

    It is interesting to note that a number that has few decimals in one base may typically have a very large number of decimals in another. For instance, it takes an infinite number of decimals to express 1/3 (=0.3333...) in the base 10, but only one decimal in the base 3. Similarly, it takes many decimals to express the number 1/10 (=0.1) in the base 2.

提交回复
热议问题