473,725 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
5 1993
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.goo gle.com...
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
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.34999999993 7038
and the second is ....... 632.35000000020 3192
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
On 30 Jun 2004 13:31:26 -0700, ma*****@gmail.c om (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
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.goo gle.com...
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
ma*****@gmail.c om (Mark) wrote in message
news:<5a******* *************** ****@posting.go ogle.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
6347
by: Allcomp | last post by:
Hello, I have seen something really strange in VB6 If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive 6? Is this a bug or something really "normal". I can see that if I do ? int ((5 * 1.2 + 0.0000000000000003)) I receive 6. If I add something smaller, I have 5 as a result What is strange is that If I do a ? 5*1.2, I receive 6 so (5 * 1.2)
24
1721
by: David | last post by:
hello. when doing the simple following computation, the value put into the variable numMinusOne is NOT the same as what the computation is showed to be in the Watch window!! here is the code: Dim xSng As Single = 6547.972 Dim yInt As Integer = 8000 Dim num As Integer = CInt(Math.Floor(xSng * yInt)) Dim numMinusOne As Integer = CInt(Math.Floor(xSng * yInt) - 1)
1
2738
by: limelight | last post by:
I have discovered a math error in the .NET framework's Log function. It returns incorrect results for varying powers of 2 that depend on whether the program is run from within the IDE or from the command line. The amount by which the calculation is off is very small; even though the double data type holds the errant value, it seems to round off when printed (via ToString()) and shows the correct one. The problem is that the errant value is...
17
3625
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels deep. In that case you need much more ram than a PC has to store functions calculated in loops so that you do not have to recalculate every time you cycle through the nest of loops. Using a HD for storage to extend ram is much too slow for many...
0
1364
by: han zhiyang | last post by:
I've just studied the "how to" web service and the async pattern in donnet.I make a test with these knowledges,but I got a strange result. Here is my test. 1.Write a simple "Add" service named MathWS( the class name),and put it in a ..asmx file named math.asmx in the "D:/winnt/interpub/wwwroot/mathws" directory. "mathws" is my new-built directory. 2.run the http://localhost/mathws/math.asmx,find it works well,and run run the...
2
1363
by: Webdiyer | last post by:
Hi, We all know that the return value of Math.Log(8,2) is 3,but how about (int)Math.Log(8,2)? On my machine,the return value of (int)Math.Log(8,2) is strange enough! it's not 3 but 2 ! I've tested other values such as (int)Math.Log(16,2),(int)Math.Log(4,2),(int)Math.Log(32,2)...et, they all return the same value as their counterpart Math.Log() methods,only (int)Math.Log(8,2) is incorrect,is this a .net bug ? I've also tested...
5
5363
by: Vedran Furańć | last post by:
I think that this results must be the same: In : math.atan2(-0.0,-1) Out: -3.1415926535897931 In : math.atan2(-0,-1) Out: 3.1415926535897931 In : -0 == -0.0 Out: True
10
1750
by: Bernard Liang | last post by:
Consider the following excerpt below, with the included relevant declarations. Firstly, the lines marked with **, ***, **** at the beginning are not supposed to have those stars at the beginning; they are only used to direct attention. Suppose, first of all, that the lines with the **** are not present. p_num is somehow getting changed from its previous value to 0 by the sscanf command (***) that doesn't even involve p_num. I cannot
17
3917
by: Lenni | last post by:
Hi, I'm currently writing a web application for bicycle wheel builders that calculate the spoke length for all sorts of variations of hubs and rims. I have translated the formula from an Excel spreadsheet and in JavaScript it looks like this: var sll=Math.sqrt(Math.pow(((fdl/2*Math.sin((2*Math.PI*cross)))/
0
8872
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8747
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9392
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6694
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Duprť who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5997
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4773
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3211
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.