Jump to content

[SOLVED] Velocity=Distance/Time -- Works Only for certain combinations


monkeypaw201

Recommended Posts

i have this code that works when i post velocity and time, distance and time, but not velocity and distance

 

the time is posted in minutes, velocity in miles per hour (outputted in knots) and the distance is in miles...

 

if you could find out whats wrong, much appreciated

 

<?php
if(isset($_GET['time']) | isset($_GET['velocity']) | isset($_GET['distance']))
{

// Define Variables
if($_GET['time'] != "")
$tt = $_GET['time'];
else
$tt = "?";

if($_GET['velocity'] != "")
$v = $_GET['velocity'];
else
$v = "?";

if($_GET['distance'] != "")
$d = $_GET['distance'];
else
$d = "?";

$deg = $_GET['degrees'];

//Convert Variables
if($_GET['time'] != "")
$t = $tt/60;
else
$t = $tt;

// Calculate
if($d == "?")
$d = $v*$t;
elseif($v == "?")
$v = $d/$t;
elseif($t == "?")
$t = $d/$v;

//Convert results
$dn = round($d);
$vn = round($v*0.868976242);
if(isset($_GET['time']))
$tn = round($tt);
else
$tn = round($t*60);
?>

Hello, <br><br>

Given the information and selections you provided, the calculations have been made and the generated information is below. <br><br>

Traveling at <b><?php echo $vn; ?> kts</b><br>
Turning <b><?php echo $deg; ?></b>° degrees<br><br>

Will take you <b><?php echo $tn; ?></b> minutes and <b><?php echo $dn; ?></b> miles

<?php }else{ ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
  </head>
  <body><FORM action="aircraft_turn.php" method="GET" enctype="multipart/form-data">
          To calulate a number, enter the two you are not looking for (ie searching: time, provide: speed, distance) 
      <br>
      <br>
      Speed: <INPUT maxlength="3" name="velocity" type="text"> mph<br>
      Time: <INPUT name="time" type="text"> minutes<br>
      Distance: <INPUT name="distance" type="text"> miles<br><br><INPUT type="submit" name="submit" value="Calculate!">
    </FORM>
  </body>
</html>
<?php } ?>

your code could use some optimizing but I believe your issue is inconsistent units.

 

Your velocity is being entered in as miles per hour, but you are using time in seconds.

 

Here are my suggestions:

 

1.  Convert time into seconds as soon as you read it in.

2.  Convert velocity into miles/second

3.  Cast all three values as floats

 

What is happening...

When you divide distance by velocity, your velocity is too high by a factor of 3600 (60^2) giving you a relatively small number for time (in seconds).  When you convert to minutes, this number is almost always going to be less then a minute, so it is rounded to zero.

 

So the simple solution is to just divide your incoming velocity by 3600, but I would simplify your code too.  Something like this:

 

<?php
if(isset($_GET['time']) | isset($_GET['velocity']) | isset($_GET['distance']))
{
$time = ((float)$_GET['time'])*60;		//time in seconds
$velocity = ((float)$_GET['velocity'])/3600;	//velocity in mi/s
$distance =  (float)$_GET['distance'];	//distance in mi


if( $time != '' && $velocity != '') { 
$distance =  $time * $velocity;
}

if( $time != '' && $distance != '') { 
$velocity = $distance/$time;
}

if( $distance!= '' && $velocity != '') { 
$time =  $distance/$velocity;
}

$deg = $_GET['degrees'];


//Convert results
$distance_mi = round($distance);
$velocity_knotts = round($velocity*3600*0.868976242);   //convert to mph then to knotts
$time_minutes = round($time/60);                                 //convert to minutes
?>

Hello, <br><br>

Given the information and selections you provided, the calculations have been made and the generated information is below. <br><br>

Traveling at <b><?=$velocity_knotts; ?> kts</b><br>
Turning <b><?=$deg; ?></b>° degrees<br><br>

Will take you <b><?=$time_minutes; ?></b> minutes and <b><?=$distance_miles; ?></b> miles

<?php }else{ ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
  </head>
  <body><FORM action="test.php" method="GET" enctype="multipart/form-data">
          To calulate a number, enter the two you are not looking for (ie searching: time, provide: speed, distance) 
      <br>
      <br>
      Speed: <INPUT maxlength="3" name="velocity" type="text"> mph<br>
      Time: <INPUT name="time" type="text"> minutes<br>
      Distance: <INPUT name="distance" type="text"> miles<br><br><INPUT type="submit" name="submit" value="Calculate!">
    </FORM>
  </body>
</html>
<?php } ?>

 

casting might not be necessary, but I don't trust do math problems on variables with implied type.  I've had results turn out orders of magnitude wrong because one variable 3 equations deep was automatically cast as an int instead of a float (rounding off the decimal).  Those are not fun to debug.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.