Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Slightly simplified, your question is about the following:

sage: RealField(150)( RDF(1)/RDF(10) )       # case 1
0.10000000000000000555111512312578270211815834
sage: RealField(150)( RR(1)/RR(10) )         # case 2
0.10000000000000000555111512312578270211815834

vs.

sage: R = RealField(prec=53, rnd='RNDZ')
sage: RealField(150)( R(1)/R(10) )           # case 3
0.099999999999999991673327315311325946822762489
sage: RealField(150)( RDF(1/10) )            # case 4
0.099999999999999991673327315311325946822762489

The first and second cases make guarantees about IEEE floating point behavior and therefore must yield the same answer. The third case explicitly states that it is rounding towards zero, and its to be expected that you get a slightly different answer with different rounding. The fourth case punts to the __float__ method of rationals, which is

def __float__(self):
    """
    Return floating point approximation to ``self`` as a Python float.

    OUTPUT: float

    EXAMPLES::

        sage: (-4/17).__float__()
        -0.23529411764705882
        sage: float(-4/17)
        -0.23529411764705882
    """
    return mpq_get_d(self.value)

According to the GMP/MPIR docs, mpq_get_d rounds towards zero so you get the same answer as in the third case.