The Mugen Fighters Guild

Help => M.U.G.E.N Development Help => Code Library => Topic started by: inktrebuchet on March 28, 2017, 05:20:47 pm

Title: Square Root Calculator
Post by: inktrebuchet on March 28, 2017, 05:20:47 pm
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 (http://mugenguild.com/forum/topics/square-roots-and-general-roots-mugen--6588.msg64497.html#msg64497)

My notes:(https://www.facebook.com/images/emoji.php/v7/fd0/1/16/1f602.png)
(http://i236.photobucket.com/albums/ff271/inktrebuchet/rooooot_zpswbrg0vbz.png)

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 = varset
trigger1 = 1
v = 1
value = 25
;persistent = 0
ignorehitpause = 1

[state n] ; add 1 every 2 ticks. and side 4
type = varadd
trigger1 = gametime%2=0
trigger1 = var(3) <= var(1)
v = 2
value = 1
;persistent = 0
ignorehitpause = 1

[state n] ; area
type = varadd
trigger1 = var(3) <= var(1) ;has reached area
v = 3
value = var(2)
;persistent = 0
ignorehitpause = 1

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

[state n] ; side 3 and 2
type = varset
trigger1 = var(3) >= var(1) ;has reached area
v = 4
value = var(2)-1
;persistent = 0
ignorehitpause = 1

[state n] ; side 1
type = varset
trigger1 = var(3) >= var(1) ;has reached area
v = 5
value = var(2)-2
;persistent = 0
ignorehitpause = 1

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

[state n] ; perimeter
type = varset
trigger1 = var(3) >= var(1) ;has reached area
v = 6
value = var(5)+var(4)+var(4)+var(2) + 4 ; All sides + number of sides.
;persistent = 0
ignorehitpause = 1

[state n] ; square root of area*(var(1))
type = varset
trigger1 = var(3) >= var(1) ;has reached area
v = 7
value = var(6)/4 ; perimeter divided by number of sides.
;persistent = 0
ignorehitpause = 1

Title: Re: Square Root Calculator
Post by: Vans on 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.
Title: Re: Square Root Calculator
Post by: inktrebuchet on March 28, 2017, 06:58:28 pm
Thank you for sharing that one! I just found http://mugenguild.com/forum/topics/square-roots-and-general-roots-mugen--6588.msg64497.html#msg64497 recently too.

My ignorance of mugen code fueled this one. It was fun anyways.
Hopefully this thread will help anyone trying to find square root functions, find them a little easier.
Title: Re: Square Root Calculator
Post by: Vans on 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.
Title: Re: Square Root Calculator
Post by: inktrebuchet on March 28, 2017, 07:16:57 pm
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.