0
$\begingroup$

I'd like to use Boolean expressions as numeric values, as in:

a = 3 < 2
b = 3
a * b

but the final result of that is 3 False, instead of 0. I can do something like:

a = 3 < 2
b = 3
a * b /. False -> 0 /. True -> 1

but that seems awkward. I suppose I can make a function to do these replacements, but is there a more natural or a builtin way to do this?

$\endgroup$

closed as off-topic by Daniel Lichtblau, ilian, m_goldberg, Michael E2, Jens Nov 22 '15 at 2:38

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Daniel Lichtblau, ilian, m_goldberg, Michael E2, Jens
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ Perhaps, a = Boole[3 < 2] is what you are looking for. $\endgroup$ – m_goldberg Nov 21 '15 at 14:52
  • 1
    $\begingroup$ What m_goldberg says. Your double replacement is longer than necessary BTW, and could have been written as a * b /. {False -> 0 ,True -> 1}. $\endgroup$ – Sjoerd C. de Vries Nov 21 '15 at 15:45
2
$\begingroup$

Restating the comments:

a = Boole[3 < 2]
b = 3
a * b
(* -> 0 *)
$\endgroup$
  • 2
    $\begingroup$ Usually. when giving an answer that merely restates what was posted by someone else in a comment, one makes it a community wiki and gives attribution where applicable. $\endgroup$ – m_goldberg Nov 22 '15 at 1:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.