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

-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to