VirtualKitten Posted July 15, 2020 Posted July 15, 2020 (edited) Dear Scribblers I am trying to cast a string value to a float such as float type = (float)"4*PI/2"; though the string is in a string variable . This seems to bring back 4 not around 6.9. Is there a function to make this evaluate. It also seems it seems float value = (float)"4*3.1472/2"; wont evaluate either if held in string variable. How can i do this calculation from a string please? D Edited July 15, 2020 by VirtualKitten NA
Mollymews Posted July 15, 2020 Posted July 15, 2020 not out-of-LSL-box we have to write a math expression parser. Somebody may have written one for LSL. dunno is quite a lot of code tho on the internet showing how to write such a math expression parser. We can google and get and port to LSL as needed/wanted
VirtualKitten Posted July 15, 2020 Author Posted July 15, 2020 (edited) Thanks Molly, Oh lord not an HTTPRequest to do this that is madness. I have tried searching google for one for LSL one but have had no luck outwoldz has not one either! Edited July 15, 2020 by VirtualKitten NA
Mollymews Posted July 15, 2020 Posted July 15, 2020 4 minutes ago, VirtualKitten said: I have tried searching google for one for LSL one but have had no luck find one that is written in a language you have some experience/knowledge and then port it to LSL for example here is a basic tutorial for javascript https://medium.com/@stoopidguy1992/how-to-write-a-math-expression-parser-in-javascript-b5147bc9466b some code here also, written in C https://github.com/zserge/expr
Fritigern Gothly Posted July 15, 2020 Posted July 15, 2020 (edited) Could this help a little? http://wiki.secondlife.com/wiki/String2Float EDIT: Nah, you need the expression parsed, not just get a float. Sorry, got a bit too enthusiastic Edited July 15, 2020 by Fritigern Gothly 1
Love Zhaoying Posted July 15, 2020 Posted July 15, 2020 33 minutes ago, Fritigern Gothly said: Could this help a little? http://wiki.secondlife.com/wiki/String2Float EDIT: Nah, you need the expression parsed, not just get a float. Sorry, got a bit too enthusiastic Just write a full expression evaluator, with order of operation support..😹
Wulfie Reanimator Posted July 19, 2020 Posted July 19, 2020 (edited) On 7/15/2020 at 1:13 PM, Love Zhaoying said: Just write a full expression evaluator, with order of operation support..😹 Watch your words... 300 lines of code and some light recursion. It's not perfect, but I only spent a little over an hour or two on it and LSL is not built for string parsing at all. You have to do a function call (and create a new string) every time you want to check a single character of an existing string. Not just that, but LSL (for some baffling reason) doesn't have break or continue, so you have to use jumps to control (exit/restart) your loops. On 7/15/2020 at 12:38 PM, Fritigern Gothly said: Could this help a little? http://wiki.secondlife.com/wiki/String2Float Why does that even exist?? You can literally just typecast a string to a float, even if there's text behind the number... value = (float)"40.23578"; llOwnerSay((string)value); // 40.235780 value = (float)"-683.3"; llOwnerSay((string)value); // -683.300000 value = (float)"9924"; llOwnerSay((string)value); // 9924.000000 value = (float)"-123abc"; llOwnerSay((string)value); // -123.000000 Edited July 19, 2020 by Wulfie Reanimator 1 1
Rachel1206 Posted July 19, 2020 Posted July 19, 2020 2 hours ago, Wulfie Reanimator said: Why does that even exist?? You can literally just typecast a string to a float, even if there's text behind the number... value = (float)"40.23578"; llOwnerSay((string)value); // 40.235780 value = (float)"-683.3"; llOwnerSay((string)value); // -683.300000 value = (float)"9924"; llOwnerSay((string)value); // 9924.000000 value = (float)"-123abc"; llOwnerSay((string)value); // -123.000000 Sunday morning thoughts.... Maybe from the old LSL pre Mono days, I really wish, the wiki entries was date marked and clearly referring to version of script implementation. Notice, the wiki states: "Under both Mono and LSO-LSL you may find strange results if dealing with strings containing more than 9 decimal places." Now casting floats to string truncates the floating number to max 6 decimal places, although this is an academic problem, it could be a problem if doing and representing scientific precise calculations. 1
Mollymews Posted July 19, 2020 Posted July 19, 2020 2 hours ago, Wulfie Reanimator said: Watch your words... 300 lines of code and some light recursion. i like this bit. 300 lines is a pretty good effort for an initial go at it
Wulfie Reanimator Posted July 19, 2020 Posted July 19, 2020 23 minutes ago, Mollymews said: i like this bit. 300 lines is a pretty good effort for an initial go at it If we discount comments, empty lines, and curly braces... 157 lines. Including some code I duplicated because I hadn't compartmentalized it into a function yet. 29 minutes ago, Rachel1206 said: Sunday morning thoughts.... Maybe from the old LSL pre Mono days, I really wish, the wiki entries was date marked and clearly referring to version of script implementation. Notice, the wiki states: "Under both Mono and LSO-LSL you may find strange results if dealing with strings containing more than 9 decimal places." Now casting floats to string truncates the floating number to max 6 decimal places, although this is an academic problem, it could be a problem if doing and representing scientific precise calculations. LSL floats are single-precision (32-bit), so they literally cannot have more than 7 decimals of precision. It's a limitation of the floating-point format itself. Likewise, reading many more digits than that and trying to add them to the float will cause problem exactly for the same reason. Another common format is double-precision (64-bit) floats, which can have up to 17 decimals of precision.
Mollymews Posted July 19, 2020 Posted July 19, 2020 (edited) i would quite like it if LSL got a 64-bit integer type. I would like it even more if we got unsigned and signed as well like integer = signed 32 bits cardinal = unsigned 32 bits integer64 = signed 64 bits cardinal64 = unsigned 64 bits and a float64 Edited July 19, 2020 by Mollymews 64 1
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now