Board index FlightGear Development Nasal

Nasal: floating point error in math.pow()

Nasal is the scripting language of FlightGear.

Nasal: floating point error in math.pow()

Postby Necolatis » Sat Nov 23, 2019 12:37 am

I get this error: floating point error in math.pow()

The line has math.pow(a,2)

Does this mean 'a' is a NaN? And is there any way to check in Nasal if a variable is NaN?
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Nasal: floating point error in math.pow()

Postby wkitty42 » Sat Nov 23, 2019 1:11 am

can you add a quick debug print line before that line to print out the value of 'a'?
i'm not sure if there is an isNAN() routine or not... i know there is something in javascript which i use in another life...
"You get more air close to the ground," said Angalo. "I read that in a book. You get lots of air low down, and not much when you go up."
"Why not?" said Gurder.
"Dunno. It's frightened of heights, I guess."
User avatar
wkitty42
 
Posts: 9148
Joined: Fri Feb 20, 2015 4:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 20.04

Re: Nasal: floating point error in math.pow()

Postby Necolatis » Sat Nov 23, 2019 1:15 am

That would flood the console. It gets called maybe 10000 times per flight until at some point the error might pop up.
Its hard to reproduce too.
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10

Re: Nasal: floating point error in math.pow()

Postby Richard » Sat Nov 23, 2019 1:10 pm

this error is coming from some post calculation validation in simgear\nasal\mathlib.c - not sure if valid to check FP numbers like this.

Code: Select all
// Toss a runtime error for any NaN or Inf values produced.  Note that
// this assumes an IEEE 754 format.
#define VALIDATE(r) (valid(r.num) ? (r) : die(c, __FUNCTION__+2))

static int valid(double d)
{
    union { double d; unsigned long long ull; } u;
    u.d = d;
    return ((u.ull >> 52) & 0x7ff) != 0x7ff;
}

static naRef die(naContext c, const char* fn)
{
    naRuntimeError(c, "floating point error in math.%s()", fn);
    return naNil();
}
Richard
 
Posts: 810
Joined: Sun Nov 02, 2014 11:17 pm
Version: Git
OS: Win10

Re: Nasal: floating point error in math.pow()

Postby xcvb » Sat Nov 23, 2019 1:46 pm

I wonder why the standard macro isfinite isn't used here.
xcvb
 
Posts: 132
Joined: Sat Mar 14, 2015 3:08 pm
Version: Next
OS: Fedora Kinoite

Re: Nasal: floating point error in math.pow()

Postby Necolatis » Sat Nov 23, 2019 2:41 pm

Thanks Richard, that confirmed my suspicion.
"Airplane travel is nature's way of making you look like your passport photo."
— Al Gore
User avatar
Necolatis
 
Posts: 2233
Joined: Mon Oct 29, 2012 1:40 am
Location: EKOD
Callsign: Leto
IRC name: Neco
Version: 2020.3.19
OS: Windows 10


Return to Nasal

Who is online

Users browsing this forum: No registered users and 6 guests