YesNoOk
avatar

Square Root Calculator (Read 1458 times)

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

ink

Square Root Calculator
#1  March 28, 2017, 05:20:47 pm
  • ****
  • inktrebuchet
    • USA
    • s236.photobucket.com/albums/ff271/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 = 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

Last Edit: March 28, 2017, 05:38:37 pm by ink
Re: Square Root Calculator
#2  March 28, 2017, 06:43:07 pm
  • ****
    • Japan
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
    • USA
    • s236.photobucket.com/albums/ff271/inktrebuchet/?
Re: Square Root Calculator
#4  March 28, 2017, 07:04:04 pm
  • ****
    • Japan
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
    • USA
    • s236.photobucket.com/albums/ff271/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.