Jump to content


Photo

Using the "real" function


  • Please log in to reply
7 replies to this topic

#1 Elroy

Elroy

    GMC Member

  • New Member
  • 5 posts
  • Version:GM8

Posted 06 July 2012 - 05:02 PM

I have a couple of questions pertaining to the use of the "real" string handling function in GML.
First of all, whenever I try to get it to actually simplify a string (e.g. 2*2), it gives me "error in function real()".
Here is the code I use (there is a good chance that this is a user error):

{
calc=get_string("","")
show_message(real(calc))
}

My second question is if there is any way to get the "real" function to be more accurate than two decimal places, I am trying to write a program where accuracy to many more decimal places would greatly increase its functionality.

If it makes any difference I am using the free version of GM8.
  • 0

#2 Jobo

Jobo

    No neurons left today

  • GMC Member
  • 2404 posts
  • Version:GM:Studio

Posted 06 July 2012 - 05:10 PM

real() parses a string into any number that the string consists of.
Therefore, the string you want to parse must be a number in a string, not a calculation.
  • 0

#3 Johnjwj

Johnjwj

    Question.

  • GMC Member
  • 111 posts
  • Version:GM8

Posted 06 July 2012 - 06:12 PM

To directly get a integer, you should use the get_integer() function instead. Also, show_message() can only display strings, meaning that you will have to convert any real values using string(). My guess is that doing this would solve your problem regarding the decimals.

Unfortunately, coming up with a way to enter calculations, like 2*2, will require further string-handling. I would divide the string using the functions included in the manual and go from there.

Edited by Johnjwj, 06 July 2012 - 06:15 PM.

  • 0

#4 Jobo

Jobo

    No neurons left today

  • GMC Member
  • 2404 posts
  • Version:GM:Studio

Posted 06 July 2012 - 08:19 PM

Also, show_message() can only display strings, meaning that you will have to convert any real values using string().

This is true if you try to put an integer into a string, but show_message() can display integers only - it can display anything, but it must be of the same type throughout.
  • 0

#5 Johnjwj

Johnjwj

    Question.

  • GMC Member
  • 111 posts
  • Version:GM8

Posted 06 July 2012 - 09:51 PM

Also, show_message() can only display strings, meaning that you will have to convert any real values using string().

This is true if you try to put an integer into a string, but show_message() can display integers only - it can display anything, but it must be of the same type throughout.

My bad. The same data type through-out is important, yes.
  • 0

#6 dannyjenn

dannyjenn

    GMC Member

  • GMC Member
  • 2064 posts
  • Version:Mac

Posted 06 July 2012 - 11:34 PM

To make the 2*2 part work, you either need to use execute_string (which is usually a bad idea) or write your own script to parse the string and make the calculations for you.
  • 0

#7 Jobo

Jobo

    No neurons left today

  • GMC Member
  • 2404 posts
  • Version:GM:Studio

Posted 06 July 2012 - 11:43 PM

execute_string

Security issues, function deprecated in Studio.
  • 0

#8 dannyjenn

dannyjenn

    GMC Member

  • GMC Member
  • 2064 posts
  • Version:Mac

Posted 07 July 2012 - 12:24 AM

execute_string

Security issues, function deprecated in Studio.

As I said, it's usually a bad idea. Even if it had no security issues at all, it's a slow function as well as very difficult to debug (if you use it a lot) and in general it's bad programming practice since people use it out of laziness to do something like this instead of writing an actual script to parse the string.

Still, if he just wants a quick and easy way then he could use it. It does work, as long as he's not using Studio (in which case it won't work, but he specifically said he's not using Studio)
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users