square root?

BlitzMax Forums/BlitzMax Programming/square root?

Im trying to make a 2d distance function
how do I do square root in blitzmax?

thanks

sqr(x) ?

x^(1/n) where n is the root you wish to find.

sqr(x) is simpler.

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

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

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).

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...

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.

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


sqr is a lot faster than^1/n since version 1.24 of Max.

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


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.

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?

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.

It's like an echo in here :-p

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... :)

I see I have sparked contraversy! It's my birthday today too. Yay.

sqr() is consistently ~80% faster than the manual approach. Using 1.28 here.

sqr(x) uses C code, which is probably why it is faster.

29 vs 132

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

22 vs 190

On my monolithic AMD

It really was my birthday.

grats :p

Happy birthday Czar. ;)

68 vs 187

XPS M1710 Intel Centrino Duo

O, and Czar: Happy Birthday!

62 vs 162 debug
10 vs 105 release

AMD Athalon 64 X2 Dual core Processor 5000+ 2.60 Ghz

BMax v1.28

hmm...

34 vs 69

Intel Core 2 Duo, OSX 10.5.3

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

Interesting...

Consistently ~ 28 vs 67 Release
~ 79 vs 116 Debug

Intel Core Duo, OS X 10.5.2