in reply to rounding to nearest integer

IIRC there is a rounding error in s?printf() whereby certain patterns of numbers will round up or down incorrectly (maybe it was system dependant too?). I cannot find the proof/thread/post about it. Anyone know about this who can speak to it?

Replies are listed 'Best First'.
Re: Re: rounding to nearest integer
by hv (Prior) on May 11, 2003 at 11:39 UTC

    The floating-point parts of s?printf() are not implemented directly in perl itself, but handed off to the system library's sprintf() function. As a result, you are at the mercy of any bugs (in rounding or anything else) in the particular version of the system library you (or your customer) are using.

    This is the only part of s?printf() handed off in that way in modern perls. In general, I tend to avoid use of floating-point numbers as far as possible in any application for which precise results are important - often you can model the values you need with integers or rationals, and the Math::Big* family of modules can also help out.

    Hugo