-2

This is really weird.

  • An object is defined with numerous properties
  • The property is calculated and stored as a decimal value (2 places)
  • I log the value to console to confirm and it is correct (561.77)
  • After a few more calculations of other properties I log the same value (561.77, correct)
  • When I log the full object, the property somehow rounded up to 562.

Why is it doing this?

The only thing I do is preset all of the object properties = NaN before I solve for each property.

enter image description here

Edit

OK heres the function...

function Airflows($data, isWarmSide) {
            var $PROP = (isWarmSide) ? $data.Warm : $data.Cool;
            var $AF = $PROP.Airflow;
            var $BP = $PROP.Bypass;
            var $HX = $PROP.Hx;

            var af = $AF.scfm, bp = $BP.scfm, hx = $HX.scfm;

            if (!isNaN($AF.scfm)) {
                isSCFM = true;
            }
            else if (!isNaN($AF.acfm)) {
                af = $AF.acfm;
                bp = $BP.acfm;
                hx = $HX.acfm;
                isACFM = true;
            }
            else if (!isNaN($AF.lbsmin)) {
                af = $AF.lbsmin;
                bp = $BP.lbsmin;
                hx = $HX.lbsmin;
                isLbsMin = true;
            }
            else if (!isNaN($AF.lbshr)) {
                af = $AF.lbshr;
                bp = $BP.lbshr;
                hx = $HX.lbshr;
                isLbsHr = true;
            }
            else if (!isNaN($AF.lbshrw)) {
                af = $AF.lbshrw;
                bp = $BP.lbshrw;
                hx = $HX.lbshrw;
                isLbsHrw = true;
            }

            var HasBypass = (bp > 0 && !isNaN(bp) && typeof (bp) !== 'undefined');

            //Check if the Bypass is 0
            if (!HasBypass) {
                $BP.scfm = 0;
                $BP.acfm = 0;
                $BP.lbsmin = 0;
                $BP.lbshr = 0;
                $BP.lbshrw = 0;
            }

            //We have bypass
            else {
                //Make sure the bypass is not greater entry flow
                if (bp > af) {
                    $PROP.Errors.push(
                        { obj: (($data.IsWarmSide) ? $AFB_W : $AFB_W), txt: "Bypass cannot be greater than entry flow." }
                    );
                    return $data;
                }
            }

            var _GASES = $PROP.GasComposition;
            var gas0 = (_GASES.length > 0) ? parseFloat(_GASES[0].num) : 99.502;
            var gas1 = (_GASES.length > 1) ? parseFloat(_GASES[1].num) : 0.498;

            let g = Math.round(parseFloat(gas0 + gas1) * 100) / 100;

            var Tdry = $PROP.Tdb.F;
            var lbwlba = $PROP.Humidity.LbwLba;
            var alt = $PROP.Altitude.ft || 0;
            var Pg = $PROP.Pg.inWC || 0;
            var Pabs = $PROP.Pabs.psia || 14.696;
            var PabsIsStd = Pabs == 14.696;

            var NO_GAUGE = (Pg === 0);

            var IS_SCFM = (!isNaN($AF.scfm));
            var IS_ACFM = (!isNaN($AF.acfm));
            var IS_LBSMIN = (!isNaN($AF.lbsmin));
            var IS_LBSHR = (!isNaN($AF.lbshr));
            var IS_LBSHRW = (!isNaN($AF.lbshrw));

            var psfa = calcPSFA(alt);

            var ALT_PRESS = (NO_GAUGE) ? psfa : Math.round(Pabs * 144 * 1000) / 1000;



            var _p;

            // Testing to see if a custom gas is used.
            // If the sum of Air and Water Vapor is 100 and if NOT Lbm/hr in the Gas Configuration selection
            if (g == 100 && gas1 != 100 && getInt($data.GasConfiguration.GasAssignment) > 0) {


                if (IS_SCFM) {
                    $AF.lbshr = Math.round(ConvertSCFMtoLbsHr(af, lbwlba));
                    $AF.lbsmin = Math.round(($AF.lbshr / 60) * 100) / 100;
                    $AF.lbshrw = Math.round(($AF.lbshr * parseFloat(1 + lbwlba)));

                    $AF.acfm = Math.round(ConvertLbsAirToACFM($AF.lbshr, Tdry, ALT_PRESS, lbwlba, _GASES));
                    if (HasBypass) {
                        $BP.lbshr = Math.round(ConvertSCFMtoLbsHr(bp, lbwlba));
                        $BP.lbsmin = Math.round(($BP.lbshr / 60) * 100) / 100;
                        $BP.lbshrw = Math.round(($BP.lbshr * (1 + lbwlba)));
                        $BP.acfm = Math.round(ConvertLbsAirToACFM($BP.lbshr, Tdry, ALT_PRESS, lbwlba, _GASES));
                    }
                }
                else if (IS_ACFM) {

                    $AF.lbsmin = 0.0000;


                    $AF.scfm = Math.round(ConvertFlowToSCFM($AF.acfm, Pabs, Tdry, lbwlba, "ACFM", PabsIsStd));
                    $AF.lbshr = Math.round(ConvertSCFMtoLbsHr($AF.scfm, lbwlba));
                    $AF.lbshrw = Math.round($AF.lbshr * parseFloat(1 + lbwlba));

                    if (isWarmSide) {
                        $data.Warm.Airflow.lbsmin = Math.round(($AF.lbshr / 60) * 100) / 100;
                    }
                    else {
                        $data.Cool.Airflow.lbsmin = (Math.round(($AF.lbshr / 60) * 100) / 100);
                    }


                    console.log("$AF.lbsmin: " + $AF.lbsmin);
                    console.log("$data.Cool.Airflow.lbsmin [0]: " + $data.Cool.Airflow.lbsmin);

                    if (HasBypass) {
                        $BP.scfm = Math.round(ConvertFlowToSCFM($BP.acfm, Pabs, Tdry, lbwlba, "ACFM", PabsIsStd));
                        $BP.lbshr = Math.round(ConvertSCFMtoLbsHr($BP.scfm, lbwlba));
                        $BP.lbsmin = Math.round(($BP.lbshr / 60) * 100) / 100;
                        $BP.lbshrw = Math.round(($BP.lbshr * (1 + lbwlba)));
                    }
                }
                else if (IS_LBSMIN) {
                    $AF.lbshr = Math.round($AF.lbsmin * 60);
                    $AF.lbshrw = Math.round($AF.lbshr * parseFloat(1 + lbwlba));
                    $AF.acfm = Math.round(ConvertFlowToACFM($AF.lbshr, alt, Tdry, lbwlba, "LBSHR", true));


                    $AF.scfm = Math.round(ConvertLbsAirToSCFM($AF.lbshr, 70, Pabs, lbwlba, null));

                    if (HasBypass) {
                        $BP.lbshr = Math.round($BP.lbsmin * 60);
                        $BP.lbshrw = Math.round(($BP.lbshr * (1 + lbwlba)));
                        $BP.acfm = Math.round(ConvertLbsAirToSCFM($BP.lbshr, Tdry, ALT_PRESS, lbwlba, null));
                        $BP.scfm = Math.round(ConvertLbsAirToSCFM($BP.lbshr, 70, ((NO_GAUGE) ? psfa : 14.696), lbwlba, null));
                    }
                }

                else if (IS_LBSHR) {
                    $AF.lbshrw = Math.round($AF.lbshr * parseFloat(1 + lbwlba));
                    $AF.lbsmin = Math.round(($AF.lbshr / 60) * 100) / 100;
                    $AF.acfm = Math.round(ConvertLbsAirToSCFM($AF.lbshr, Tdry, ALT_PRESS, lbwlba, null));
                    $AF.scfm = Math.round(ConvertLbsAirToSCFM($AF.lbshr, 70, ((NO_GAUGE) ? psfa : 14.696), lbwlba, null));

                    if (HasBypass) {
                        $BP.lbshrw = Math.round(($BP.lbshr * (1 + lbwlba)));
                        $BP.lbsmin = Math.round(($BP.lbshr / 60) * 100) / 100;
                        $BP.acfm = Math.round(ConvertLbsAirToSCFM($BP.lbshr, Tdry, ALT_PRESS, lbwlba, null));
                        $BP.scfm = Math.round(ConvertLbsAirToSCFM($BP.lbshr, 70, ((NO_GAUGE) ? psfa : 14.696), lbwlba, null));
                    }
                }

                else if (IS_LBSHRW) {
                    $AF.lbshr = Math.round($AF.lbshrw / parseFloat(1 + lbwlba));
                    $AF.lbsmin = Math.round(($AF.lbshr / 60) * 100) / 100;
                    $AF.acfm = Math.round(ConvertLbsAirToSCFM($AF.lbshr, Tdry, ALT_PRESS, lbwlba, null));
                    $AF.scfm = Math.round(ConvertLbsAirToSCFM($AF.lbshr, 70, ((NO_GAUGE) ? psfa : 14.696), lbwlba, null));

                    if (HasBypass) {
                        $BP.lbshr = $BP.lbshrw;
                        $BP.lbsmin = Math.round(($BP.lbshr / 60) * 100) / 100;
                        $BP.acfm = Math.round(ConvertLbsAirToSCFM($BP.lbshr, Tdry, ALT_PRESS, lbwlba, null));
                        $BP.scfm = Math.round(ConvertLbsAirToSCFM($BP.lbshr, 70, ((NO_GAUGE) ? psfa : 14.696), lbwlba, null));
                    }
                }

            }

            //Else we have a Custom Gas Selection
            else {

                /*
                 Notes:
                    -   We need to DISABLE Humidity
                    -   The Water Vapor gas changes Airflow MASS FLOW RATES
                    -   Air and Water Vapor gases change Humidity
                    -   If Custom Gas, then the Humidities() function will need the ability to solve ALL Humidities (ie: assume all Humidity inputs are NaN)
                    -   A Humidity Change should trigger an Airflow Change (to solve for Mass Flow Rates)
                */

                var Mode = getInt($data.GasConfiguration.GasAssignment);

                //If we do not have two gases selected OR WaterVapor is equal to 1 OR Mode is Lbm/hr then WE DONT HAVE AIRWATER_ON
                var AIRWATER_ON = !(_GASES.length != 2 || _GASES[1].num == 1 || Mode === 0);

                //Determine the Pressure Type we are using (are we calculating pressure using altitude, or using absolute pressure)
                var PRESS = (Pg === 0) ? Pabs : alt;


                //If not lbm/hr
                if (Mode > 0) {

                    if (!isNaN($AF.scfm)) {

                        $AF.lbshr = Math.round(ConvertToLbsAir($AF.scfm, Tdry, 14.696, _GASES));
                        $AF.lbshrw = $AF.lbshr;
                        $AF.lbsmin = Math.round(($AF.lbshr / 60) * 100) / 100;
                        $AF.acfm = Math.round(ConvertLbsAirToACFM($AF.lbsair, Tdry, PRESS, $PROP.Humidity.LbwLba, ((AIRWATER_ON) ? null : _GASES)));

                        //Set Bypass
                        if ($BP.scfm > 0) {
                            $BP.lbshr = Math.round(ConvertToLbsAir($AF.scfm, Tdry, 14.696, _GASES));
                            $BP.lbshrw = $BP.lbshr;
                            $BP.lbsmin = Math.round(($BP.lbshr / 60) * 100) / 100;
                        }
                        else {
                            $BP.scfm = 0;
                            $BP.acfm = 0;
                            $BP.lbshr = 0;
                            $BP.lbshrw = 0;
                            $BP.lbsmin = 0;
                        }
                    }

                }


            }


            console.log("$data.Cool.Airflow.lbsmin [1]: " + $data.Cool.Airflow.lbsmin);

            if (HasBypass) {
                $HX.scfm = ($AF.scfm - $BP.scfm);
                $HX.acfm = ($AF.acfm - $BP.acfm);
                $HX.lbsmin = ($AF.lbsmin - $BP.lbsmin);
                $HX.lbshr = ($AF.lbshr - $BP.lbshr);
                $HX.lbshrw = ($AF.lbshrw - $BP.lbshrw);

            }
            else {
                $HX.scfm = $AF.scfm;
                $HX.acfm = $AF.acfm;
                $HX.lbsmin = $AF.lbsmin;
                $HX.lbshr = $AF.lbshr;
                $HX.lbshrw = $AF.lbshrw;
            }

            console.log("$data.Cool.Airflow.lbsmin [2]: " + $data.Cool.Airflow.lbsmin);

            $data.Cool.Airflow.lbsmin = Math.round($data.Cool.Airflow.lbsmin * 100) / 100;

            console.log("--- showing data ---");
            console.log($data);



            return $data;
        }
Sanya
  • 1,226
  • 4
  • 19
  • 40
  • 1
    That's what `Math.round()` does. You have lots of calls to this when setting properties. – Barmar Jun 01 '22 at 23:20
  • 1
    @Barmar Math.round(x * 100) / 100 is how you convert to 2 decimal places – Sanya Jun 01 '22 at 23:22
  • Even If I take out that last line to round to 2 decimal places it still does the same thing. That line is not the issue – Sanya Jun 01 '22 at 23:23
  • I suggest you use the single-stepper to see when the value is changing. – Barmar Jun 01 '22 at 23:25
  • 3
    Note that the console keeps a live reference to the object. So if the object is modified after you log it, it will change in the console. Use `console.log(JSON.stringify($data, null, 2);` to log a snapshot. – Barmar Jun 01 '22 at 23:26
  • @Barmar stringifying Json seems to show the correct value (561.77). What do you think the issue is? – Sanya Jun 01 '22 at 23:29
  • 1
    As I said, something is changing the value later, after you log it. – Barmar Jun 01 '22 at 23:31
  • @Barmar got it. Thank you. I never knew that about console – Sanya Jun 01 '22 at 23:34

0 Answers0