Re: Mulitple conditionals [message #64974] |
Sat, 07 February 2009 04:48 |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Bernhard Reinhardt writes:
> I guess I can not shock you IDL old stagers with this but for an IDL
> newbee like me this seems quite strange:
>
> IDL> if 1 eq (1 or 2) then print, "true" else print, "false"
> false
> IDL> if "a" eq ("a" or "b") then print, "true" else print, "false"
> true
>
> Maybe thats the IDL-way of punishing people to addle to type the long
> version:
>
> IDL> if ("a" eq "a" or "a" eq "b") then print, "true" else print,"false"
> true
> IDL> if (1 eq 1 or 1 eq 2) then print, "true" else print, "false"
> true
>
> Can anyone explain the logic behind this behavoir to me?
I've given up "explaining" a great deal of IDL's behavior,
but this one is fairly simple. IDL does "bitwise" comparisons
between values. So...
IDL> print, binary(1 or 2)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1
IDL> print, binary(1)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
Clearly, these two bit patterns are different, so the answer
is "false":
IDL> if 1 eq (1 or 2) then print, "true" else print, "false"
false
I'll leave it to someone else to explain why strings
are handled differently. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|