Ticket #188 (closed enhancement: fixed)
Python's builtin round function is funny since it always coerces to float
|Reported by:||was||Owned by:||somebody|
Description (last modified by was) (diff)
From Susan Addington:
I just downloaded SAGE and am trying it out on MacOS 10.4. So far the only weirdness I have found is the behavior of the round function: round(sqrt(2),2) gives 1.4099999999999999. While this is correct, it looks strange.
Here is some further I/O that illustrates more what is going on:
sage: a = sqrt(2) sage: type(a) <type 'sage.rings.real_mpfr.RealNumber'> sage: b = round(a,2); b 1.4099999999999999 sage: type(b) <type 'float'> sage: round? Namespace: Python builtin Docstring [source file open failed]: round(number[, ndigits]) -> floating point number Round a number to a given precision in decimal digits (default 0 digits). This always returns a floating point number. Precision may be negative.
Note that round is a python builtin and it always outputs a float. It would make way more sense if round on a mpfr or complex number, etc., were to output a number of that type. Thus in SAGE the round function should be replaced by our own, which first tries to call a _round_ function, and if that doesn't work, calls the builtin round function.