Re: Math weirdness with doubles...
| From: | D. Wokan | Date: | Fri, 27 Feb 2004 22:20:40 +0000 |
| Subject: | Re: Math weirdness with doubles... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-179034@lists.php.net to get a copy of this message | ||
They are not the same amount. They each got a different rounding error. Double values only display a small number of decimal places (relatively speaking). If you look at the difference you're getting, it's 0.0000000000000142108547152 (I may be off by a zero or two), so given you're working with a currency, I'd say you can safely round to about 4 or 8 decimal places to get a reasonably accurate result. I typically use 4 since most money types I've seen only go out 4 decimal places to begin with.
--
D. Wokan
(Sorry for the double reply, Jon. Didn't realise this list doesn't set
the reply-to until it was too late.)
jon roig wrote:
> Ok... It's Friday and maybe my brain is dead, but I'm having a weird
> problem with some basic math.
>
> Here's a little snippet of the code I'm working with:
>
> ---------------------------
> echo "<p>Current:$currentAmount:".gettype($currentAmount)." -
> Paid:$paidAmount:".gettype($paidAmount)."</p>";
>
> $currentAmount = $currentAmount-$paidAmount;
> echo "<p>Final Current: $currentAmount";
> -----------------
>
> Straightforward... Yeah?
>
> Here's a sample of the output:
> Current:97.6:double - Paid:97.6:double
> Final Current: 1.42108547152E-014
>
> Now if both $currentAmount and $paidAmount are doubles, subtracting them
> should yield a zero, shouldn't it?
>
> -- jon
>
> -------------------
> jon roig
> web developer
> email: jon@trafficdesigns.com
> phone: 888.230.7557
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.593 / Virus Database: 376 - Release Date: 2/20/2004
>
>