Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Avoiding float overflow "Math Error" crashes

Thickbrick Sleaford
Baffled
Join date: 28 Dec 2006
Posts: 26
07-08-2008 07:03
I'm writing a chat calculator that takes user input and runs it through llPow() (among others). My problem is that the user can potentially do something like llPow(2, 128), which will crash the script with a math error. I think I can avoid crashes with imaginary results ( if ((base<0.0) && (exponent<1.0)) ), but I can't figure out how to check for results that are too large without actually doing the math and crashing.

I've noticed that the script only crashes when I try to do anything with the result of the llPow() call (like cast it to string). This may mean there's a way to catch it and not crash, but I can't figure it out. So is there way to do this?

I've thought of using a watchdog script, but apparently llResetOtherScript() can't reset scripts that crashed. The only other way I can think of is deleting the crashed script and using llRemoteLoadScriptPin() to re-upload it from another prim.
Hewee Zetkin
Registered User
Join date: 20 Jul 2006
Posts: 2,702
07-08-2008 11:51
Well, N^M = e^(ln(N)*M) so you could calculate ln(N)*M and test whether that is bigger than some threshold value before performing the actual exponentiation.
Thickbrick Sleaford
Baffled
Join date: 28 Dec 2006
Posts: 26
07-08-2008 13:59
Thanks!

The constant to check is 88.722839 (ln(2)*128)
and the check is:
if (llLog(llFAbs(N)) * M >= 88.722839)

I saw something similar being done in the library float2hex functions, but I didn't understand what was going on there. I claim extenuating "long time since highschool" for my ignorance.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
07-08-2008 18:53
From: Thickbrick Sleaford
I saw something similar being done in the library float2hex functions, but I didn't understand what was going on there. I claim extenuating "long time since highschool" for my ignorance.

I would be surprised if in highschool the float2hex code was easily decipherable; I spent a long time thinking about clever ways of optimizing the code to such an end I don't even fully understand how all the code works now.

The current iteration of float2hex, if I remember correctly, ignores the entire rounding error problem; I managed to do this by building a bit of slop into the algorithm so that it doesn't upset the math if the exponent is one off in either direction.

Instead of float2hex, I would recommend looking at FUI and IUF; for those I had to correct the rounding error directly.
https://wiki.secondlife.com/wiki/User:Strife_Onizuka#Float_.3C-Union-.3E_Integer

Using logarithms is the correct approach here but you will get rounding errors that you won't be able to correct for, expect to loose some of the very highest numbers (> 2^127).
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey
Kaluura Boa
Polygon Project
Join date: 27 Mar 2007
Posts: 194
07-09-2008 01:27
From: Thickbrick Sleaford
I've thought of using a watchdog script, but apparently llResetOtherScript() can't reset scripts that crashed. The only other way I can think of is deleting the crashed script and using llRemoteLoadScriptPin() to re-upload it from another prim.


I played with a crashing script and I got exactly the same idea. This is not "pretty" but this is a way around llResetOtherScript() uselessness. Besides, it's more bulletproof than adding tons of tests to slow down a script that may crash only in some corner cases.
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
07-09-2008 07:03
From: Kaluura Boa
I played with a crashing script and I got exactly the same idea. This is not "pretty" but this is a way around llResetOtherScript() uselessness. Besides, it's more bulletproof than adding tons of tests to slow down a script that may crash only in some corner cases.

I'm pretty sure they removed that limitation from llResetOtherScript.

EDIT: I could be wrong. Here is the jira for the issue.
https://jira.secondlife.com/browse/SVC-57
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey