test for floats and co.

27 views
Skip to first unread message

G. M.-S.

unread,
May 3, 2023, 11:21:39 AM5/3/23
to sage-s...@googlegroups.com

Related to a recent discussion, is there a (simple) way to find whether an expression contains non exact explicit numbers?

For example:

M1=matrix(1,2,[1,x+2])
M2=matrix(1,2,[1,x+2.])
mytest(M1)
→ True
mytest(M2)
→ False

Guillermo

Nils Bruin

unread,
May 3, 2023, 11:58:35 AM5/3/23
to sage-support
On Wednesday, 3 May 2023 at 08:21:39 UTC-7 G. M.-S. wrote:

Related to a recent discussion, is there a (simple) way to find whether an expression contains non exact explicit numbers?

For symbolic expression you should probably walk the entire expression tree.

For sage objects, examining the parents involved should do the trick:

sage: QQ.is_exact()
True
sage: RR.is_exact()
False

However, be careful:

sage: A=matrix(RR,2,2,[1,2,3,4])
sage: parent(A).is_exact()
True

because matrix rings presently aren't aware of inexactness of their base rings. So you should walk the construction of a parent and see if there are any inexact constituents:

sage: parent(A).construction()
(MatrixFunctor, Real Field with 53 bits of precision)

 

G. M.-S.

unread,
May 3, 2023, 12:41:50 PM5/3/23
to sage-s...@googlegroups.com

Thanks Nils.

I shall try to manage with the information you have given me.

(In case you are curious, this is for code aimed at my students.)

Guillermo

Nils Bruin

unread,
May 3, 2023, 5:25:37 PM5/3/23
to sage-support
On Wednesday, 3 May 2023 at 08:58:35 UTC-7 Nils Bruin wrote:
However, be careful:

sage: A=matrix(RR,2,2,[1,2,3,4])
sage: parent(A).is_exact()
True

because matrix rings presently aren't aware of inexactness of their base rings.

This is something that is considered a bug; see https://github.com/sagemath/sage/issues/34247
Reply all
Reply to author
Forward
0 new messages