round() problem
Bill Moran
wmoran at collaborativefusion.com
Thu Apr 13 14:24:15 UTC 2006
Krzysztof Nakielski <nakiel at nakiel.net> wrote:
> On Wed, Apr 12, 2006 at 09:37:01AM -0400, Bill Moran wrote:
> > On Wed, 12 Apr 2006 12:09:17 +0200
> > Krzysztof Nakielski <nakiel at nakiel.net> wrote:
> >
> > > Hi,
> > >
> > > I am having problem with round() function in php, python, mysql. I am
> > > not sure if thats FreeBSD issue. I receive the same results on 4.11, 5.4
> > > and 6.0.
> > >
> > > php (4.4.1, 5.1.2):
> > > %php -r 'print round(8.075, 2) ."\n";'
> > > 8.07
> >
> > Have you compared these results to other POSIX systems?
>
> On RHEL 3 only PHP returns good result. In python and mysql there is the
> same issue.
>
> > The problem is in the way that real numbers are implemented. If you
> > do some searches, you'll find many, many discussions of this. Simple
> > fact is that the behaviour under these circumstances is not what
> > you think it is. This kind of thing is the reason that most languages
> > have high-precision floating point libraries available.
> >
>
> I will search for this. But first thing is to give up with python, php,
> mysql in billing software or write own functions otherwise you can lose
> money.
That's not a good solution.
Real numbers are not designed to be accurate in the way that you define
accurate. If you want your application to behave in a manner that you
understand, then you need to understand how those numbers are handled
by the language. A few things to research:
http://fixedpoint.sourceforge.net/
http://dev.mysql.com/doc/refman/5.0/en/precision-math.html
http://us2.php.net/manual/en/ref.bc.php
http://en.wikipedia.org/wiki/Fixed-point_arithmetic
--
Bill Moran
More information about the freebsd-questions
mailing list