• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

What the fark? PHP math does not work?

Al Neri

Diamond Member
I am writing a quick script that will take baseball statistics for innings pitched and convert it to usable mathematical form.

"What was that" you may say.

Well, if a pitcher pitches 80 complete innings and then gets 1 out in an inning and gets pulled it is written as 80.1 - whereas mathematically he has really pitched 80 1/3

.1 is notation for 1 out
.2 for two outs


so... i wrote the following, which should convert IP from 10.1 to 10.33333333, but it does not work! What on earth am I doing wrong?


Thanks!

AlNeri



<?
$IP=10.1;
$IPwhole=intval($IP);
$IPremain=$IP-$IPwhole;
if ($IPremain==0.1)
{
$convertedIP=$IPwhole+(1/3);
}
elseif($IPremain==0.2)
{
$convertedIP=$IPwhole+(2/3);
}
else {$convertedIP=$IP;}

echo "$IP would be rewritten as $convertedIP";
 
^ right, $IPRemain may be 0.10000000000000002 or 0.09999999999999997

Also, in C/C++:
$convertedIP=$IPwhole+(2/3) ;

is int + (int/int), and 2/3 = 0 for ints. You'd need to have 2.0 / 3.0 or cast one to float / double.
 
"it does not work" is not a possible result. It does something, just maybe not what you expected. So what DOES it do; What output do you get from the echo line?

It might help you to check your variables at each step of the process:

$IP=10.1;
echo $IP;
$IPwhole=intval($IP);
echo $IPwhole;
$IPremain=$IP-$IPwhole;
echo $IPremain;
if ($IPremain==0.1)
{
$convertedIP=$IPwhole+(1/3);
}
elseif($IPremain==0.2)
{
$convertedIP=$IPwhole+(2/3);
}
else {$convertedIP=$IP;}
echo $convertedIP;

echo "$IP would be rewritten as $convertedIP";
 
Never compare floating point numbers without reading the docs for the given programming language. Computers are notoriously bad at floating point math as odd as that sounds. I won't go into detail about why but it would be an excellent lesson to read up on.

In your case the solution is very easy, simply compare strings.

<?php
$IP=10.1;
$IPwhole=intval($IP);
$IPremain=(string)($IP-$IPwhole);
if (strcmp($IPremain, '0.1') == 0)
{
$convertedIP=$IPwhole+(1/3);
}
elseif(strcmp($IPremain,'0.2') == 0)
{
$convertedIP=$IPwhole+(2/3);
}
else {
$convertedIP=$IP;
}

echo "$IP would be rewritten as $convertedIP";
?>
 
Originally posted by: Al Neri
I am writing a quick script that will take baseball statistics for innings pitched and convert it to usable mathematical form.

"What was that" you may say.

Well, if a pitcher pitches 80 complete innings and then gets 1 out in an inning and gets pulled it is written as 80.1 - whereas mathematically he has really pitched 80 1/3

.1 is notation for 1 out
.2 for two outs


so... i wrote the following, which should convert IP from 10.1 to 10.33333333, but it does not work! What on earth am I doing wrong?


Thanks!

AlNeri



<?
$IP=10.1;
$IPwhole=intval($IP);
$IPremain=$IP-$IPwhole;
if ($IPremain==0.1)
{
$convertedIP=$IPwhole+(1/3);
}
elseif($IPremain==0.2)
{
$convertedIP=$IPwhole+(2/3);
}
else {$convertedIP=$IP;}

echo "$IP would be rewritten as $convertedIP";

$innings = floor($IP);
$outs = ($IP - $innings) * 10;
 
> In your case the solution is very easy, simply compare strings.

To work reliably that might require using sprintf to set the number of decimal digits, since converting 0.09999999... won't automatically do any rounding to "0.1" in many languages (I haven't tried it in PHP).
 
Originally posted by: DaveSimmons
> In your case the solution is very easy, simply compare strings.

To work reliably that might require using sprintf to set the number of decimal digits, since converting 0.09999999... won't automatically do any rounding to "0.1" in many languages (I haven't tried it in PHP).

Not in this case because the decimal place isn't an actual decimal. The value according to the OP will only ever be .1 or .2.
 
Back
Top