Square root?

Topics regarding Scripting with Reality Factory
Post Reply
User avatar
Grok
Posts: 137
Joined: Sat May 27, 2017 9:20 pm

Square root?

Post by Grok » Fri Jan 05, 2018 9:03 pm

Are there any way to get the square root of a value in a script?

I've tried

Code: Select all

dist = Math("sqrt", float Value);	

that I saw in an older post, but it did not work for me.

I want to get the distance between two points on the screen. It is not strictly necessary to solve the problem I'm working on now, but it could be a useful tool for the future.
:)

Allanon
Posts: 493
Joined: Mon Aug 29, 2005 8:23 am

Re: Square root?

Post by Allanon » Sat Jan 06, 2018 12:03 am

value is a floating point number or variable:

Code: Select all

dist = Math.sqrt(value)
Look up the Math class in the docs for more information on math functions.

User avatar
Grok
Posts: 137
Joined: Sat May 27, 2017 9:20 pm

Re: Square root?

Post by Grok » Sat Jan 06, 2018 11:27 am

Allanon wrote:value is a floating point number or variable:

Code: Select all

dist = Math.sqrt(value)
Look up the Math class in the docs for more information on math functions.


Thank you for a quick answer.

Unfortunately I don't get this one to work either, and don't seem to find any descriptions of math functions anywhere.

I am using the 76.1 version of RF (latest stable). Is this possibly something implemented in the 78.0 version (beta)?

Allanon
Posts: 493
Joined: Mon Aug 29, 2005 8:23 am

Re: Square root?

Post by Allanon » Sat Jan 06, 2018 8:56 pm

Grok wrote:
Allanon wrote:Unfortunately I don't get this one to work either, and don't seem to find any descriptions of math functions anywhere.

I am using the 76.1 version of RF (latest stable). Is this possibly something implemented in the 78.0 version (beta)?
That code was from the Math Class in version 78.0, I think you will need to write your own square root function using the Simkin Script Syntax for earlier versions of RF. Maybe something like this:

Code: Select all

abs[(value)
{
    if (value < 0)
    {
        return value * -1
    }
    return value
}]

sqrt[(value)
{
    last_guess = value / 2.0
    while true
    {
        guess = (last_guess + (value / last_guess)) / 2
        if (abs(guess - last_guess) < .00001)
        {
             return guess
        }
        last_guess = guess
    }
}]
Note: This code was written off the top of my head and never tested so it probably has some errors.

User avatar
Grok
Posts: 137
Joined: Sat May 27, 2017 9:20 pm

Re: Square root?

Post by Grok » Sat Jan 06, 2018 9:19 pm

Allanon wrote: ---
That code was from the Math Class in version 78.0, I think you will need to write your own square root function using the Simkin Script Syntax for earlier versions of RF. Maybe something like this:
---
OK.

I could probably manage that if necessary.

Or maybe I should just give 78.0 a try :)

Thank you for your input. I appreciate it.

Post Reply