in reply to Reading wrong value from excel sheet

what every programmer should know about floating point

IIRC, unless the denominator of your fraction is able to be represented as sums of 1/2**N (mlx: clarified) (1/2 + 1/8, for example), you end up with an approximation of the value. Many times, this tends to be "close enough". If dealing with money, for example, you will often want to use some other representation than floating point under the hood (an integer representation of millicents or some such comes to mind).

An example REPL - perl -ne 'printf("%.60f\n", $_)'.

Repost of Re: Unexpected results when storing a Math::Round nearest value in MongoDB

Update: See My floating point comparison does not work. Why ? for more information

Update 2: Clarified after apparent confusion

--MidLifeXis

Replies are listed 'Best First'.
Re^2: Reading wrong value from excel sheet
by locked_user sundialsvc4 (Abbot) on May 21, 2013 at 19:59 UTC

    Hmmm... didn’t I once hear someone saying that “1/10” could not be exactly represented in base-two, just as “1/3” (0.333....) cannot be exactly represented in base-ten?

      I am not sure I understand your point. I don't think that I said that 1/10 could be represented in base-two.

      --MidLifeXis

        Of course, 1/2 + 1/8 = (1+1)/(2+8) = 2/10, not 1/10!
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ