square root?

BlitzMax Forums/BlitzMax Programming/square root?

slenkar(Posted 2008) [#1]
Im trying to make a 2d distance function
how do I do square root in blitzmax?

thanks


klepto2(Posted 2008) [#2]
sqr(x) ?


Czar Flavius(Posted 2008) [#3]
x^(1/n) where n is the root you wish to find.


GfK(Posted 2008) [#4]
sqr(x) is simpler.

x^(1/n) is faster (so use this if you need a lot of square roots).


fredborg(Posted 2008) [#5]
x^(1/n) is faster (so use this if you need a lot of square roots).
no


GfK(Posted 2008) [#6]
no, what?

using sqr(x) for 10 million iterations: 664ms
using x^(1/n) for 10 million iterations: 82ms.

OK, that's a lot of iterations but the bottom line is, x^(1/n) is eight times faster than Sqr(x).


Brucey(Posted 2008) [#7]
If you don't need the actual distance, but just need to know if A is beyond (or before) a certain distance, you don't even need to calculate the square root...


Gabriel(Posted 2008) [#8]
x^(1/n) where n is the root you wish to find.

Additionally, where N is a float or a double, otherwise you'll do an integer division and get one for the result no matter what you put in.


fredborg(Posted 2008) [#9]
SuperStrict

Local y:Float

Local a:Int = MilliSecs()
For Local i:Float = 0.0 To 1000000.0 Step 1.0
	y = Sqr(i)
Next
a = MilliSecs()-a

Local b:Int = MilliSecs()
For Local i:Float = 0.0 To 1000000.0 Step 1.0
	y = i^0.5
Next
b = MilliSecs()-b

Print a+" vs "+b



ziggy(Posted 2008) [#10]
sqr is a lot faster than^1/n since version 1.24 of Max.


johnnyfreak(Posted 2008) [#11]
why don't use lookup tables

SuperStrict

Local y:Float

Local a:Int = MilliSecs()
For Local i:Float = 0.0 To 1000000.0 Step 1.0
	y = Sqr(i)
Next
a = MilliSecs()-a

Local b:Int = MilliSecs()
For Local i:Float = 0.0 To 1000000.0 Step 1.0
	y = i^0.5
Next
b = MilliSecs()-b

Local arr:Float[1000000]

For Local i:Int = 0 To 1000000 Step 1
	arr[i] = Sqr(i)
Next

Local c:Int = MilliSecs()
For Local i:Float = 0.0 To 1000000.0 Step 1.0
	y = arr[i]
Next
c = MilliSecs() - c

Print a+" vs "+b + " vs " + c



GfK(Posted 2008) [#12]
sqr is a lot faster than^1/n since version 1.24 of Max.
I'm using 1.28 and Sqr() is a lot slower. Unless there's something available via subversion that I don't have.


tonyg(Posted 2008) [#13]
Fredborg's code came up with the same results on my system (e.g. sqr faster than ^1/n) and I am 1.28.
@GFK, what are your results with Fredborg's code and what code are you using?


ImaginaryHuman(Posted 2008) [#14]
You don't actually have to do a square root at all, all it will do is scale the number. If you don't specifically need it to be in a certain range then just use the pre-square-rooted version of the result.


Brucey(Posted 2008) [#15]
It's like an echo in here :-p


WendellM(Posted 2008) [#16]
It's like an echo in here :-p

You gave the positive version of the answer while ImaginaryHuman gave the negative one. Both are needed since it's a square root question... :)


Czar Flavius(Posted 2008) [#17]
I see I have sparked contraversy! It's my birthday today too. Yay.


Jim Teeuwen(Posted 2008) [#18]
sqr() is consistently ~80% faster than the manual approach. Using 1.28 here.


Czar Flavius(Posted 2008) [#19]
sqr(x) uses C code, which is probably why it is faster.


xlsior(Posted 2008) [#20]
29 vs 132

However -- I'm curious if there is any difference on AMD vs. Intel.
(Mine was using Intel Core 2 Duo)


REDi(Posted 2008) [#21]
22 vs 190

On my monolithic AMD


Czar Flavius(Posted 2008) [#22]
It really was my birthday.


Jim Teeuwen(Posted 2008) [#23]
grats :p


Shambler(Posted 2008) [#24]
Happy birthday Czar. ;)


Kistjes(Posted 2008) [#25]
68 vs 187

XPS M1710 Intel Centrino Duo

O, and Czar: Happy Birthday!


TomToad(Posted 2008) [#26]
62 vs 162 debug
10 vs 105 release

AMD Athalon 64 X2 Dual core Processor 5000+ 2.60 Ghz

BMax v1.28


Brucey(Posted 2008) [#27]
hmm...

34 vs 69

Intel Core 2 Duo, OSX 10.5.3

Probably a lot to do with the compiler/settings too, I imagine.


unlikely(Posted 2008) [#28]
Interesting...

Consistently ~ 28 vs 67 Release
~ 79 vs 116 Debug

Intel Core Duo, OS X 10.5.2