irata73 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, can someone explain me this strange behaviour and give me a solution.
perl -e 'printf "%d\n", 75.600 * 100' 7559

Replies are listed 'Best First'.
Re: Multiply with 100 in perl
by choroba (Cardinal) on Nov 11, 2015 at 17:11 UTC

      Just to add a little information, the problem isn't multiplying by 100, it's the 75.6

      $ perl -e 'printf "%.20f\n", 75.600' 75.59999999999999431566
Re: Multiply with 100 in perl
by BrowserUk (Patriarch) on Nov 11, 2015 at 17:10 UTC

    See if Re: Re: Re: Bug? 1+1 != 2 helps your understanding.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Multiply with 100 in perl
by MidLifeXis (Monsignor) on Nov 11, 2015 at 21:04 UTC

    It has to do with how the computer represents floating point numbers. The computer is representing the data as a summation of various powers of 2. Since .6 cannot be represented as a summation of powers of 2, it is only a close approximation. When you truncate the value using the "%d" format specification, it gives the results you show (as illustrated by others on this thread).

    The reason that perl6 (mentioned elsewhere) gets it right (IIRC), is because if it cannot exactly represent the value, it will store it as a ratio (in this case, 756/10) and use integer math (or at least exact precision math) on the values. Since I have not started to learn Perl6, this is from memory. Use at your own risk.

    See also: what every programmer should know about floating point. (update: choroba beat me to it)

    --MidLifeXis

      Just an example to confirm and illustrate the difference between Perl 5 and Perl 6:
      C:\Users\Laurent>perl -e "printf qq{%.20f \n}, 75.600;" 75.59999999999999400000 C:\Users\Laurent>perl6 -e "printf qq{%.20f \n}, 75.600;" 75.60000000000000000000
Re: Multiply with 100 in perl
by BillKSmith (Monsignor) on Nov 12, 2015 at 04:57 UTC
    choroba has shown that the "%.0f" format specifier solves the problem. The printf formats are "inherited" from C. The "%d" format in intended for printing C's INT type. It is almost never appropriate in perl.
    Bill
Re: Multiply with 100 in perl
by hdb (Monsignor) on Nov 11, 2015 at 20:10 UTC

    This is probably not what you are looking for and limited to this very special case...

    my $x = $ARGV[0].'00'; $x=~s/\.(..)/$1./; printf "%d\n", $x;
Re: Multiply with 100 in perl
by ctilmes (Vicar) on Nov 11, 2015 at 20:47 UTC
    This won't fix your immediate problem (others have pointed out why it happens), but to give you hope for the future, this is one of many things perl6 gets right. It does math right.(tm)
    $ perl6 -e 'printf "%d\n", 75.600 * 100' 7560

      Floating point arithmetic has limited accuracy try bignum library.

      perl -Mbignum -e 'printf"%.20F\n", 75.600 * 100;'

        Bignum is good, but printf still converts to float.

        $ perl -Mbignum -e 'printf "%.32g\n", 75.600 * 10**30' 75599999999999995986368094273536 $ perl -Mbignum -le 'print 75.600 * 10**30' 75600000000000000000000000000000
Re: Multiply with 100 in perl
by LanX (Saint) on Nov 12, 2015 at 10:06 UTC