this is not about floating point math in general being broken... it IS NOT THE SAME ISSUE - this is likely an automatic type conversion - not float precision issue:
So I'm trying to write an a small calculation to make an estimate of the number of pieces of material that arrive on a pallet... I am taking the weight of the pallet, subtracting excess weight (tare: weight of any boxes, pallets, etc.) - dividing it by the weight of a sample of ten pieces, then multiplying by 10 to get the total amount. (sample of ten for estimating average piece weight)
running on paper everything seemed fine but: when writing that in PHP it would give a erroneous response.
I've broken that equation up and echoing results back to diagnose why I'm getting ridiculous returns.
long story short, there is some rounding automatically happening... and I'm not sure why.
I'm gathering weight in LBS, the weight of a pallet is a whole number (stored as a float).
the weight of sample of ten is some fraction of a pound - say, 0.32. lets say the weight is 123 lbs. the result of that weight and piece weight would be...123.
the fraction is always getting rounded up to 1 - as if it's type converted to int. (php should type convert the INT to a FLOAT, not the other way around?... which should be irreverent here because both numbers are stored as floats)
why is this value always rounding up (I'm assuming it's type converting to an INT but why is that happening), and how do I stop it from doing so.
below is the entire calculation - switch and all, the only relevant part to the question is at the $pieceTen assignment line.
I've tried using floatval in a few different place, I've tried one long equation, I've tried breaking it up step by step and that's where I discovered the rounding.
$weightOfTenPieces = $_POST['weightOfTenPieces'];
$weightOfTenPieces = floatval($weightOfTenPieces);
$ContainerType = mysqli_real_escape_string($conn, $_POST['ContainerType']);
$Weight = mysqli_real_escape_string($conn, $_POST['Weight']);
$PieceNumber = 0;
$containerNumber = mysqli_real_escape_string($conn, $_POST['containerNumber']);
$containerCount = intval($containerNumber);
$CalWeight = floatval($Weight);
//weight calculated in oz to avoid floating point numbers
echo "container count::".$containerCount."<br /> CalWeight:: ".$CalWeight."<br /> weight of ten pieces:: ".$weightOfTenPieces."<br />";
switch($ContainerType)
{
case "PALLETOFBOXES":
$containerWeight = 24 + (1 * $containerCount);
break;
case "PALLET":
$containerWeight = 24 * $containerCount;
case "BOX":
$containerWeight = 1 * $containerCount;
break;
case "TUB":
$containerWeight = 2 * ($containerCount);
break;
default:
$containerWeight = 1 * $containerCount;
break;
}
echo "container weight:: ".$containerWeight."<br />";
if ($weightOfTenPieces =! 0 &&
$weightOfTenPieces =! null &&
$containerWeight != null &&
$containerWeight != 0 )
{
$TARE = $_POST['TARE'];
$tare = floatval($TARE);
$tare = $tare + $containerWeight;
//convert weight from lbs to oz
$materialweight = $CalWeight - $tare;
echo "material weight calculated at: ".$materialweight."<br />";
$PieceTen = $materialweight / $weightOfTenPieces;
echo "PieceTen calculated at: ".$PieceTen."<br />";
$PieceNumber = $PieceTen * 10;
}
else
{
ECHO "not calucalted route";
$PieceNumber = "not calculated";
}
echo "piece number calculated at: ".$PieceNumber."<br />";
debug result screen:
container count::5
CalWeight:: 337
weight of ten pieces:: 0.32
container weight:: 29
material weight calculated at: 308
PieceTen calculated at: 308
piece number calculated at: 3080
Record updated successfully
I expect 962.5 for pieceTen and 9,625 for PieceNumber
"; ``` – altruios Sep 05 '19 at 16:09