What the fark? PHP math does not work?

Al Neri

Diamond Member
Jan 12, 2002
5,680
1
76
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";
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
^ 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.
 

GaryJohnson

Senior member
Jun 2, 2006
940
0
0
"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";
 

FP

Diamond Member
Feb 24, 2005
4,568
0
0
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";
?>
 

blahblah99

Platinum Member
Oct 10, 2000
2,689
0
0
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;
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
> 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).
 

FP

Diamond Member
Feb 24, 2005
4,568
0
0
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.
 

Fiveohhh

Diamond Member
Jan 18, 2002
3,776
0
0
If you don't require the resolution I would think (x < 1.11 && x > .99) would work just as well