http://qs321.pair.com?node_id=11124401


in reply to finding the correct integer

The "%f" format does rounding to the specified precision. In this case use:
print sprintf("%4.0f\n", 0.6505 / 0.0001);

OUTPUT:

6505
Bill

Replies are listed 'Best First'.
Re^2: finding the correct integer
by LanX (Saint) on Nov 30, 2020 at 18:20 UTC
    Yes, it does in the case the OP provided.

    But it's important to keep in mind that accumulated rounding errors for longer calculations won't be magically compensated.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      LanX and haukex have the "right" answer here. Sure, various things can be done to coax "the right answer" out of "this example," but the underlying mathematical issues are still there and can't be evaded.

        you continue to add nothing of value to any discussion. Why do you bother?

Re^2: finding the correct integer
by LanX (Saint) on Nov 30, 2020 at 22:10 UTC
    another nitpick, the OP asked for int not rounding

    please compare:

    DB<6> print int 0.65049 / 0.0001; 6504 DB<7> printf("%4.0f\n", 0.65049 / 0.0001); 6505 DB<8>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery