Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Any rational number can be represented as a fraction p/q, where p and q are integers with no common factors (reduced fraction).

If that fraction is representable as an exact "decimal" number in base b that means that p/q = m/b^n = m * b^(-n), where m and n are integers too. For example, 3/4 = 75/10^2 or 0.75 in decimal, 3/2^2 or 0.11 in binary.

That means p * b^n = m * q. We said p and q have no common factors, so all of q's factors go into b^n. That means that q divides b^n, or in other words:

p/q is representable as an exact "decimal" number in base b if and only if q divides some power of b.

For example, 0.1 is 1/10. But there is no power of 2 which is divisible by 10, so 0.1 is not exactly representable in binary.

As another example, 1/3=0.33333.... because there is no power of 10 divisible by 3.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: