Opened 6 years ago

Closed 5 years ago

#21124 closed defect (fixed)

Real/Complex number str() method: do not truncate by default and allow specifying number of digits

Reported by: jdemeyer Owned by:
Priority: major Milestone: sage-8.1
Component: basic arithmetic Keywords:
Cc: Merged in:
Authors: Jeroen Demeyer Reviewers: Leif Leonhardy, Travis Scrimshaw
Report Upstream: N/A Work issues:
Branch: 1993e82 (Commits, GitHub, GitLab) Commit: 1993e823663a192a30dbc9fb010977baf5959079
Dependencies: Stopgaps:

Status badges

Description (last modified by jdemeyer)

  1. Use truncate=False by default in the str() method but use truncate=True in __repr__. For explicit string conversions using the str() method, I assume that the user wants full control and doesn't want random truncation to interfere. This can also be seen in the Sage library: there are a lot more places which call x.str(truncate=False) than x.str(truncate=True). For normal printing, or conversion to str, truncation is still the default.
  1. Implement a x.str(digits=20) argument to explicitly choose the number of digits for printing a real number.

Change History (18)

comment:1 Changed 6 years ago by jdemeyer

  • Summary changed from (RR element).str(): allow specifying number of digits to RealNumber.str(): allow specifying number of digits

comment:2 Changed 6 years ago by jdemeyer

  • Description modified (diff)
  • Summary changed from RealNumber.str(): allow specifying number of digits to RealNumber.str(): allow specifying number of digits and do not truncate by default

comment:3 Changed 6 years ago by jdemeyer

  • Description modified (diff)

comment:4 Changed 6 years ago by jdemeyer

  • Description modified (diff)
  • Summary changed from RealNumber.str(): allow specifying number of digits and do not truncate by default to RealNumber.str(): do not truncate by default and allow specifying number of digits

comment:5 Changed 6 years ago by jdemeyer

  • Summary changed from RealNumber.str(): do not truncate by default and allow specifying number of digits to Real/Complex number str() method: do not truncate by default and allow specifying number of digits

comment:6 Changed 6 years ago by jdemeyer

  • Description modified (diff)

comment:7 Changed 6 years ago by leif

Do I have to look into the doctesting framework or will this ticket just fix #21121 as a by-product?

comment:8 Changed 6 years ago by jdemeyer

It will not fix #21121 as by-product, but #21121 will be trivial to fix using #21124.

comment:9 Changed 6 years ago by jdemeyer

  • Branch set to u/jdemeyer/_rr_element__str____allow_specifying_number_of_digits

comment:10 Changed 6 years ago by jdemeyer

  • Commit set to a2933d9acef4dc84d2ecdbee86d9c255f07175db
  • Status changed from new to needs_review

New commits:

e8816a8Avoid a compiler warning
a2933d9Improve RealNumber.str()

comment:11 Changed 6 years ago by leif

--- a/src/sage/rings/complex_mpc.pyx
+++ b/src/sage/rings/complex_mpc.pyx
@@ -1026,32 +1026,32 @@ cdef class MPComplexNumber(sage.structure.element.FieldElement):
- ``base`` -- base for output
- - ``truncate`` -- if ``True``, round off the last digits in printing
- to lessen confusing base-2 roundoff issues.
+ - ``**kwds`` -- other arguments to pass the the ``str()``
+ method of the real numbers in the real and imaginary parts.
EXAMPLES::

s/the the/to the/

--- a/src/sage/rings/complex_number.pyx
+++ b/src/sage/rings/complex_number.pyx
@@ -465,17 +465,19 @@ cdef class ComplexNumber(sage.structure.element.FieldElement):
- ``base`` -- (Default: 10) The base to use for printing
- - ``truncate`` -- (Default: ``True``) Whether to print fewer
- digits than are available, to mask errors in the last bits.
-
- ``istr`` -- (Default: ``I``) String representation of the complex unit
+ - ``**kwds`` -- other arguments to pass the the ``str()``
+ method of the real numbers in the real and imaginary parts.
+
EXAMPLES::

s/the the/to the/

I.e., sed -i 's/ the the / to the /' src/sage/rings/complex_{mpc,number}.pyx


There's some inconsistency in giving the defaults of arguments:

(Default: 10) vs. (default:10) or even just mentioning it in the text (e.g. for no_sci, where the default is None), or in some cases also for truncate, where the default now is False, but the description says (just) "if True, ...".

(But that's a minor thing, as you'd presumably have to touch more regions to normalize all instances.)

P.S.: I looked at your previous commit, and just briefly scanned the new one.

comment:12 Changed 6 years ago by git

  • Commit changed from a2933d9acef4dc84d2ecdbee86d9c255f07175db to 1de12e3e00ab7caf2875b32e65adc69c1861f7ee

Branch pushed to git repo; I updated commit sha1. New commits:

1de12e3Minor doc fixes

comment:13 Changed 5 years ago by jdemeyer

  • Milestone changed from sage-7.3 to sage-8.0
  • Status changed from needs_review to needs_work

comment:14 Changed 5 years ago by git

  • Commit changed from 1de12e3e00ab7caf2875b32e65adc69c1861f7ee to 6955cfee1b2c458c30b0059ee3662388e47ce089

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

6955cfeImprove RealNumber.str()

comment:15 Changed 5 years ago by jdemeyer

  • Status changed from needs_work to needs_review

Rebased to 7.6.rc2

comment:16 Changed 5 years ago by git

  • Commit changed from 6955cfee1b2c458c30b0059ee3662388e47ce089 to 1993e823663a192a30dbc9fb010977baf5959079

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

1993e82Improve RealNumber.str()

comment:17 Changed 5 years ago by tscrim

  • Milestone changed from sage-8.0 to sage-8.1
  • Reviewers set to Leif Leonhardy, Travis Scrimshaw
  • Status changed from needs_review to positive_review

LGTM.

comment:18 Changed 5 years ago by vbraun

  • Branch changed from u/jdemeyer/_rr_element__str____allow_specifying_number_of_digits to 1993e823663a192a30dbc9fb010977baf5959079
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.