Forum Moderators: coopster

Message Too Old, No Replies

PHP Maths

Doesn't like figures adding up to 0.00

         

barns101

5:56 pm on Mar 20, 2006 (gmt 0)

10+ Year Member



I have a script for a racing tipster website that calculates a rolling profit/loss table based on results stored in a database.

It's been working fine for months now but a recent result has thrown up a bug and I just can't understand what the problem is. The overall profit was £100 in the red (-£100) on 18th March and then a win of £100 on 19th March should show a new overall profit of £0.00. However the figure -5.6843418860808E-14 is shown instead!

Here is the relevant PHP code used (which operates in a loop):


...
if($row["result"]=='Won')
{
$profit = 100;
}
if($row["result"]=='Lost')
{
$profit = ($row["odds"]-1)*(-100);
}
if($row["result"]=='N/R') // Non-Runner (neither a win nor a loss)
{
$profit = '-';
}
if($row["result"]=='Won')
{
$overall_profit = $overall_profit + $profit;
}
if($row["result"]=='Lost')
{
$overall_profit = $overall_profit + $profit;
}
echo "<tr>
<td>$date</td>
<td>$selection</td>
<td>$row[odds]</td>
<td>$row[result]</td>
<td>$profit</td>
<td>$overall_profit</td>
</tr>";
...

Altering the previous results so that the overall profit does not hit exactly £0.00 eliminates the funny long number, so I guess it must be something in the maths to do with ending up with £0.00

Anyone got any ideas?

coopster

12:10 am on Mar 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I guess I would start by dumping the variables out to see what they contain as they come out of the database.
print '<pre>'; 
var_dump($overall_profit);
var_dump($profit);
exit('</pre>');
$overall_profit = $overall_profit + $profit;

inbound

12:34 am on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is due to the precision method being chosen by PHP. A simple fix will be to do this:

$newvariable = int($variable*100)/100

That should do it, or don't bother dividing by 100 and just use the (pence) result with a little formatting.

barns101

10:34 am on Mar 21, 2006 (gmt 0)

10+ Year Member



Thank you both for replying. :)

Inbound's solution results in the following error: "Fatal error: Call to undefined function: int()"

I tried the following but it did not work, either:


if($row["result"]=='Won')
{
$overall_profit = int $overall_profit + $profit;
}

I have also searched for "PHP precision method" but come up blank.

Any other ideas?

** EDIT **
I have also searched the PHP documentation but can't find anyone experiencing similar problems

omoutop

10:50 am on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Int is used as (int)$variable or (integer)$variable.

You could also try the number_format() function
[gr2.php.net...]

barns101

12:27 pm on Mar 21, 2006 (gmt 0)

10+ Year Member



Thanks omoutop, number_format() sorted it.

So why does PHP behave unexpectedly when adding and subtracting what I thought were simple whole numbers? I guess it will be something to do with the fact that unless I specify the variable as a number using number_format(), PHP treats it like a string or something like that?

inbound

7:15 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops,

The int() syntax is used in excell rather than PHP, different syntax required in PHP.

The strange result is due to floating point numbers being used when not appropriate. The returned number that looked so strange is in fact a tiny anomoly but it looks very strange when printed with no formatting. The lesson from this is to anticipate such strange results and code accordingly, either using the correct variable type or using formatting.

coopster

8:04 pm on Mar 21, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The returned variables from a result set are going to be string. Then, depending on which operations you are using there may be some type conversion. There is a small example and explanation for some of this in this thread where a member was having Trouble adding numbers with 12 decimal places [webmasterworld.com].

barns101

1:04 am on Mar 22, 2006 (gmt 0)

10+ Year Member



Thanks for the explanations. :)

Although I have been coding PHP for a few years and have used number_format() before when it was obvious that the decimal places would need to be specified, I never knew that seemingly whole numbers would need to be specially formatted. Oh well, you code and learn! ;) Thanks again.