0

UPDATE - answer was in comments... looking at the code has a part where the variable was getting asigned not zero instead of checking if not zero. != vs =!

this is not the 3 = == === question, this bug was a bit more esoteric.



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:

TL;DR: relevent code

$weightOfTenPieces = $_POST['weightOfTenPieces'];
$weightOfTenPieces = floatval($weightOfTenPieces);


$Weight = mysqli_real_escape_string($conn, $_POST['Weight']);
$CalWeight = floatval($Weight);


switch($ContainerType)
        {
        case "PALLETOFBOXES":
            $containerWeight = 24 + (1 * $containerCount);
            break;

$TARE = $_POST['TARE']; 
$tare = floatval($TARE);
$tare = $tare + $containerWeight;


$materialweight = $CalWeight - $tare;
$PieceTen =  $materialweight / $weightOfTenPieces;
        echo "PieceTen calculated at: ".$PieceTen."<br />";

the above code results in a correct responce in phpfiddle. but when I run it on my server - it beheavies as if $weightOfTenPieces == 1 when $PieceTen gets assigned - regardless of actual value... if echo'ed before - it gives the correct value... the numbers for weightoftenpeices would be something like 0.32... when hard coded in the $PieceTen assignment it returns the expected value.

messing with floating point percision, type conversions, casting - all sorts of things... no luck.


    $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

I expect 962.5 for pieceTen and 9,625 for PieceNumber

altruios
  • 986
  • 1
  • 10
  • 29
  • Something relevant [here](https://stackoverflow.com/questions/17218312/how-do-i-get-a-float-value-when-dividing-two-integers-php) perhaps? – Jacob H Sep 05 '19 at 17:50
  • 1
    There is a lot of unnecessary code here that is non-reproducible. Please provide a [mcve]. Thank you. – ggorlen Sep 05 '19 at 17:51
  • PHP.INI PRECISION SET AT 14. IT IS NOT A PRECISION ISSUE. relevant code is at top of page, the unnecessary code is there to see if anything could be messing with it. – altruios Sep 05 '19 at 18:32
  • It is a stupid and obscure bug in your code - you have `$weightOfTenPieces =! 0 && ` in your `if(...)` which is an assignment of `! 0` (which is 1) to `$weightOfTenPieces ` rather than `$weightOfTenPieces != 0 &&` which is checking if it is not 0. – Nigel Ren Sep 05 '19 at 18:33
  • nice catch! that was the bug! – altruios Sep 05 '19 at 18:46
  • Why do you use `mysqli_real_escape_string($conn`? Yo don't need it. – Dharman Sep 05 '19 at 18:57
  • I was under the impression that A real Escape on EVERY input is a good idea. why is it not needed? – altruios Sep 06 '19 at 18:45

0 Answers0