By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

Strange math result... 1-1= -1 ???

P: n/a
See something weird on this result?

632.35 - 632.35 = -1.13686837722E-013

If anyone has seen something similar, please let me know!

this a part of the code:

$total_due = abs( $order_total ) - abs( $total_paid );
echo( $order_total . " - " . $total_paid . " = " . $total_due );

I've added the abs() in order to be sure that I'm dealing with
numbers, I've also used is_numeric() and confirmed it.
The echo() is showing the result I pasted up there.
any ideas??
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
I have never seen that before and no clue as how to fix it.

But for a work around, use...

$output = round($input, 2); // outputs 12.36 from 12.3556

Hope this helps.

-William
"Mark" <ma*****@gmail.com> wrote in message
news:5a**************************@posting.google.c om...
See something weird on this result?

632.35 - 632.35 = -1.13686837722E-013

If anyone has seen something similar, please let me know!

this a part of the code:

$total_due = abs( $order_total ) - abs( $total_paid );
echo( $order_total . " - " . $total_paid . " = " . $total_due );

I've added the abs() in order to be sure that I'm dealing with
numbers, I've also used is_numeric() and confirmed it.
The echo() is showing the result I pasted up there.
any ideas??

Jul 17 '05 #2

P: n/a
Mark wrote:
See something weird on this result?

632.35 - 632.35 = -1.13686837722E-013
The first 632.35 is in fact something like 632.349999999937038
and the second is ....... 632.350000000203192
If anyone has seen something similar, please let me know!

this a part of the code:

$total_due = abs( $order_total ) - abs( $total_paid );
echo( $order_total . " - " . $total_paid . " = " . $total_due );
echo number_format($order_total, 14);
echo number_format($total_paid, 14);
I've added the abs() in order to be sure that I'm dealing with
numbers, I've also used is_numeric() and confirmed it.
The echo() is showing the result I pasted up there.
any ideas??


As another poster said use round().

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #3

P: n/a
On 30 Jun 2004 13:31:26 -0700, ma*****@gmail.com (Mark) wrote:
See something weird on this result?

632.35 - 632.35 = -1.13686837722E-013

If anyone has seen something similar, please let me know!

this a part of the code:

$total_due = abs( $order_total ) - abs( $total_paid );
echo( $order_total . " - " . $total_paid . " = " . $total_due );

I've added the abs() in order to be sure that I'm dealing with
numbers, I've also used is_numeric() and confirmed it.
The echo() is showing the result I pasted up there.

any ideas??


This is normal behaviour from computers which use the IEEE floating-point
format (i.e. pretty much all of them). Many decimal numbers have no exact
representation in this format, however they will differ only by a very small
value. So when you start doing calculations, small errors build up.

It looks like you are working with monetary values. You should avoid using
floating point numbers for money, and convert everything to integer values of
the smallest currency unit or fraction thereof you want to deal with, for
instance cents or pence. Then only convert to a floating point value for
display. This way all calculations remain exact.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #4

P: n/a
You subject line isn't correct (just for the record).

-1.13686837722E-013 isn't -1. It's a number that is very close to being
zero. It happens to be negative, meaning it's slightly less than zero. It
*certainly* isn't anywhere close to -1.

So within the normal limits of precision of floating point arithmetic, you
have "x - x = 0" which is correct. As others have pointed out, the computer
is doing its job exactly right.

--
--------------------
My e-mail address doesn't have a 2 in it.

"Mark" <ma*****@gmail.com> wrote in message
news:5a**************************@posting.google.c om...
See something weird on this result?

632.35 - 632.35 = -1.13686837722E-013

If anyone has seen something similar, please let me know!

this a part of the code:

$total_due = abs( $order_total ) - abs( $total_paid );
echo( $order_total . " - " . $total_paid . " = " . $total_due );

I've added the abs() in order to be sure that I'm dealing with
numbers, I've also used is_numeric() and confirmed it.
The echo() is showing the result I pasted up there.
any ideas??

Jul 17 '05 #5

P: n/a
ma*****@gmail.com (Mark) wrote in message
news:<5a**************************@posting.google. com>...

See something weird on this result?

632.35 - 632.35 = -1.13686837722E-013
There's ABSOLUTELY NOTHING strange about it. This is how floating-
point numbers work.
this a part of the code:

$total_due = abs( $order_total ) - abs( $total_paid );
echo( $order_total . " - " . $total_paid . " = " . $total_due ); any ideas??


Try this:

$total_due = $order_total - $total_paid;
echo $order_total , " - " , $total_paid , " = " ,
number_format($total_due, 2);

Cheers,
NC
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.