- Joined
- Jun 21, 2012
- Messages
- 431
Hi guys!
I wanted to know if there an alternative to
I wanted to know if there an alternative to
SquareRoot
?SquareRoot
?So... The formula to apply this in jass is?
If you're doing a lot of sqrt() calls in a short time (physic engine, for example) it could potentially be beneficial to optimize a lot and use something like the fast inverse sqrt trick DracoL1ch posted. However, for most WC3 code sqrt() is fine and if it really matters you can achieve an acceptable increase in speed/efficiency by working with the square of the number you want to compute the sqrt of exactly as Wietlol said.What is the problem with sqrt()? Is there a specific situation which should not use it?
Exactly. What Wietlol explained is a nice trick to avoid the sqrt function call, but it only should be used privatly by a system, and only works if you have a reference value, so like MaxDistance for example.The answers to these questions depend a lot on why you don't want to use sqrt() in the first place.
The problem is the processing timer as wietlol said, but neither is it a serious problem. I just wanted to know if there is another way to calculate the sqrt().What is the problem with sqrt? Is there a specific situation which should not use it?
You can post a test code?in my tests fast inverse square root was slower in jass.
You can post a test code?
function rsqrt takes real r returns real
local real x = r * 0.5
set r = i2r(0x5f3759df - ( r2i(r) / 2 ))
return r * ( 1.5 - ( x * r * r ) )
endfunction
Typecasts like here or here.I assume that i2r() and r2i() are I2R() and R2I().
Or are they typecasts (without changing the bytedata behind the screen)?
//# +nosemanticerror
private function indexToReal takes integer i returns real
return i
endfunction
//# +nosemanticerror
private function realToIndex takes real i returns integer
return i
endfunction
private function cleanR takes real r returns real
return r
return 0.0 // dont let jh inlines this
endfunction
private function cleanI takes integer i returns integer
return i
return 0 // dont let jh inlines this
endfunction
function i2r takes integer i returns real
return cleanR(indexToReal(i)) // this gets inlined
endfunction
function r2i takes real i returns integer
return cleanI(realToIndex(i)) // this gets inlined
endfunction
function rsqrt takes real r returns real
local real x = r * 0.5
set r = i2r(0x5f3759df - ( r2i(r) / 2 ))
return r * ( 1.5 - ( x * r * r ) )
endfunction
And you're confident it's the most efficient with 2 casts?
SquareRoot
was faster. I just posted it because op was interested in it.Thanks... you say that it is slow thatJASS:function rsqrt takes real r returns real local real x = r * 0.5 set r = i2r(0x5f3759df - ( r2i(r) / 2 )) return r * ( 1.5 - ( x * r * r ) ) endfunction
Go wild. Needs an r2i /i2r return-bug function of some sort.
The performance killer here is ofc the r2i/i2r calls.
SquareRoot
however I find it interesting.If you want to avoid it because of its required processing time, you could do the calculations on the squared values instead.
Aka:
MaxDistance = 600
DistanceX = 400
DistanceY = 300
IsInRange = MaxDistance >= sqrt(DistanceX*DistanceX + DistanceY*DistanceY)
Or
IsInRange = MaxDistance*MaxDistance >= DistanceX*DistanceX + DistanceY*DistanceY
I agree with you.I think Wietlol's answer is the solution.