Jump to content

why -n is ture?


Recommended Posts

Why negative values evaluate as true in the following:

if -1 then
;kill self
 endif

Was this always the case? Or did the behavior change at some point?

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

Why negative values evaluate as true in the following:

if -1 then
;kill self
 endif

Was this always the case? Or did the behavior change at some point?

It returns True because it is True

$iVal = -1
If $iVal = -1 Then
   MsgBox(0, "Results", "It's True")
Else
   MsgBox(0, "Results", "It's False")
EndIf

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

I understand that AutoIt treats -1 as true... I discovered it the hard way. But it doesn't make sense; negative values are almost always an error in all languages. What is the logic behind this behavior?

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

I understand that AutoIt treats -1 as true... I discovered it the hard way. But it doesn't make sense; negative values are almost always an error in all languages. What is the logic behind this behavior?

Logic is this:

-1 is not nothing

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

  • Developers

I understand that AutoIt treats -1 as true... I discovered it the hard way. But it doesn't make sense; negative values are almost always an error in all languages. What is the logic behind this behavior?

You have 2 options being True or False. 0 = False, anything else is True.

Doesn't that make sense?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

I understand that AutoIt treats -1 as true... I discovered it the hard way. But it doesn't make sense; negative values are almost always an error in all languages. What is the logic behind this behavior?

-1 does equal -1 therefore it is true. What can be simpler than that? As has been stated Only 0 or an empty string ("") are false when looking at the value of variables. I think what you want would be a new function like Boolean() where only positive values resolve to True

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Logic is this:

-1 is not nothing

I see... thanks

[Edit] Thanks everyone...

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

Actually if you want another test then try this one and see the difference.

$iVal = -1
If NOT $iVal Then
   MsgBox(0, "Results", "It's False")
Else
   MsgBox(0, "Results", "It's True")
EndIf

That's why the keyword NOT is there

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Actually if you want another test then try this one and see the difference.

$iVal = -1
 If NOT $iVal Then
    MsgBox(0, "Results", "It's False")
 Else
    MsgBox(0, "Results", "It's True")
 EndIf

That's why the keyword NOT is there

Thanks

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

A value of 'True' typically represents -1 or 1 in a language. -1 is the same as all bits set.

Here's what MSDN says:

The encoding for logical FALSE is all bits set to zero (0x0), and the encoding for logical TRUE is all bits set to 1 or 0xFFFF

http://msdn.microsoft.com/en-us/library/cc...0(PROT.10).aspx

Link to comment
Share on other sites

I'm not sure how other processors work, but for the Intel chipsets, there are no built-in 'bool' types/operations or 'If variable' logic tests, unless you count the 'cx' register and its 'jcxz' instruction - but that itself requires the register to first contain the data you'd like to test. (which in the case of most variables requires a 'mov' to cx first). There are conditional jumps\operations based on a 'zf' (zero-flag), but these must be preceded by a compare or 'test' operation. To emulate 'If' tests in higher-level languages, you would either use 'test' to test if certain/all bits are set, or compare a value to zero (which logically is the same as 'If x=0' or 'If x<>0'). Also, using 'not' would be the equivalent of changing a value's status from true to false (no-bits set to all-bits set and vice versa).

So basically, no - the processor doesn't run the same way as 'If variable' logic statements work. That is something you will only find in higher-level languages, which themselves can use any set of machine instructions they like, though the easiest would be 'cmp [var],0'.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...