Predict output for ROUND function

  • Scott, very fine explantion.

    -- Mark D Powell --

  • I'm honestly surprised at the number of people here who were not aware of the approximate nature of IEEE floating-point numbers. That information was in every programming class I took and every programming book I read. (It's even in BOL: http://msdn.microsoft.com/en-us/library/ms187912.aspx -- read the "Using float and real Data" section).

    As a result, the first choice I was immediately able to eliminate was the "correct" one. I have to admit, though: I didn't want to go throught the same level of work as Scott did (great explanation, by the way); nor did I want to "cheat" by copy-paste-execute, so I guessed (correctly, as it turned out).

    This is not a criticism of anyone's knowledge -- it is instead a vote of applause for this question. It brings out a limitation of a commonly-used data type, of which many were not aware. This is an example of a question that both tests and educates.

    Plus, it reinforces my natural instinct to avoid float whenever possible...:-P

  • sknox (1/11/2010)


    I have to admit, though: I didn't want to go throught the same level of work as Scott did (great explanation, by the way); nor did I want to "cheat" by copy-paste-execute, so I guessed (correctly, as it turned out).

    I cheated because I knew about the incomplete correspondence between decimals and binaries, and did not want to guess. But now I have found a great website for decimal-to-IEEE754 conversion: http://www.binaryconvert.com

    This site shows hexadecimal and binary representation of an entered number, as well as the most accurate result of the conversion from this binary back to decimal.

    Here is the result for 165.755 (double precision):

    0x4064B828F5C28F5C

    01000000 01100100 10111000 00101000

    11110101 11000010 10001111 01011100

    The result of backward conversion to decimal: 1.65754999999999995452526491135E2

    The same for 165.555:

    0x4064B1C28F5C28F6

    01000000 01100100 10110001 11000010

    10001111 01011100 00101000 11110110

    1.65555000000000006821210263297E2

    With these values the correct answer becomes obvious 🙂

  • Now I have found a great website for decimal-to-IEEE754 conversion: http://www.binaryconvert.com

    This site shows hexadecimal and binary representation of an entered number, as well as the most accurate result of the conversion from this binary back to decimal.

    Great resource - Thanks! 😎

    Kelsey Thornton
    MBCS CITP

Viewing 4 posts - 16 through 18 (of 18 total)

You must be logged in to reply to this topic. Login to reply