Strange answers to simple maths [kinda solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Btw, i just tried in PHP4 and PHP5:

Code: Select all

<?php
$a = 2.01;
$b = 2;

echo ($a - $b);
?>
It also outputs 0.01
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Syranide - Thanks for the response. Given that I am working with 2.01 - 2, and returning .01 is important, what approach would you take to make it work?

timvw - See, now that is how I originally had it, hence the original question, and it STILL gives me this kooky value. I just dont understand it. I've written stacks of code and never once have I come up against a problem like this. What's worse, it does it incorrectly on the commercial server I linked above.

I'm at a loss. If there is a better method, I'm all ears.
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

first off I can't see how you could possibly get 2.01 - 2 not equal to 0.01 (considering not doing the 100-thingy)... the precision should be more than adequate for that. (sidenote 1/2 isn't necessarily equal to 0.5 due to the float precision)

as I said, eitherway you should commonly round your values if you are expecting "human" values within the range of a million or so (with decimals). this is a common problem and there is no perfect solution to it (except bcmath which has its own costs). computer math is not precise, and therefore rounding is more or less required. e.g. rounding your value down there to 5-6 decimals would likely solve all your problems.

(this applies to everything, take caution with divisions which could result in a float or be used on a float)

however, it is very strange that 2.01 - 2 isn't 0.01... very strange indeed.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Well, I'm all out of options I guess. I'll just use

Code: Select all

round($c, 2);
on it and consider it done.

It's just insane though. I mean, it's purely for currency calculations. I only need precision to go as far as 2 past the decimal point, but ... I don't know.

I dont really want to use bcmath functions, and given that it fails for me on two servers, and it works for everyone else, I'll just have to grin and bear it.

Thanks for the input everyone.
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

Hehe, that was precisly my point btw, you don't have to force it to as low as 2 perhaps, but considering the situation, however getting more than 2 wouldn't be any good either as there is no way to pay that amount.

regarding BCMath, it is very common that is not available (not available by default) so it is a good thing to not use it, especially for purposes like this where there without doubt should be perfectly good without it.

Happy rounding! :P
Post Reply