Board index FlightGear Development Nasal

math.clamp() fixes

Nasal is the scripting language of FlightGear.

math.clamp() fixes

Postby Red Leader » Thu Apr 28, 2016 8:59 pm

Hi all,

I think I've found a bug in math.clamp(). While documenting it, I found that it behaves very weirdly. Looking at the source code, it looks like the algorithm (lines 126-127) has some typos/mixups which make the output weird. The solution would be to change the code to the following:
Code: Select all
static naRef f_clamp(naContext c, naRef me, int argc, naRef* args)
{
    naRef min = naNumValue(argc > 0 ? args[0] : naNil());
    naRef max = naNumValue(argc > 1 ? args[1] : naNil());
    naRef x = naNumValue(argc > 2 ? args[2] : naNil());

    if(naIsNil(min) || naIsNil(max) || naIsNil(x))
        naRuntimeError(c, "non numeric arguments to clamp()");

    if (x.num < min.num)
        x.num = min.num;
    else if (x.num > max.num)
        x.num = max.num;
    return VALIDATE(x);
}

This fixes the algorithm and renames variables a and b to min and max to make it clearer.

Reagrds,
Red Leader

P.S. If someone could forward this to the mailing list, it would be greatly appreciated.
For once you have tasted flight you will walk the earth with your eyes turned skywards, for there you have been and there you will long to return.
Leonardo da Vinci

Projects:
BAe Sea Harrier FA2Scripted AI Objects frameworkDocumenting Nasal
Red Leader
 
Posts: 22
Joined: Sat Oct 25, 2014 8:52 pm
Location: United Kingdom
Callsign: Red-Led
Version: 2017.3.1
OS: Microsoft Windows 10

Re: math.clamp() fixes

Postby wkitty42 » Sat Apr 30, 2016 3:13 pm

Red Leader wrote in Thu Apr 28, 2016 8:59 pm:P.S. If someone could forward this to the mailing list, it would be greatly appreciated.

done...
"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: 9146
Joined: Fri Feb 20, 2015 4:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 20.04

Re: math.clamp() fixes

Postby Red Leader » Sat Apr 30, 2016 5:14 pm

Thanks!
For once you have tasted flight you will walk the earth with your eyes turned skywards, for there you have been and there you will long to return.
Leonardo da Vinci

Projects:
BAe Sea Harrier FA2Scripted AI Objects frameworkDocumenting Nasal
Red Leader
 
Posts: 22
Joined: Sat Oct 25, 2014 8:52 pm
Location: United Kingdom
Callsign: Red-Led
Version: 2017.3.1
OS: Microsoft Windows 10

Re: math.clamp() fixes

Postby wkitty42 » Mon May 02, 2016 10:13 pm

from the dev list...

torstenD wrote:I think the arguments for clamp should be value/min/max and not min/max/value as suggested?
Besides that, the algorithm looks indeed suspicious.
Fix pushed: https://sourceforge.net/p/flightgear/simgear/ci/bba11c18d1ebf2c347d6330ff1d5b9aef75e76cb/

Thanks for reporting

Torsten
"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: 9146
Joined: Fri Feb 20, 2015 4:46 pm
Location: central NC, USA
Callsign: wk42
Version: git next
OS: Kubuntu 20.04


Return to Nasal

Who is online

Users browsing this forum: No registered users and 3 guests