2
f1[x_] := IntegerQ[x]

f2[x_] := Element[x, Integers]

Both functions give identical results in everything except for such case:

f1[4.]

(*False*)

f2[4.]
(*4. \[Element] Integers*)

Any idea why?

Thanks

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
  • 5
    Precisely this is documented in the Details for IntegerQ. "IntegerQ[ expr ] returns False unless expr is manifestly an integer (i.e. has head Integer). Simplify[ expr \[Element]Integers] can be used to try to determine whether an expression is mathematically equal to an integer." – Mark Adler Jan 04 '15 at 05:51
  • 1
    It is unfortunate that data types do not always correspond to number classes even though they may share the same name. See http://chat.stackexchange.com/rooms/19708/discussion-on-question-by-david-carraher-real-numbers-in-the-wolfram-language for a discussion about data types versus classes of numbers (Real numbers). – DavidC Jan 04 '15 at 14:31
  • 1
    Another way to look at this, the 4. is an approximate real number. If you interpret it as 4+O($MachineEpsilon), then you can see it may or may not be an exact integer so Element properly returns unevaluated . – george2079 Jan 05 '15 at 20:13

1 Answers1

4

IntegerQ is meant for programming and tests for a data type, not whether something is mathematically an integer.

Element is meant to represent a mathematical concept.

The two are not interchangeable.

Functions ending in ...Q always return True or False. Since the data type of x is not Integer (in the programming sense---it's a symbol), IntegerQ[x] returns False.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • I am not questioning about ...Q logical tests functions. I am questioning why Element does not recognize that 4. is a real number as it does with 4.1. If you try to used Element with any domain (Integers, Reals, Complexes) it works perfectly except for this specific case. – Basheer Algohi Jan 04 '15 at 06:17
  • @Algohi Because all 4.0, 4, 4/1, 8/2, etc. are different representations of the same mathematical concept, the number four, which is a member of Z. Element tests mathematical set-inclusion while IntegerQ checks if the argument is "structurally" represented in Mathematica as an Integer type or not. – István Zachar Jan 04 '15 at 10:29
  • Also compare to 4. == 4 and 4. === 4 – Carlo Jan 04 '15 at 14:47