My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum


Reply
 
LinkBack Thread Tools Display Modes
October 18th, 2012, 07:30 AM   #1
 
Joined: Oct 2012

Posts: 2
Thanks: 0

PHP Maths error or am I losing the plot?

Just did the most inoxuous of PHP lines:

print 100.82031-100.82;

Result?

0.00031000000001313

What is going on here?
Did I miss this class at school?
Can someone else please try this and tell me I'm not the only one!!
kg123 is offline  
 
October 18th, 2012, 07:42 AM   #2
 
Joined: Oct 2012

Posts: 2
Thanks: 0

Re: PHP Maths error or am I losing the plot?

I worked backwards from the above and still got the same result.
i.e. I did:

print 100.82031000000001313-100.82;

and also get 0.00031000000001313

What on earth is going on???
How can 100.82031-100.82 and 100.82031000000001313-100.82 equal the same thing??
Is this a problem with the PHP Maths or something more sinister
kg123 is offline  
October 18th, 2012, 09:56 AM   #3
Global Moderator
 
CRGreathouse's Avatar
 
Joined: Nov 2006
From: UTC -5

Posts: 13,911
Thanks: 388

Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic
Re: PHP Maths error or am I losing the plot?

http://en.wikipedia.org/wiki/IEEE_floating_point
CRGreathouse is offline  
January 3rd, 2013, 12:19 PM   #4
 
Joined: Dec 2012

Posts: 6
Thanks: 0

Re: PHP Maths error or am I losing the plot?

It's a rounding error, kg123. It happens because the machine is using binary to represent the number, and for this particular number there doesn't exist a way to store it exactly using the floating point representation.

There's no way to prevent it; you just need to roll with the punches. Instead of checking if two numbers are equal, consider checking if they're "close enough" to each other.
Joeyjojo is offline  
Reply

  My Math Forum > Science Forums > Computer Science

Tags
error, losing, maths, php, plot



Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Am I losing my marbles? mathkid Calculus 2 November 18th, 2012 09:54 PM
possible error/relative error jkh1919 Calculus 5 August 4th, 2012 06:30 PM
Relative error Percenttage error esther Calculus 1 October 25th, 2011 03:57 AM
Maths dont know what a number is-maths ends in meaningless bas Number Theory 3 March 22nd, 2011 10:26 AM
*** Solving an Equation - Losing Sleep Over IT!!! *** etes Algebra 3 October 20th, 2007 07:24 PM





Copyright © 2014 My Math Forum. All rights reserved.