Ask Your Question

why sagemath says 1.0 is integer?

asked 2019-12-25 01:01:43 -0600

Nasser gravatar image

This is really strange. sagemath 8.9


sage: var('x')
sage: x=1
sage: x.is_integer()
sage: x=1.0
sage: x.is_integer()

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 import x
>>> x=1
>>> isinstance(x,int)
>>> x=1.0
>>> isinstance(x,int)
>>> isinstance(x,float)

Question is: Should sagemath return true for 1.0 being an integer?

edit retag flag offensive close merge delete


The 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).

nbruin gravatar imagenbruin ( 2019-12-25 11:33:27 -0600 )edit

I 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.

Iguananaut gravatar imageIguananaut ( 2019-12-31 04:35:45 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-12-25 13:22:20 -0600

dsejas gravatar image

Hello, @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


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!

edit flag offensive delete link more



Perhaps 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.

nbruin gravatar imagenbruin ( 2019-12-26 11:01:34 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-12-25 01:01:43 -0600

Seen: 174 times

Last updated: Dec 25 '19