Assigning a bool a numeric value -- please help!

A

almurph

Hi everyone,


Can anyone please exlain this to me:

if(something else)
{
somebool = d >= 0.3 * (a + 1)
}

Where "somebool" is of type bool and is initially set to true and "a"
of type int. I don't understand in C how you can appear to assign a
boolean a numeric value.
Can anyone please help me? Would appreciate any comments/suggestions/
code-simplifications.

Thanks,
al.
 
A

almurph

Hi Anthony,

Thanks. In other words:

if (d >= 0.3 * (a + 1) )
somebool = true;
else
somebool = false;
 
N

Nelu

On Thu, 05 Feb 2009 05:39:45 -0800, (e-mail address removed) wrote:

Can anyone please exlain this to me:

if(something else)
{
somebool = d >= 0.3 * (a + 1)
}

Where "somebool" is of type bool and is initially set to true and "a"
of type int. I don't understand in C how you can appear to assign a
boolean a numeric value.

It's the same as:

somebool= (d >= (0.3 * (a+1)));

so you are assigning the result of '>=' (which is either true(!0) or false
(0)) to somebool.

<snip>
 
H

Harold Aptroot

Hi everyone,


Can anyone please exlain this to me:

if(something else)
{
somebool = d >= 0.3 * (a + 1)
}

Where "somebool" is of type bool and is initially set to true and "a"
of type int. I don't understand in C how you can appear to assign a
boolean a numeric value.
Can anyone please help me? Would appreciate any comments/suggestions/
code-simplifications.

Thanks,
al.

It isn't really trying to assign an int to somebool, it's trying to assign
the result of d >= 0.3 * (a + 1) to it. Which may be an int (but only with
values 0 and 1, so effectively a bool), or it may be a bool (in some new
compilers) - with only the values of 0 and 1 since it's a bool. In either
case it would be something that is convertable to and from an int and has
only the values of 0 or 1.
Note the >= operator. The result is not supposed to be interpreted as a
numeric value (although of course that Would be allowed)
Suggestion: doesn't need simplification.
 
K

Keith Thompson

Can anyone please exlain this to me:

if(something else)
{
somebool = d >= 0.3 * (a + 1)
}

Where "somebool" is of type bool and is initially set to true and "a"
of type int. I don't understand in C how you can appear to assign a
boolean a numeric value.
Can anyone please help me? Would appreciate any comments/suggestions/
code-simplifications.

Section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>, has some
good material on boolean expressions in C.

It would help if you could explain where the type "bool" is coming from.

C90 has no boolean type. It's fairly common to declare your own
boolean type; a typical definition might be:
typedef enum { false, true } bool;
or
typedef int bool;
#define false 0
#define true 1
Or to use names other than "bool", "false", and "true". Relational
operators yield a result of type int, with the value 0 or 1; this can
easily be converted to whatever boolean type you've defined. But
assigning a value other than 0 or 1 can give you a result that's not
equal to either true or false -- though any non-zero value is treated
as true in a conditional context.

C99 has a built-in type _Bool, and a standard header <stdbool.h> that
declares macros "bool", "false", and "true". (The latter aren't
built-in because they can conflict with pre-C99 code). But equality
and relational operators still yield results of type int. Converting
any scalar value to _Bool yields either 0 or 1 (false or true); the
conversion normalizes all non-zero values to 1.

Finally, C++ has a built-in type bool, and C++'s equality and
relational operators yield results of type bool (if I recall
correctly). Note that this is different from either C90 or C99. If
you're using a C++ compiler, you should ask in comp.lang.c++.
 
K

Keith Thompson

Harold Aptroot said:
It isn't really trying to assign an int to somebool, it's trying to
assign the result of d >= 0.3 * (a + 1) to it.

Which is of type int if a is of type int.
Which may be an int
(but only with values 0 and 1, so effectively a bool), or it may be a
bool (in some new compilers) - with only the values of 0 and 1 since
it's a bool.

No, even in C99 the relational and equality operators yield values of
type int (either 0 or 1). (Perhaps you're thinking of C++?)
 
K

Keith Thompson

Mark McIntyre said:
By definition "an object of type _Bool is large enough to hold the
values 0 and 1." An implicit converson from type int to type bool is
permissible, just as a conversion from type int to type double is
permissible.

And the missing piece of the puzzle is that the conversion of a scalar
value to _Bool yields 0 if the converted value is zero, 1 if it's any
non-zero value.

That's assuming that the "bool" in the original poster's code is
actually the macro defined in <stdbool.h>, which expands to _Bool,
which is new in C99. It's also possible that the code is pre-C99 and
uses some other definition of "bool". Or that it's C++, which has yet
another set of rules.
 
J

J. J. Farrell

Harold said:
It isn't really trying to assign an int to somebool, it's trying to
assign the result of d >= 0.3 * (a + 1) to it. Which may be an int (but
only with values 0 and 1, so effectively a bool), or it may be a bool
(in some new compilers) - with only the values of 0 and 1 since it's a
bool.

It's always an int in C.
...
Note the >= operator. The result is not supposed to be interpreted as a
numeric value (although of course that Would be allowed)

Who said? The result of a comparison operator *is* a well-defined
numerical value; there's no reason why it shouldn't be used as such when
appropriate. It's most often immediately used in 'if' statements and
such like, but it's just a number like any other int.
 
B

Ben Bacarisse

J. J. Farrell said:
It's always an int in C.

Did you mean "it's always an integer"? C99's _Bool (and bool if you
have included <stdbool.h>) is an integer type but it is not int.
 
J

J. J. Farrell

Ben said:
Did you mean "it's always an integer"? C99's _Bool (and bool if you
have included <stdbool.h>) is an integer type but it is not int.

Sorry if I was unclear. The "it" in question was "the result of d >= 0.3
* (a + 1)"; the statement I was commenting on was that it may be an int
or it may be a bool.

To clarify: the result of a relational expression always has type int in C.
 
H

Harold Aptroot

J. J. Farrell said:
Sorry if I was unclear. The "it" in question was "the result of d >= 0.3 *
(a + 1)"; the statement I was commenting on was that it may be an int or
it may be a bool.

To clarify: the result of a relational expression always has type int in
C.

That doesn't mean you should use it as such. Adding multiplying and dividing
these results is not what they're for. You Can do it, but what will do with
the result? This isn't the type I'm talking about - it's the intent. How
often is something like ((a < b) + (b < c) + (c < d)) / ((a > b) + (b > c))
used?
 
J

J. J. Farrell

Harold said:
That doesn't mean you should use it as such. Adding multiplying and
dividing these results is not what they're for. You Can do it, but what
will do with the result? This isn't the type I'm talking about - it's
the intent. How often is something like ((a < b) + (b < c) + (c < d)) /
((a > b) + (b > c)) used?

Not often, but there's no reason why it shouldn't be if it's appropriate
(as long as you know that either (a > b) or (b > c) in this case ;) ).
Style and readability considerations might often rule such uses out.
They're numbers with well-known values, not something special to be used
for a particular purpose. One possibly reasonable use would be to save
the result of a comparison for later, which is presumably what was going
on in the OP's example.
 
K

Keith Thompson

Harold Aptroot said:
That doesn't mean you should use it as such. Adding multiplying and
dividing these results is not what they're for. You Can do it, but
what will do with the result? This isn't the type I'm talking about -
it's the intent. How often is something like ((a < b) + (b < c) + (c <
d)) / ((a > b) + (b > c)) used?

Not often, but there's nothing wrong with taking advantage of the
numeric value when it makes sense to do so. For example:

int count = 0;
count += x > MAX_X;
count += y > MAX_Y;
count += z > MAX_Z;
if (count >= 2) {
fprintf(stderr, "Warning: two or more values exceed maxima\n");
}

It may be too terse for some people's tastes, and there's always the
danger of extending the idea to something like isdigit() that
*doesn't* necessarily return 1 for true, but I find it clear enough.

You could do avoid it by doing something like:

if (x > MAX_X) {
count ++;
}

or even:

count += (x > MAX_X ? 1 : 0);

but I prefer the simpler form.
 
J

James Kuyper

Harold said:
That doesn't mean you should use it as such. Adding multiplying and
dividing these results is not what they're for. You Can do it, but what
will do with the result? This isn't the type I'm talking about - it's
the intent. How often is something like ((a < b) + (b < c) + (c < d)) /
((a > b) + (b > c)) used?

Not often, but when it is the simplest way to express an idea (and I
have had situations where I've written such code because it was the
simplest way), I don't see any reason not to use it.

There's something to be said for a language that has a distinct boolean
type, with no implicit conversions to or from numeric types, along with
a requirement that the condition in an if(), while(), for() or ?: must
have boolean type. However, that's a language so greatly divorced from
the historical development of C that it should be given a different name.

As long as the result of a comparison expression is guaranteed to be
either 0 or 1, it's entirely appropriate to write code which depends
upon that fact.
 
B

Bruce Cook

Harold said:
That doesn't mean you should use it as such. Adding multiplying and
dividing these results is not what they're for. You Can do it, but what
will do with the result? This isn't the type I'm talking about - it's the
intent. How often is something like ((a < b) + (b < c) + (c < d)) / ((a >
b) + (b > c)) used?

I agree with you on this, having seen some abuses and problems with people
using 'bool' returning functions that actually returned zero/on-zero values.

I find it is good practice to use bools as bools and not treat them as a
normal int.

Bruce
 
B

Ben Bacarisse

Richard said:
Q: Where did ANYONE see BOOL in C treated as ANYTHING other than 0 or
non zero?

C's logical and comparison operators all return int. So do other
seemingly logical tests like isspace. You need to know not only the
type but the range of possible return values in order to use all of
these correctly. If all of these returned _Bool, then you would have
one less thing to think about, but the boat sailed on that one years
ago.
YOu know, when C was designed it was so designed for a reason. Please,
please stop trying to make it into some kind of type safe nut ball
language.

Keith seems to me to be doing quite the opposite. He is warning that
the messy history of C means that you can be easily mistaken about
what types are used in various situations.
Learn what a bit is and go from there.

What do you hope to gain by being rude? Please remember Thumper's
mother.
 
G

Guest

it's automatically converted. If the expression evaluates to zero
then false (0) is assigned to somebool otherwise true (1).

I think the above code is a little too "clever".

I think you should distinguish type and value

I'm lost as to which expression you are talking about.
What expression and what "new compilers"?
Q: Where did ANYONE see BOOL in C treated as ANYTHING other than 0 or
non zero?

C99 bool types hold either 0 or 1
YOu know, when C was designed it was so designed for a reason.
really?

Please,
please stop trying to make it into some kind of type safe nut ball
language.

Learn what a bit is and go from there

I don't think that will teach you much about C. certainly not
the whole story.
 
B

Ben Bacarisse

Bruce Cook said:
Harold said:
I agree with you on this, having seen some abuses and problems with people
using 'bool' returning functions that actually returned zero/on-zero values.

I find it is good practice to use bools as bools and not treat them as a
normal int.

You will hit a terminology problem here. Your first bool is in quotes
so presumably you mean an int being using in a true/false way. C99
has a real bool type (called _Bool and typedefed to bool if you use
the recommended header) which is probably not what you mean.

The problems of int returning function that use non-zero values (other
than 1) can be avoided if you do use bool (the C99 type). For
example:

#define b_isspace(x) ((bool)isspace(x))

int how_many = b_isspace(c1) + (c2 != '\n') + (pos < len);

will give you an int between 0 and 2 that counts the number of
conditions that were met. These situations are rare, but when they
crop up, C99's rules for how _Bool works make everything quite sane in
my opinion.
 
N

Nelu

That statement is either incorrect or strangely written, depending on
whether the reader interprets the '!' as a C '!'.

Suggested fix: "(which is either true(1) or false(0))".

The standard uses a non-zero (not necessarily one) value for true even if
'>=' will return 1.

I was talking about truth values not about the boolean macros.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top