1
$\begingroup$

I ran into a bit of a weird (for me anyway) problem. Consider the bit of code below:

r0 = 3;
param = 40;
lambda = param π/2.;
lmax = 18;
ParallelTable[
  If[IntegerQ[2 lambda/π Sqrt[l (l + 1) - m^2]], 1, 0], {l, 1, lmax},{m, -l, l}]

When I evaluate this code, it should obtain an array mostly filled with 0s and a few values of 1.

However, if parameters r0 and param are given as real numbers (3. and 40. instead of 3 and 40) the result is all zeroes. Why is that? Why does it matter if there are integer or real numbers given as input for a formula that contains Pi and Sqrt?

$\endgroup$

closed as off-topic by Daniel Lichtblau, José Antonio Díaz Navas, m_goldberg, ilian, LCarvalho Mar 9 '18 at 20:47

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, José Antonio Díaz Navas, m_goldberg, ilian, LCarvalho
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Note that IntegerQ[1.] is False, whereas IntegerQ[1] is True, and that whenever you combine real and integer numbers, the result is real. Maybe use If[Abs[x - Round[x]] < 10^-12, ...] instead of IntegerQ. $\endgroup$ – JEM_Mosig Mar 6 '18 at 21:20
  • $\begingroup$ That is a great suggestion and I will try that, however in that expression there are real and integers! Pi is real, Sqrt is real. numbers l and m inside the Sqrt are integers by definition. After some more tries I realised that r0 cand be real but not param or the number 2 in the expressions in the block. I hope this makes sense. $\endgroup$ – lucian Mar 6 '18 at 21:35
  • 1
    $\begingroup$ Although Pi is real, it is an exact number. That is, it is not represented as 3.14159..., but as the symbolic expression Pi. As long as it is not combined with a finite-precision number, it stays exact and Mathematica may simplify the expression to an exact integer. For example Pi/Pi gives 1, not 1.. $\endgroup$ – JEM_Mosig Mar 6 '18 at 21:47
  • 1
    $\begingroup$ Btw.: Shouldn't the line lambda = param \[Pi]/2.; actually be lambda = param \[Pi]/2;? Because otherwise you'll only get zeros. $\endgroup$ – JEM_Mosig Mar 6 '18 at 21:53
2
$\begingroup$

Expressions like Pi or Sqrt[2] are exact symbolic expressions. That is, they are not represented by a finite string of digits, such as 3.14159... for Pi. Therefore, Mathematica can perform exact arithmetic with them. E.g.

Pi / Pi
(* 1 *)

The result is the exact number 1, and not the approximate real number 1..

IntegerQ[expr] checks if the head of expr is Integer. Therefore,

IntegerQ[1]
(* True *)

but

IntegerQ[1.]
(* False *)

because Head[1.] is Real and not Integer. Likewise

Sqrt[2]^2
(* 1 *)

and thus

IntegerQ[Sqrt[2]^2]
(* True *)

Note that whenever you combine an exact expression with an approximate real number, you get an approximate real number. For example

1. Sqrt[2]
(* 1.41421 *)

or

Sqrt[2.]
(* 1.41421 *)

instead of

Sqrt[2]
(* Sqrt[2] *)

which remains un-evaluated, as this is an exact expression that cannot be represented by a finite string of numbers.

$\endgroup$
  • $\begingroup$ thank you! this clarified a lot of questions for me! I always try to avoid exact numbers unless I used them as indexes in arrays for example. $\endgroup$ – lucian Mar 7 '18 at 11:38
3
$\begingroup$
IntegerQ[1.]
(* False *)

This is because Head[1.] is Real, not Integer.

1. \[Element] Integers

returns unevaluated because 1. means approximately 1, so Mathematica can't make a reliable decision.

IntegerQ[Rationalize[1.]]
(* True *)

This works because Rationalize finds the nearest Rational, and reduces it to Integer if it can. This may be too fussy in practice, I don't know. Another way is:

isInteger[x_] := Round[x] == x
isInteger[1.]
(* True *)

This works because Equal is True for approximate quantities that are close enough.

$\endgroup$
  • $\begingroup$ thank you! you explanation is also very clear! I have now a better understanding of how mathematica handles numbers! $\endgroup$ – lucian Mar 7 '18 at 11:40

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