
Computer Science Computer Science Forum 
 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.82031100.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!! 
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.82031000000001313100.82; and also get 0.00031000000001313 What on earth is going on??? How can 100.82031100.82 and 100.82031000000001313100.82 equal the same thing?? Is this a problem with the PHP Maths or something more sinister 
October 18th, 2012, 09:56 AM  #3 
Global Moderator Joined: Nov 2006 From: UTC 5 Posts: 14,203 Thanks: 487 Math Focus: Number theory, computational mathematics, combinatorics, FOM, symbolic logic  Re: PHP Maths error or am I losing the plot? 
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. 

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 ismaths 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 