Message Boards Message Boards

4 Replies
4 Total Likes
View groups...
Share this post:

How do you find type of variable and get an == on the type to evaluate?

Posted 10 years ago

Sorry for such a basic question but after several hours of search I am still flummoxed by this.

How do you detect variable types in Mathematica? (a pointer to an answer works as well)

I want to be able to have a statement evaluate to true if an arguement is real and false if it not. The true part works, but Mathematica just chooses (I am a rookie and don't quite understand why) not to evaluate the statement if it is false.

Thank you,


POSTED BY: Brent Halonen
4 Replies

That makes more sense, I've used it myself in Solve, just never thought of that reason.

TrueQ seems like a useful function. Lots of different understandings of "equals" is possible, so there must be a equally large set of equality operators.

POSTED BY: Brent Halonen

I think it's to allow for the writing of equations that then are subsequently used in Solve, DSolve, and other such functions. If Equal were to return False when both sides do not explicitly evaluate to True or False then there'd be no mechanism with which to write systems of equations. For example the following would not be possible as a useful syntax

Solve[a x + b ==c, x]

As a general rule I always use SameQ if I want to make a determination of truth in an expression. However, you might want to use Equal to allow for the possibility that an expression actually might not evaluate to either True or False and thereby make use of (for example) a third argument in an If statement.

When I look at other people's code I find they often do not make use of === and never notice the potential issue of using == until it bites them. An alternative is to make use of == along with wrapping the expression in TrueQ which forces a False when the expression using == does not evaluate to True or False. So--rule of thumb--always use === over == when doing a test of truth unless there's a good reason to make use of ==.

POSTED BY: David Reiss

Thanks for the quick and simple answer, I am still confused on what that means for how the language is written.

As I understand it:

SameQ is a strict equality test that returns false unless it returns true. Equal is a looser equality test (allows for type differences) but doesn't automatically return false.

Do you know why Mathematica does not return a false in this when == is used? What advantages does this give?

Is this because Equals will sometimes try to return true when they are slightly different, and want to avoid returning false negatives?

POSTED BY: Brent Halonen
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract