Sign in to follow this  
Followers 0
supersonic

Strange behaviour using Abs()

4 posts in this topic

#1 ·  Posted (edited)

Hi -

If running this code snippet Abs() "converts" Int32 into Double:

Local $iSC_Countdown = -100

MsgBox(0, $iSC_Countdown, VarGetType($iSC_Countdown))

$iSC_Countdown = Abs($iSC_Countdown)

MsgBox(0, $iSC_Countdown, VarGetType($iSC_Countdown))

Why? Is it a bug?

Greets,
-supersonic.

EDIT:

Testet with 3.3.12 and 3.3.15.

EDIT (#2):

Abs() doesn't preserve the variable type if input is integer. If it is behaviour by design maybe it is worth to be mentioned in help file?

Edited by supersonic

Share this post


Link to post
Share on other sites



:)

Local $iSC_Countdown = -100
$iSC_Countdown = Abs($iSC_Countdown)
MsgBox(0, $iSC_Countdown, IsInt($iSC_Countdown))

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Interesting observation and behavior.  I'm curious what (if any) scenarios you have run into that this would cause issues (me thinks lower-level RAM allocation perhaps)? 

edit: Same behavior observed when using Abs on a positive Int

Local $iSC_Countdown = 100
MsgBox(0, $iSC_Countdown, VarGetType($iSC_Countdown))

$iSC_Countdown = Abs($iSC_Countdown)
MsgBox(0, $iSC_Countdown, VarGetType($iSC_Countdown))


;simple workaround
Func _Abs($iValue)
    If $iValue < 0 Then $iValue *= -1
    Return $iValue
EndFunc

$iSC_Countdown = -200
MsgBox(0, $iSC_Countdown, VarGetType($iSC_Countdown))

$iSC_Countdown = _Abs($iSC_Countdown)
MsgBox(0, $iSC_Countdown, VarGetType($iSC_Countdown))

 

Edited by spudw2k

Share this post


Link to post
Share on other sites

I observed this behaviour incidently. It would be very helpful to know if this is an unmeant inconsistency. I could live with it but then it should be documented in help file. Perhaps a dev could enlight us? :)

A workaround can be helpful - but only in special cases. Variable types should be preserved (where logical).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0