Jump to content

Is there a way to cast a string calculation such as "4*PI/2" to a float?


VirtualKitten
 Share

You are about to reply to a thread that has been inactive for 1445 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

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 by VirtualKitten
NA
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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:

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 by Wulfie Reanimator
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Mollymews
64
  • Like 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1445 days.

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
 Share

×
×
  • Create New...