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

对着背影说爱祢 提交于 2019-12-04 02:11:39

问题


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 approximation?


回答1:


0.1 is one such example, as well as 0.2

This question is also similar to this other SO question, which already has very good answers.




回答2:


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.




回答3:


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




回答4:


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.




回答5:


The numbers that can be exactly represented in base 2 are the dyadic rationals. These are numbers that can be written in the form k/2^n for some integer k and whole number n. Any number that cannot be written in that form will have a non-terminating representation in base 2.

However, you seem to be asking not about what numbers are representable in base 2, but rather what numbers are representable in some fixed floating-point type, such as float or double. This is a more subtle question; any number that is not a dyadic rational cannot be represented, but not all dyadic rationals can be represented either.




回答6:


It's every number that can't be expressed as k/2^n for integer k and whole number n.

The easy way to find all these numbers is to write down some prime numbers that do not include 2. 3, 5, 7, 11, 13, 17 and 19 are good examples of prime numbers that don't include 2.

Start multiplying. 1/3, 2/3, 1/5, 2/5, 3/5, 4/5, 1/6, 5/6, 1/7, 2/7, etc.

if you do this -- and you avoid numbers of the form k/2^n -- you'll enumerate every possible fraction that cannot be exactly represented in binary.

You should probably stop enumerating when you get to numbers for which the left-most 64-bits are all identical.




回答7:


In python 2.4:

>>> 1.0 / 5.0
0.20000000000000001

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




回答8:


binary(.00011001100110011...) == decimal(.1)




回答9:


I'm going to take a stab at infinity




回答10:


The same set of numbers that can't be exactly represented by base 10 can't exactly be represented by base 2. There shouldn't be a difference there.



来源:https://stackoverflow.com/questions/1674477/what-number-in-binary-can-only-be-represented-as-an-approximation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!