What number in binary can only be represented as an approximation?

前端 未结 10 684
礼貌的吻别
礼貌的吻别 2021-01-16 12:10

In decimal (base 10), 1/3 can only be approximated to 0.33333 repeating.

What number is the equivalent in binary that can only be represented as an app

相关标签:
10条回答
  • 2021-01-16 12:25

    A better question is to ask what numbers can be represented exactly in binary. Everything else can only be approximated or not represented at all.

    See What every computer scientist should know about floating point arithmetic.

    0 讨论(0)
  • 2021-01-16 12:29

    Well, there are infinite numbers that can't be precisely represented in that notation, but here's one: 1/10.

    0 讨论(0)
  • 2021-01-16 12:32

    I am assuming that you mean to ask which rational numbers can be expressed in binary using a finite representation. I am deducing this from your example of 1/3 in decimal. The fact is that every rational number can be expressed in binary if you allow infinite representations. But this question is only interesting from a computer science perspective if you only permit finite representations. I am further assuming that you are not asking about specific computer representations (say, IEEE 754) but rather merely asking about general positional representations.

    A rational number p/q with (p, q) = 1 can be expressed a finite representation in base b if and only if every prime factor of q divides b. No irrational numbers have a finite representation in any base.

    In particular, a rational number p/q with (p, q) = 1 can be expressed as a finite representation in binary if and only if every prime factor of q divides 2. That is, the only rational numbers p/q with (p, q) = 1 that have a finite representation in binary are those where q = 2^k for some nonnegative integer k. Moreover, all such rational numbers can be expressed in a finite representation in binary. These numbers are known as dyadic rationals.

    0 讨论(0)
  • 2021-01-16 12:33

    In python 2.4:

    >>> 1.0 / 5.0
    0.20000000000000001
    

    That indicates that base 2 has a hard time representing it exactly.

    0 讨论(0)
提交回复
热议问题