From owner-freebsd-questions@FreeBSD.ORG Thu Apr 13 14:24:15 2006 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 68B7016A403 for ; Thu, 13 Apr 2006 14:24:15 +0000 (UTC) (envelope-from wmoran@collaborativefusion.com) Received: from mx00.pub.collaborativefusion.com (mx00.pub.collaborativefusion.com [206.210.89.199]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9FF4243D45 for ; Thu, 13 Apr 2006 14:24:05 +0000 (GMT) (envelope-from wmoran@collaborativefusion.com) Received: from localhost (monrovll-cuda1-24-53-251-44.pittpa.adelphia.net [24.53.251.44]) (AUTH: LOGIN wmoran, TLS: TLSv1/SSLv3,256bits,AES256-SHA) by wingspan with esmtp; Thu, 13 Apr 2006 10:24:04 -0400 id 00056420.443E5F04.00016013 Date: Thu, 13 Apr 2006 10:24:03 -0400 From: Bill Moran To: Krzysztof Nakielski Message-Id: <20060413102403.1c653d51.wmoran@collaborativefusion.com> In-Reply-To: <20060413092226.GA10039@nakiel.dyndns.org> References: <20060412100917.GA1406@nakiel.dyndns.org> <20060412093701.0309d4a6.wmoran@collaborativefusion.com> <20060413092226.GA10039@nakiel.dyndns.org> Organization: Collaborative Fusion X-Mailer: Sylpheed version 1.0.6 (GTK+ 1.2.10; i386-portbld-freebsd5.4) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: freebsd-questions@freebsd.org Subject: Re: round() problem X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Apr 2006 14:24:15 -0000 Krzysztof Nakielski 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 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