0

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
  • 986
  • 1
  • 10
  • 29
  • the amount wrong does not lead me to think it is a 'precision issue', this is rounding to a whole number, not missing a 0.0000000001 of a digit. – altruios Sep 05 '19 at 15:55
  • in which case edit your question with only the relevant code instead of the whole file, show which var = the 0.32 and show the calculation - nothing else is needed. Expected and actual output also is good – treyBake Sep 05 '19 at 15:59
  • I included everything to in case there was something I missed in initializing the values. there is an expected and actual output... it's also described exactly which part of the code is in question. this is a weird enough problem that I'm likely missing something obvious. this also results in rounding to 1. ```php $PieceTen = 0.0; $PieceTen = number_format((float)$PieceTen, 5, '.', ''); $PieceTen = number_format((float)$materialweight, 5, '.', '') / number_format((float)$weightOfTenPieces, 5, '.', ''); echo "PieceTen calculated at: ".$PieceTen."
    "; ```
    – altruios Sep 05 '19 at 16:09
  • can you give me an example $materialweight^ – treyBake Sep 05 '19 at 16:11
  • and weightoftenpieces oto please – treyBake Sep 05 '19 at 16:12
  • material weight would be something like 123 weightoftenpiece 0.32. – altruios Sep 05 '19 at 16:14
  • https://phpfiddle.tk/ec4a6fdb <- produces a bigger number than 1 o.O – treyBake Sep 05 '19 at 16:15
  • very interesting. so Its somewhere machine/server side, not code side. it returns correct number in php fiddle, but still returns 1 when doing the calculation on my server... so what could cause that? running php 7.3.6. – altruios Sep 05 '19 at 16:19
  • .... you're returning a boolean aren't you? You're returning a `true` rather than the value (or maybe something along those lines) – treyBake Sep 05 '19 at 16:21
  • even more interesting. its not floating points at all it looks like. if weightOfTenPieces=23, it still gets turned into 1... it's not dividing the value at all by the looks of it. – altruios Sep 05 '19 at 16:25
  • if you look up to the code, on top of WeightofTen returning an echo properly, all the echos return the correct numbers, except the result of the calculation... so I don't think it's a bool sneaking in anywhere. – altruios Sep 05 '19 at 16:27

0 Answers0