If in an If doesn't work

Hi everyone,

I want to check if a number is negative or positive.
But untill now, global.set(port + ".NMEA.nav.position.alt.altitude_relative" is not written

if (string[0] == "GGA") 
    global.set(port + ".NMEA.UTC.time", string[1]);
    global.set(port + ".NMEA.nav.position.dms.lat", string[2]);
    global.set(port + ".NMEA.nav.position.dms.NS", string[3]);
    global.set(port + ".NMEA.nav.position.dms.lon", string[4]);
    global.set(port + ".NMEA.nav.position.dms.EW", string[5]);
    global.set(port + ".NMEA.nav.fix.fix", string[6]);
    global.set(port + ".NMEA.sat.sat_used", (string[7]*1));
    global.set(port + ".NMEA.nav.position.accuracy.HDOP", string[8] * 1);
    global.set(port + ".NMEA.nav.position.alt.altitude", (string[9]*1));
    global.set(port + ".NMEA.nav.position.alt.altitude_unit", string[10]);
    global.set(port + ".NMEA.nav.position.alt.altitude_ellipsoid", (string[11]*1));
        if (Math.sign(string[11] === -1)) 
        global.set(port + ".NMEA.nav.position.alt.altitude_relative", ((string[11] - string[9])*-1));
            else if (Math.sign(string[11] === 1)) 
            global.set(port + ".NMEA.nav.position.alt.altitude_relative", ((string[11] - string[9]) * 1));
    global.set(port + ".NMEA.nav.DGPS.update", string[12]);
    global.set(port + ".NMEA.nav.DGPS.age", (string[13]*1));
    global.set(port + ".NMEA.nav.DGPS.id", string[14]);

What am I doing wrong?

I'm not sure but....

if (Math.sign(string[11] === -1)) 
        global.set(port + ".NMEA.nav.position.alt.altitude_relative", ((string[11] - string[9])*-1));

I'm not sure what altitude_ellipsoid mean as you have an altitude already.
So reading it how I see it:

if look at the Math.sign of string[11] and see if it is -1
((string[11] - string[9])*-1))

So let's look at this:

[11] is say..... -5
[9] is 20

-5 - 20 * -1
= -25 * -1
= 25

Isn't the extra * -1 at the end negating things?

- * - = +

Why are you having the last bit (on two lines - though different) of *-1 and * 1?
To me they aren't needed.

But I have been wrong before and will be again.

But to me that is wrong. Somehow.

Hi Trying_to_learn,

the difference between the *-1 and * 1 is that it removes the negative.
If string[11] is negative, do calc and make it positve.
if string[11] is positive, do calc and let it positive.

But I am not sure why this calculation doesn't work at all.
The result is that global.set(port + ".NMEA.nav.position.alt.altitude_relative" is not written to global.

In case of string[11] is 0, there will be no action.

Try to trace down actual values.

1 Like

That's a good catch @hotNipi
I didn't even think of that one.

My bad, I misunderstood what Math.sign did. :man_facepalming:

The Math.sign() static method returns 1 or -1, indicating the sign of the number passed as argument. If the input is 0 or -0, it will be returned as-is.

That syntax is also wrong I think. It should be:

if (Math.sign(string[11]) === -1)

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.