ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 31 Dec 2019 11:35:45 +0100why sagemath says 1.0 is integer?https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/This is really strange. sagemath 8.9
Compare
sage: var('x')
x
sage: x=1
sage: x.is_integer()
True
sage: x=1.0
sage: x.is_integer()
True
In sympy
(base) >python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> from sympy.abc import x
>>> x=1
>>> isinstance(x,int)
True
>>> x=1.0
>>> isinstance(x,int)
False
>>> isinstance(x,float)
True
Question is: Should sagemath return true for 1.0 being an integer?
Wed, 25 Dec 2019 08:01:43 +0100https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/Comment by Iguananaut for <p>This is really strange. sagemath 8.9</p>
<p>Compare</p>
<pre><code>sage: var('x')
x
sage: x=1
sage: x.is_integer()
True
sage: x=1.0
sage: x.is_integer()
True
</code></pre>
<p>In sympy</p>
<pre><code>(base) >python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> from sympy.abc import x
>>> x=1
>>> isinstance(x,int)
True
>>> x=1.0
>>> isinstance(x,int)
False
>>> isinstance(x,float)
True
</code></pre>
<p>Question is: Should sagemath return true for 1.0 being an integer?</p>
https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/?comment=49277#post-id-49277I should note, when you write "in sympy" none of what you demonstrate actually has anything to do with SymPy and is just plain Python behavior w.r.t. the types of objects, which is an entirely language-level statement and has basically no mathematical meaning. In Python `type(1)` is `int`, and `type(1.0)` is `float`. This is asking about the language-level type that implements the value (just as `type('')` is `str`). The mathematical fact (depending on the domain you're working in...) that 1.0 is the same as 1 is irrelevant here.Tue, 31 Dec 2019 11:35:45 +0100https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/?comment=49277#post-id-49277Comment by nbruin for <p>This is really strange. sagemath 8.9</p>
<p>Compare</p>
<pre><code>sage: var('x')
x
sage: x=1
sage: x.is_integer()
True
sage: x=1.0
sage: x.is_integer()
True
</code></pre>
<p>In sympy</p>
<pre><code>(base) >python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> from sympy.abc import x
>>> x=1
>>> isinstance(x,int)
True
>>> x=1.0
>>> isinstance(x,int)
False
>>> isinstance(x,float)
True
</code></pre>
<p>Question is: Should sagemath return true for 1.0 being an integer?</p>
https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/?comment=49183#post-id-49183The routine is a very thin wrapper of the mpfr_integer_p mpfr library function. The approach you demonstrate for sympy would also work in sage if you adjust the types used, although the more "sagey" thing would be to look at the "parent": compare `parent(1.0)` with `parent(1)`.Wed, 25 Dec 2019 18:33:27 +0100https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/?comment=49183#post-id-49183Answer by dsejas for <p>This is really strange. sagemath 8.9</p>
<p>Compare</p>
<pre><code>sage: var('x')
x
sage: x=1
sage: x.is_integer()
True
sage: x=1.0
sage: x.is_integer()
True
</code></pre>
<p>In sympy</p>
<pre><code>(base) >python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> from sympy.abc import x
>>> x=1
>>> isinstance(x,int)
True
>>> x=1.0
>>> isinstance(x,int)
False
>>> isinstance(x,float)
True
</code></pre>
<p>Question is: Should sagemath return true for 1.0 being an integer?</p>
https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/?answer=49187#post-id-49187Hello, @Nasser! As @nbruin pointed out in his comment, this behavior is related to the `mpfr_integer_p` function from the `MPFR` library. (`is_integer()` is a wrapper for that subroutine.)
Basically, Sage uses that library in order to have arbitrary precision real numbers (actually, the precision is limited by the amount of memory). In particular, if you call
type(1.0)
you will see the answer
<class 'sage.rings.real_mpfr.RealLiteral'>
Can you see the "mpfr" in there? That means that the `RealLiteral` type is internally represented using the MPFR library with a little of Cython magic for the C-Python interface.
Anyway, there are good reasons to return `True` when you call `(1.0).is_integer()`. Mathematically speaking, $1.0$ and $1$ are the same number, so both representations are integer. (Remember that Sage is a mathematical tool/programming language.) In essence, you are asking if $1.0\in\mathbb{Z}$ is true ---which is---, i.e., you are asking about the set of integer numbers.
On the other hand, concerning your `Sympy` example, notice that you are asking something different. The `isinstance` function is a question from the point of view of software programming, i.e., you are asking what kind of data type is your literal `1.0` or `1`. Evidently, `1.0` is an **instance** of the real data type, while `1` is an **instance** of the integer data type.
**In summary, `is_integer` is a mathematical question, and `isinstance` is a software programming question.**
I hope this helps!Wed, 25 Dec 2019 20:22:20 +0100https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/?answer=49187#post-id-49187Comment by nbruin for <p>Hello, <a href="/users/7931/nasser/">@Nasser</a>! As <a href="/users/62/nbruin/">@nbruin</a> pointed out in his comment, this behavior is related to the <code>mpfr_integer_p</code> function from the <code>MPFR</code> library. (<code>is_integer()</code> is a wrapper for that subroutine.)</p>
<p>Basically, Sage uses that library in order to have arbitrary precision real numbers (actually, the precision is limited by the amount of memory). In particular, if you call</p>
<pre><code>type(1.0)
</code></pre>
<p>you will see the answer</p>
<pre><code><class 'sage.rings.real_mpfr.RealLiteral'>
</code></pre>
<p>Can you see the "mpfr" in there? That means that the <code>RealLiteral</code> type is internally represented using the MPFR library with a little of Cython magic for the C-Python interface.</p>
<p>Anyway, there are good reasons to return <code>True</code> when you call <code>(1.0).is_integer()</code>. Mathematically speaking, $1.0$ and $1$ are the same number, so both representations are integer. (Remember that Sage is a mathematical tool/programming language.) In essence, you are asking if $1.0\in\mathbb{Z}$ is true ---which is---, i.e., you are asking about the set of integer numbers.</p>
<p>On the other hand, concerning your <code>Sympy</code> example, notice that you are asking something different. The <code>isinstance</code> function is a question from the point of view of software programming, i.e., you are asking what kind of data type is your literal <code>1.0</code> or <code>1</code>. Evidently, <code>1.0</code> is an <strong>instance</strong> of the real data type, while <code>1</code> is an <strong>instance</strong> of the integer data type.</p>
<p><strong>In summary, <code>is_integer</code> is a mathematical question, and <code>isinstance</code> is a software programming question.</strong></p>
<p>I hope this helps!</p>
https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/?comment=49212#post-id-49212Perhaps a little expansion on the interpretation of floats: People often think of `1.0` as an approximation of a real number. In that case, the answer to the question "Is 1.0 an integer?" should be "maybe", and for 1.3 it should be "no". For computations it's often more attractive to say that 1.0 is representing a real number exactly; just perhaps not the one you were originally aiming at. In that case, 1.0 is indeed an integer, but then you should probably not be asking that question because it doesn't necessarily mean that the number you were interested in when you got 1.0 has the same property.Thu, 26 Dec 2019 18:01:34 +0100https://ask.sagemath.org/question/49180/why-sagemath-says-10-is-integer/?comment=49212#post-id-49212