Ask Your Question

Revision history [back]

1/5 is a Sage rational number, whereas 1/5. is a floating point number:

sage: (1/5).parent()
Rational Field
sage: (1/5.).parent()
Real Field with 53 bits of precision

Now, when you put them in a numpy array, they are converted to a type named numpy.float64:

sage: type(numpy.array([1/5])[0])
<type 'numpy.float64'>
sage: type(numpy.array([1/5.])[0])
<type 'numpy.float64'>

As you can see, this conversion do not lead to the same approximation for 1/5 and 1/5., they seem not rounded to the same direction:

sage: numpy.float64(1/5)
0.19999999999999998
sage: numpy.float64(1/5.)
0.20000000000000001
sage: numpy.float64(0.2)
0.20000000000000001

This explains your problem. There are various issues about the rounding direction in Sage and the tools its uses, and i agree there should be a general audit.

Now, if you want 1/5 to be rounded in the right direction, you can first round it using RealField() which is more consistent:

sage: numpy.array([RR(1/5)])[0] == 0.2
True

For Sage, 1/5 is a Sage rational number, whereas 1/5. is a floating point number:

sage: (1/5).parent()
Rational Field
sage: (1/5.).parent()
Real Field with 53 bits of precision

Now, when you put them in a numpy array, they are converted to a type named numpy.float64:

sage: type(numpy.array([1/5])[0])
<type 'numpy.float64'>
sage: type(numpy.array([1/5.])[0])
<type 'numpy.float64'>

As you can see, this conversion do not lead to the same approximation for 1/5 and 1/5., they seem not rounded to the same direction:

sage: numpy.float64(1/5)
0.19999999999999998
sage: numpy.float64(1/5.)
0.20000000000000001
sage: numpy.float64(0.2)
0.20000000000000001

This explains your problem. There are various issues about the rounding direction in Sage and the tools its uses, and i agree there should be a general audit.

Now, if you want 1/5 to be rounded in the right direction, you can first round it using RealField() which is more consistent:

sage: numpy.array([RR(1/5)])[0] == 0.2
True

For Sage, 1/5 is a rational number, whereas 1/5. is a floating point number:

sage: (1/5).parent()
Rational Field
sage: (1/5.).parent()
Real Field with 53 bits of precision

Now, when you put them in a numpy array, they are converted to a type named numpy.float64:

sage: type(numpy.array([1/5])[0])
<type 'numpy.float64'>
sage: type(numpy.array([1/5.])[0])
<type 'numpy.float64'>

As you can see, this conversion do not lead to the same approximation for 1/5 and 1/5., they seem not rounded to the same direction:

sage: numpy.float64(1/5)
0.19999999999999998
sage: numpy.float64(1/5.)
0.20000000000000001
sage: numpy.float64(0.2)
0.20000000000000001

This explains your problem. There are various issues about the rounding direction in Sage and the tools its uses, and i agree there should be a general audit.

Now, if you want 1/5 to be rounded in the right direction, you can first round it using RealField() which is seems more consistent:consistent than numpy.float64:

sage: numpy.array([RR(1/5)])[0] == 0.2
True