PDA

View Full Version : calculation failure?



largo_winch
19-07-2011, 12:05
hello. anybody can check this very little code example? the result for "less -10" I don't understand. thanks for testing.



' Empty GUI script created on 07-15-2011 11:13:22 by (ThinAIR)
Uses "console"
Function TBMain() As Long
MsgBox 0, alittle(1)
End Function

Function alittle (less As DWord) As Long
Local a As Integer
Local b As Integer less = -10 ' ?
'less = 10 ' ok
a = 2 b = 200
Function = a*b+less
End Function
' a) result for less = 10 : 410 ok
' b) result for less = -10 : 4294967686 ?

there are few people they are really programming with thinbasic at the moment. background could be summer time? ;)

bye, largo

Charles Pegge
19-07-2011, 13:46
Hi Largo,

You have made "less" an unsigned integer by using "Dword" as its type.
Assigning a negative value will be interpreted as a twos complement value thus:

-1 is &HFFFFFFFF
-2 is &HFFFFFFFE
-10 is &HFFFFFFF6

Try using "Long" instead of "Dword"

Charles