How to find the sign of a value?

Blitz3D Forums/Blitz3D Programming/How to find the sign of a value?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

modified WolRons little test code:

variable% = 1


;first routine roughly 697 milliseconds
;WolRons routine

starttime = MilliSecs()
For iter = 1 To 100000000
If Abs(variable) = variable
  ;variable is positive
Else
  ;variable is negative
EndIf
Next
Print MilliSecs() - starttime


;second routine about 289 milliseconds
;Jeremys routine

starttime = MilliSecs()
For iter = 1 To 100000000
If variable > 0
  ;variable is positive
Else
  ;variable is negative
EndIf
Next
Print MilliSecs() - starttime


;third routine about 756 milliseconds
;Micheals

starttime = MilliSecs()
For iter = 1 To 100000000
If Sgn(variable)
  ;variable is positive
Else
  ;variable is negative
EndIf
Next
Print MilliSecs() - starttime


;fourth routine about 253 milliseconds
;Mine ... note: the IF/ELSE results are switched

flagger=%10000000000000000000000000000000

starttime = MilliSecs()
For iter = 1 To 100000000
If (variable And flagger)
  ;variable is negative
Else
  ;variable is positive
EndIf
Next
Print MilliSecs() - starttime


WaitKey()
End


For the most part the ABS() and SGN() routines take more time because of the function call...pushing/popping onto the stack and all that...Jeremy's routine works very well, even would work with floats...All I'm doing is a bitwise check of the bit supposedly containing the integer pos/neg flag...it's a really old trick that is only usefull for integers and won't work with floats.