# Square Root Calculator (Read 2791 times)

Started by ink, March 28, 2017, 05:20:47 pm

#### ink

##### Square Root Calculator
#1  March 28, 2017, 05:20:47 pm
• inktrebuchet
I made this when I couldn't find a way to get the square root of a number in MUGEN(during play) for a piece of code that uses Pythagorean Theorem. I'm new to MUGEN coding so if there is an easier way please let me know!

-Edit-
Just found this... which is probably a better bet than the code below. http://mugenguild.com/forum/topics/square-roots-and-general-roots-mugen--6588.msg64497.html#msg64497

My notes:

Code:
`;==========================================================================;Inktrebuchet ;Square Root Calculator 3/28/2017                    ;========================================================================== ;==========================================================================;Goal:;-Find Square Root;;notes:;Number you want square root of goes in var(1);Square root of number is var(7);;-------------------------------------------------------------------------- ;==========================================================================;                AI Enemy Combo Count System Helper Initialization;==========================================================================; Variable usage:; This is a record of the variables that are used;; var(1) - Number you want to find Square Root of; var(2) - ; var(3) - ; var(4) - ; var(5) - ; var(6) - ; var(7) - Square Root of number;--------------------------------------------------------------------------;===========================================================;Find Square Root;===========================================================[state n] ; area (value to find square root of)type = varsettrigger1 = 1v = 1value = 25;persistent = 0ignorehitpause = 1[state n] ; add 1 every 2 ticks. and side 4type = varaddtrigger1 = gametime%2=0trigger1 = var(3) <= var(1)v = 2value = 1;persistent = 0ignorehitpause = 1[state n] ; areatype = varaddtrigger1 = var(3) <= var(1) ;has reached areav = 3value = var(2);persistent = 0ignorehitpause = 1;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[state n] ; side 3 and 2type = varsettrigger1 = var(3) >= var(1) ;has reached areav = 4value = var(2)-1;persistent = 0ignorehitpause = 1[state n] ; side 1type = varsettrigger1 = var(3) >= var(1) ;has reached areav = 5value = var(2)-2;persistent = 0ignorehitpause = 1;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>[state n] ; perimetertype = varsettrigger1 = var(3) >= var(1) ;has reached areav = 6value = var(5)+var(4)+var(4)+var(2) + 4 ; All sides + number of sides.;persistent = 0ignorehitpause = 1[state n] ; square root of area*(var(1))type = varsettrigger1 = var(3) >= var(1) ;has reached areav = 7value = var(6)/4 ; perimeter divided by number of sides.;persistent = 0ignorehitpause = 1`
Last Edit: March 28, 2017, 05:38:37 pm by ink

#### Vans

##### Re: Square Root Calculator
#2  March 28, 2017, 06:43:07 pm
x ** 1/2 is the square root of x.

There's many methods of calculating a square root: trigonometric, exponential, approximating via series, etc. If you ever make use of the exponential or trigonometric functions while you calculate it, you will end up with the same result.

I advice against wasting variables for doing this.
Last Edit: March 28, 2017, 06:50:09 pm by Vans

#### ink

##### Re: Square Root Calculator
#3  March 28, 2017, 06:58:28 pm
• inktrebuchet

#### Vans

##### Re: Square Root Calculator
#4  March 28, 2017, 07:04:04 pm
For a fun exercise, I came up with another way of calculating a square root after reading this thread. This is based on an old geometric construction by Euclides (I believe).

sqrt(x) = ((x+1.0)/2.0)*sin(acos((x-1.0)/(x+1.0)))

x should be written in float form.

#### ink

##### Re: Square Root Calculator
#5  March 28, 2017, 07:16:57 pm
• inktrebuchet
A re-discovery on my part then, I just made this up at my desk after no luck searching for solutions online. haha

I am very impressed to see your formula do the same work! Thank you for sharing it here.