Why can't decimal numbers be represented exactly in binary?

前端 未结 20 3477
不知归路
不知归路 2020-11-21 05:15

There have been several questions posted to SO about floating-point representation. For example, the decimal number 0.1 doesn\'t have an exact binary representation, so it\'

相关标签:
20条回答
  • 2020-11-21 05:46

    In the equation

    2^x = y ;  
    x = log(y) / log(2)
    

    Hence, I was just wondering if we could have a logarithmic base system for binary like,

     2^1, 2^0, 2^(log(1/2) / log(2)), 2^(log(1/4) / log(2)), 2^(log(1/8) / log(2)),2^(log(1/16) / log(2)) ........
    

    That might be able to solve the problem, so if you wanted to write something like 32.41 in binary, that would be

    2^5 + 2^(log(0.4) / log(2)) + 2^(log(0.01) / log(2))
    

    Or

    2^5 + 2^(log(0.41) / log(2))
    
    0 讨论(0)
  • 2020-11-21 05:46

    The number 61.0 does indeed have an exact floating-point operation—but that's not true for all integers. If you wrote a loop that added one to both a double-precision floating point number and a 64-bit integer, eventually you'd reach a point where the 64-bit integer perfectly represents a number, but the floating point doesn't—because there aren't enough significant bits.

    It's just much easier to reach the point of approximation on the right side of the decimal point. If you started writing out all the numbers in binary floating point, it'd make more sense.

    Another way of thinking about it is that when you note that 61.0 is perfectly representable in base 10, and shifting the decimal point around doesn't change that, you're performing multiplication by powers of ten (10^1, 10^-1). In floating point, multiplying by powers of two does not affect the precision of the number. Try taking 61.0 and dividing it by three repeatedly for an illustration of how a perfectly precise number can lose its precise representation.

    0 讨论(0)
  • 2020-11-21 05:46

    The high scoring answer above nailed it.

    First you were mixing base 2 and base 10 in your question, then when you put a number on the right side that is not divisible into the base you get problems. Like 1/3 in decimal because 3 doesnt go into a power of 10 or 1/5 in binary which doesnt go into a power of 2.

    Another comment though NEVER use equal with floating point numbers, period. Even if it is an exact representation there are some numbers in some floating point systems that can be accurately represented in more than one way (IEEE is bad about this, it is a horrible floating point spec to start with, so expect headaches). No different here 1/3 is not EQUAL to the number on your calculator 0.3333333, no matter how many 3's there are to the right of the decimal point. It is or can be close enough but is not equal. so you would expect something like 2*1/3 to not equal 2/3 depending on the rounding. Never use equal with floating point.

    0 讨论(0)
  • 2020-11-21 05:46

    As we have been discussing, in floating point arithmetic, the decimal 0.1 cannot be perfectly represented in binary.

    Floating point and integer representations provide grids or lattices for the numbers represented. As arithmetic is done, the results fall off the grid and have to be put back onto the grid by rounding. Example is 1/10 on a binary grid.

    If we use binary coded decimal representation as one gentleman suggested, would we be able to keep numbers on the grid?

    0 讨论(0)
  • 2020-11-21 05:48

    you know integer numbers right? each bit represent 2^n


    2^4=16
    2^3=8
    2^2=4
    2^1=2
    2^0=1

    well its the same for floating point(with some distinctions) but the bits represent 2^-n 2^-1=1/2=0.5
    2^-2=1/(2*2)=0.25
    2^-3=0.125
    2^-4=0.0625

    Floating point binary representation:

    sign  Exponent    Fraction(i think invisible 1 is appended to the fraction )
    B11  B10 B9 B8   B7 B6 B5 B4 B3 B2 B1 B0

    0 讨论(0)
  • 2020-11-21 05:50

    To repeat what I said in my comment to Mr. Skeet: we can represent 1/3, 1/9, 1/27, or any rational in decimal notation. We do it by adding an extra symbol. For example, a line over the digits that repeat in the decimal expansion of the number. What we need to represent decimal numbers as a sequence of binary numbers are 1) a sequence of binary numbers, 2) a radix point, and 3) some other symbol to indicate the repeating part of the sequence.

    Hehner's quote notation is a way of doing this. He uses a quote symbol to represent the repeating part of the sequence. The article: http://www.cs.toronto.edu/~hehner/ratno.pdf and the Wikipedia entry: http://en.wikipedia.org/wiki/Quote_notation.

    There's nothing that says we can't add a symbol to our representation system, so we can represent decimal rationals exactly using binary quote notation, and vice versa.

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