Ask Your Question

Revision history [back]

Sage has several types of floating-point numbers.

See an overview in

Among them, float and RDF elements are displayed with no trailing zero, while RR elements are displayed with a fixed number of .

To figure out what kind of number a given number is, use the parent function.

Convert between types of numbers by using int(a), ZZ(a), QQ(a), float(a), RR(a), RDF(a), etc.

If numbers with different parents are added, multiplied, subtracted or divided, the parent of the result is determined by Sage's coercion system.

Example.

sage: a = 3/2
sage: b = float(a)
sage: c = RDF(a)
sage: d = RR(a)

sage: a
3/2
sage: parent(a)
Rational Field
sage: QQ
Rational Field

sage: b
1.5
sage: parent(b)
<class 'float'>
sage: float
<class 'float'>

sage: d
1.50000000000000
sage: parent(d)
Real Field with 53 bits of precision
sage: RR
Real Field with 53 bits of precision

sage: c
1.5
sage: parent(c)
Real Double Field
sage: RDF
Real Double Field

sage: parent(a + b)
<class 'float'>
sage: parent(a + c)
Real Double Field
sage: parent(a + d)
Real Field with 53 bits of precision
sage: parent(b + c)
Real Double Field
sage: parent(b + d)
Real Field with 53 bits of precision
sage: parent(c + d)
Real Field with 53 bits of precision

Sage has several types of floating-point numbers.

See an overview in

Among them, float and RDF elements are displayed with no trailing zero, while RR elements are displayed with a fixed number of .significant figures.

To figure out what kind of number a given number is, use the parent function.

Convert between types of numbers by using int(a), ZZ(a), QQ(a), float(a), RR(a), RDF(a), etc.

If numbers with different parents are added, multiplied, subtracted or divided, the parent of the result is determined by Sage's coercion system.

Short answer to your question: work in RDF.

Below are a few examples to illustrate the above explanations.

Example.

sage: a = 3/2
sage: b = float(a)
sage: c = RDF(a)
sage: d = RR(a)

sage: a
3/2
sage: parent(a)
Rational Field
sage: QQ
Rational Field

sage: b
1.5
sage: parent(b)
<class 'float'>
sage: float
<class 'float'>

sage: d
1.50000000000000
sage: parent(d)
Real Field with 53 bits of precision
sage: RR
Real Field with 53 bits of precision

sage: c
1.5
sage: parent(c)
Real Double Field
sage: RDF
Real Double Field

sage: parent(a + b)
<class 'float'>
sage: parent(a + c)
Real Double Field
sage: parent(a + d)
Real Field with 53 bits of precision
sage: parent(b + c)
Real Double Field
sage: parent(b + d)
Real Field with 53 bits of precision
sage: parent(c + d)
Real Field with 53 bits of precision