1
$\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$
2
  • 2
    $\begingroup$ Perhaps, a = Boole[3 < 2] is what you are looking for. $\endgroup$
    – m_goldberg
    Commented Nov 21, 2015 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$ Commented Nov 21, 2015 at 15:45

1 Answer 1

2
$\begingroup$

Restating the comments:

a = Boole[3 < 2]
b = 3
a * b
(* -> 0 *)
$\endgroup$
1
  • 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
    Commented Nov 22, 2015 at 1:45

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