Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:11511 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 9815 invoked by uid 1010); 23 Jul 2004 14:45:07 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 9791 invoked by uid 1007); 23 Jul 2004 14:45:07 -0000 Message-ID: <20040723144507.9782.qmail@pb1.pair.com> To: internals@lists.php.net Date: Fri, 23 Jul 2004 21:02:28 +0300 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031007 X-Accept-Language: en-us, en MIME-Version: 1.0 References: <20040723080319.74978.qmail@pb1.pair.com> <4100DB8A.2000003@cschneid.com> <20040723105532.30754.qmail@pb1.pair.com> <4100F756.3080000@cschneid.com> In-Reply-To: <4100F756.3080000@cschneid.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 80.221.251.197 Subject: Re: fp, rounding, decimal arithmetic - definitive account? From: george@ishop.co.uk (George Whiffen) Christian, You are paranoid! You don't need integers AND bc arithmetic! One or the other will do fine. C doubles are exact for integers a million times bigger than your requirements (i.e. over 1e15). That means that all integer calculations are also exact, even if php does store them as floats. If you really want non-integer results to be truncated (as with bc and zero scale), rather than approximated which is what the fp will do, then floor()/ceil() etc. will also work fine. Alternatively, you could have left your data with "cents" i.e. 2 decimal places and just used bc arithmetic and a scale of two or whatever you need for your sub-cent calculations. But if, as you say, you are really worried about a solution that works whatever your future customers do, then you definitely want php decimal arithmetic sorted out. Then you can handle multi-currencies without code changes. You don't have to care "where the decimal point goes" as long as there's no more than 15 digits altogether. George Christian Schneider wrote: > Hey George, > > My point is: I'm not against improving accuracy of floating point > arithmetic in PHP (although it could lead to some weird compatibility > problems with older version) but I wouldn't advise people who really, > really have to rely on their numbers to be correct to use floats anyway. > > > George Whiffen wrote: > > Exactly my point. When coding php you need floating point you can > > trust or it is useless. As I said, bc is probably the best currently > > You can never trust floating point if you need both big numbers and > precision at the same time. Floating point is always a trade off between > the two. > > > If I'm wrong, and it's really only bc that can be trusted to add 0.1 > > and 0.1, then shouldn't we be using that internally, rather than > > pretending to have a useable fp decimal arithmetic? > > Floating point (even the current PHP implementation) is fine if you're > happy enough with approximations which is the case 99% of the time. > > > P.S. I'm interested that you have money numbers so big that 15 digits > > are not enough. It can't be gross world income which still isn't up > > I store cent values as integer strings and I possibly have to deal with > values bigger than 214 million bucks (where PHP starts to convert ints > to float) so I decided to go for BC Math. I didn't even care to check > what the range/precision limits of floats are as I wanted something > absolutely predictable, no matter what numbers are put into the system > by our customers. > > Just because you're paranoid doesn't mean they're not after you :-) > - Chris