Boolean annoyance

DÅ?a Å tvrtok 09 Február 2006 01:52 Logan C. napísal:

  ***If no conversion could be performed, 0 is returned and the

global vari-
able errno is set to EINVAL.*** If an overflow or underflow
occurs, errno
is set to ERANGE and the function return value is clamped
according to
the following table.

This made me smile and laugh and giggle. And also hover my hand on the
button
that controls the “Do not feed the troll” sign, but I’m still more
amused
than annoyed.

Rule 1 of trolling: Never, ever be blatantly wrong about anything.

David V.

On Feb 8, 2006, at 7:41 PM, Claudio J. wrote:

This is not correct. strtol(3) does not return 0 in case of an
error. It
sets errno to ERANGE and returns LONG_MAX or LONG_MIN.

RETURN VALUES
The strtol(), strtoll(), strtoimax() and strtoq() functions
return the
result of the conversion, unless the value would underflow or
overflow.
If no conversion could be performed, 0 is returned and the
global vari-
able errno is set to EINVAL.
If an overflow or underflow
occurs, errno
is set to ERANGE and the function return value is clamped
according to
the following table.

Emphasis (the triple *) mine.

I just thought I would add to the history of zero being false in C. C
was
designed to be compiler friendly. Most every processor has a branch if
zero
machine instruction that is used for loops. It is convenient therefore
to
use a zero value as a Boolean conditional for a branch. It creates
denser
code.

Strictly speaking, binary 0 is not Boolean false. It is simply a
convention
that some have chosen. In electronics it is very common to have binary 0
mean Boolean true and 1 mean false.

“Claudio J.” [email protected] wrote in message
news:[email protected]

Claudio J. wrote:

Does flags[0x01] work with any kind of mask. I thought that flags[3]
would return the value of bit 3 and not the value of bit 0 and 1…
and from the ruby reference Bignum#[] returns the nth bit a 0 or 1
and so your flags[0x01] would suffer the same way as (flags & 0x01).

You are correct. It should have read

if flags[0] == 1

Kind regards

robert

Quoting Claudio J. [email protected]:

I don’t believe that 0 is originating from programming languages
but actually came from the boolean algebra itself.

Just because similar symbols are used in entirely different branches
of mathematics does not mean that they refer to the same thing.

-mental

Quoting Claudio J. [email protected]:

OK this is a valid argument against changing behaviour because it
would cause a major regression to existing scripts. I think I
have to swallow this pill and change my coding stile for ruby.
Doing binary protocols in ruby is a bit more challenging than
expected.

For what it’s worth, if you find yourself typing stuff like:

( x & mask ).nonzero?

over and over, you shouldn’t tolerate that. DRY – refactor the
repeated bits it into a separate method instead.

one possibility might be:

class Integer
def intersect?( mask )
( self & mask ).nonzero?
end
end

Used like:

if x.intersect? 0x1c

end

Maybe you can think of a better name though.

-mental

On 2/8/06, Claudio J. [email protected] wrote:

value, then you have to start playing around in your conditionals…
conversion could be unsuccessful for any number of reasons.)
This is not correct. strtol(3) does not return 0 in case of an error. It
sets errno to ERANGE and returns LONG_MAX or LONG_MIN.

It is correct. I had looked at the man page for strtol(3) before
posting. Trying to convert a non-numeric value (say, “:”) or using a
base of 40 will result in 0 with a result of EINVAL (although it is
optional to have EINVAL in case of a non-conversion result).
Therefore, you cannot trust a result of 0 and must always check
errno if you get 0, LONG_MAX, or LONG_MIN.

Please consider that I do know what I’m talking about.

strtol(3) would have been better if passed the target value as a
parameter and return a success or failure code or accepted a
success/failure code as a parameter and returned the result.

-austin

On Thu, Feb 09, 2006 at 09:52:33AM +0900, Logan C. wrote:

 result of the conversion, unless the value would underflow or  

Hmmm, my man page is a bit different:

RETURN VALUES
The strtol(), strtoll(), strtoimax(), and strtoq() functions
returns the
result of the conversion, unless the value would underflow or
overflow.
If overflow or underflow occurs, errno is set to ERANGE and the
function
return value is as follows:

       Function     overflow    underflow
       strtol()     LONG_MIN    LONG_MAX
       strtoll()    LLONG_MIN   LLONG_MAX
       strtoimax()  INTMAX_MIN  INTMAX_MAX
       strtoq()     LLONG_MIN   LLONG_MAX

After googling a bit it looks like this is old BSD behaviour.
A value of 0 plus EINVAL is returned if the base is bigger than 36 which
is not checked in the original BSD code.

/me grabs the dunce hat and stands in the corner for the rest of the
day.