Problem with power...
BlitzMax Forums/BlitzMax Programming/Problem with power...
| ||
Hello, Can someone explain me that : SuperStrict Function Power:Long(a:Long , b:Long) Return a ^ b EndFunction Print String(Long(Long(4)^Long(3))) Print String(Power(4,3)) why my Power function returns me 63 ? TIA McFox |
| ||
4^3 is a float operation that did not quite make 64. try it out with return(a^b)*10000. the 63.9999 float was casted to 63 which is not mathematically correct but apparently is the standard way of doing things. Don't know why the other way produces a 64 but there is only a small difference b/w 64.000001 and 63.9999 :) |
| ||
Could do something like this:Function Power:Long( a:Long, b:Long ) Local Round:Double( in:Double ) out:Double = a^b If Abs(out Mod 1) >= .5 Then round = Ceil Else round = Floor EndIf Return Round(out) End Function |
| ||
Or you can just do return Long(out+.5) |