473,326 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

php float problem, 1-1 = 2.22044604925E-16

bonski
53
Good day to all,

Recently I have encountered a problem regarding to float numbers. I have this query that gets the values from database and insert it to an array. These values are decimal (float) numbers. Then basing on these values I have a given value inputted from a textfield, and this given value decrements(subtract with the values from array) until its last remainder. Here's how it looks like:

Expand|Select|Wrap|Line Numbers
  1. //assume that these values are from query to array
  2. $arr = array(0.2, 0.2, 1, 1, 1, 1, 1 ,1);
  3.  
  4. //now the given from textfield which creates error
  5. $given = (float)2.4;
  6.  
  7. //this loop gets how many values should be needed
  8. //I dont want to use the whole array values, so I need to check each value that is equal or near to the given value
  9.  
  10. $check_sum = 0;
  11. $ctr = 0;
  12. for ($a=0; $a<sizeof($arr); $a++) {
  13.  $check_sum = $check_sum + $arr[$a];
  14.  if ($check_sum<$given) {
  15.    $ctr++;
  16.    //if sum is less than the given continue on the loop
  17.  } else if ($check_sum==$given) {
  18.    $ctr++; 
  19.    break;
  20.    // if the sum is equal then stop then loop
  21.  } else {
  22.    $ctr++;
  23.    break;
  24.    //as soon as the sum is already more than the given value then stop
  25.  }
  26.  
  27.  //now i have figure out the length of my array to be use
  28.  for ($i=0; $i<$ctr; $i++) {
  29.   if ($given>$arr[$i]) {
  30.  
  31.     $answer = $given - $arr[$i];
  32.     echo $given.' - '.$arr[$i].' = '.$answer.'<br />';
  33.     //decrements 
  34.     $given = $given - $arr[$i];
  35.   } else if ($given == $arr[$i]) {
  36.     // if both equal, the loop ends, should display $answer = 0
  37.     echo $given.' - '.$arr[$i].' = '.$answer.'<br />';
  38.   } else {
  39.     //if given is less than the value from array, get the excess value
  40.     $excess = $arr[$i] - $given
  41.     echo $arr[$i].' - '.$given.' = '.$excess.'<br />';
  42.   }
  43. }
  44.  
  45. //so the output should be
  46. //2.4-0.2 = 2.2
  47. //2.2-0.2 = 2
  48. //2-1 = 1
  49. //1-1 = 0
  50.  
  51. //but instead, it outputs like this
  52. //2.4-0.2 = 2.2
  53. //2.2-0.2 = 2
  54. //2-1 = 1
  55. //1-1 = 2.22044604925E-16
  56.  
  57.  
So now, I know the script is ok.. but why 2.22044604925E-16 instead of zero? I hope someone can give me a clarification so I can revise to this one and prevent the float(2.22044604925E-16) to come out, but instead the 0.

Thanks. =)
Mar 4 '10 #1
1 3170
bonski
53
Hi all, nevermind this.. problem solve, instead of this, i multiple each values with 1000.. coz these values are units in kilograms.. then i converted it into grams.. coz they said 1-1 in float will result to some bug.

thanks
Mar 4 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Neal D. Becker | last post by:
Is there a way in python to access properties of floats? I need something equiv to C DBL_EPSILON defined in <float.h>.
13
by: TheKeith | last post by:
Is it just me or does opera and ie not render the float style properly? IE does a sucky job at it, but opera makes a total mess; Mozilla/Firefox renders it correctly however. I'm just trying to be...
5
by: Chris Beall | last post by:
See http://pages.prodigy.net/chris_beall/BeallSprings/WC.Deed%20GG.116-17.html using the CSS at http://pages.prodigy.net/chris_beall/BeallSprings/BSstyle.css The page is a transcript of a...
2
by: Dr. Richard E. Hawkins | last post by:
I've googled around, and asked everyone I know, but I still can't find any reference to anyone else having faced this particular IE bug with floats. I've put a page at...
14
by: Glen Able | last post by:
Should it be possible to create a custom class, 'Float', which would behave as a drop-in replacement for the builtin float type? As mentioned in another thread, I once tried this in rather a...
8
by: Jonathan Fielder | last post by:
Hi, I have a 32 bit integer value and I wish to find the single precision floating point value that is closest to but less than or equal to the integer. I also have a similar case where I need...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
6
by: James Thurley | last post by:
According to the docs, floats are 32 bit and doubles are 64 bit. So using floats should be faster than using doubles on a 32 bit processor, and my tests confirm this. However, most of the Math...
20
by: ehabaziz2001 | last post by:
That program does not yield and respond correctly espcially for the pointers (*f),(*i) in print_divide_meter_into(&meter,&yds,&ft,&ins); /*--------------pnt02own.c------------ ---1 inch = 2.51...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.