[issue23602] Implement __format__ for Fraction

2021-04-23 Thread Sergey B Kirpichev
Change by Sergey B Kirpichev : -- nosy: +Sergey.Kirpichev ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue23602] Implement __format__ for Fraction

2015-03-31 Thread Stefan Krah
Stefan Krah added the comment: It wouldn't be too bad if Py and C version of Decimal.__format__ had same interface. What do you think? Let's discuss that in a separate issue. [DefaultContext] I don't understand what do you mean with this. Is this something that I'm doing wrong in my patch

[issue23602] Implement __format__ for Fraction

2015-03-30 Thread Stefan Krah
Stefan Krah added the comment: Regarding Decimal: 1) The context precision isn't used for formatting. If you have another reason for proposing the optional context argument for dec_format(), please open another issue. 2) There's another problem: The mythical DefaultContext

[issue23602] Implement __format__ for Fraction

2015-03-30 Thread Tuomas Suutari
Tuomas Suutari added the comment: On 30 March 2015 at 13:49, Stefan Krah wrote: Regarding Decimal: 1) The context precision isn't used for formatting. If you have another reason for proposing the optional context argument for dec_format(), please open another issue. Yes,

[issue23602] Implement __format__ for Fraction

2015-03-30 Thread Martin Panter
Martin Panter added the comment: I understand double rounding to mean incorrectly rounding something like 0.14 up to 0.2. It should be rounded once to 1 decimal place (0.1). If you temporarily round it to a higher number of places before rounding to 1 place, you’re doing it wrong. So you

[issue23602] Implement __format__ for Fraction

2015-03-29 Thread Tuomas Suutari
Tuomas Suutari added the comment: Thanks for the comments again! I fixed the format(F(4, 27), '.1f') - 0.2 issue Serhiy Storchaka reported. Fix for that was as simple as adding one to the precision the decimals are calculated in, but while adding test cases for that I realized two new things:

[issue23602] Implement __format__ for Fraction

2015-03-29 Thread Stefan Behnel
Stefan Behnel added the comment: Meaning, something like this should work: x = (nom * 10**(prec+1)) // den if x % 10 5: x = x // 10 else: x = x // 10 + 1 print('%s.%s' % (x[:-prec], x[-prec:])) -- ___ Python tracker

[issue23602] Implement __format__ for Fraction

2015-03-29 Thread Stefan Behnel
Stefan Behnel added the comment: Or, speaking of division with remainder: n, r = divmod(nom * 10**prec, den) if r * 5 = den: n += 1 x = str(n) print('%s.%s' % (x[:-prec], x[-prec:])) ... minus the usual off-by-one that the tests would quickly find :) --

[issue23602] Implement __format__ for Fraction

2015-03-29 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: I think that Decimal is not needed for Fraction.__format__ (and I'm not sure that issue23602v4.patch is correct). The correct way to format Fraction as fixed-precision decimal is to use Fraction.__round__() or similar algorithm. The implementation can look

[issue23602] Implement __format__ for Fraction

2015-03-29 Thread Tuomas Suutari
Tuomas Suutari added the comment: On 29 March 2015 at 19:54, Serhiy Storchaka wrote: I think that Decimal is not needed for Fraction.__format__ (and I'm not sure that issue23602v4.patch is correct). Of course it's not needed. I'm using it to avoid re-implementing all the various formatting

[issue23602] Implement __format__ for Fraction

2015-03-29 Thread Stefan Behnel
Stefan Behnel added the comment: But these parameters could also be partly delegated to normal string (not number) formatting, right? One of the advantages of not depending on Decimal is, well, to not depend on Decimal, which is a rather uncommon dependency when using Fractions in an

[issue23602] Implement __format__ for Fraction

2015-03-29 Thread Wolfgang Maier
Wolfgang Maier added the comment: Initially, I also thought that this should be addressable with Fraction.__round__ or an optimized variation of it, but Tuomas is right that it gets complicated by the fact that you need to cope with the different format specifiers and not all of them fit the

[issue23602] Implement __format__ for Fraction

2015-03-26 Thread Wolfgang Maier
Wolfgang Maier added the comment: Decimal formatting intentionally differs from float formatting, see #23460. I see. Thanks for the pointer. What about the missing zero in the exponent ? -- ___ Python tracker rep...@bugs.python.org

[issue23602] Implement __format__ for Fraction

2015-03-26 Thread Stefan Krah
Stefan Krah added the comment: The zero isn't missing. :) We are following http://speleotrove.com/decimal/decarith.html, with thousands of test cases. We could decide to do something special for g, but there are good reasons not to do that. --

[issue23602] Implement __format__ for Fraction

2015-03-26 Thread Stefan Krah
Stefan Krah added the comment: Decimal formatting intentionally differs from float formatting, see #23460. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23602 ___

[issue23602] Implement __format__ for Fraction

2015-03-26 Thread Wolfgang Maier
Wolfgang Maier added the comment: actually, I'm not sure whether formatting Decimals gives correct output under all conditions (correctly rounded yes, but maybe not formatted correctly?). compare: format(float('1.481e-6'),'.3g') '1.48e-06' format(Decimal('1.481e-6'),'.3g') '0.0148'

[issue23602] Implement __format__ for Fraction

2015-03-23 Thread Stefan Behnel
Stefan Behnel added the comment: Absolutely. Fractions are all about exact calculations, much more so than Decimals. So the formatting output should be as accurate as requested or possible (well, excluding infinity). -- nosy: +scoder ___ Python

[issue23602] Implement __format__ for Fraction

2015-03-23 Thread Mark Dickinson
Mark Dickinson added the comment: [Eric] I'm not sure it needs fixing... Hmm. We've gone to some lengths to make sure that we get correctly-rounded results for formatting of Decimal and float types, as well as to make sure that operations like converting a Fraction to a float are correctly

[issue23602] Implement __format__ for Fraction

2015-03-21 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: from fractions import Fraction as F format(F(4, 27), 'f') '0.1481481' format(F(4, 27), '.1f') '0.2' -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23602

[issue23602] Implement __format__ for Fraction

2015-03-09 Thread Tuomas Suutari
Tuomas Suutari added the comment: Martin Panter wrote: Regarding sharing fractions._parse_format_specifier(), perhaps have a look at _pydecimal._parse_format_specifier() I did find that, but since it was a private function in private module, I was unsure if I can use it here. The _pydecimal

[issue23602] Implement __format__ for Fraction

2015-03-09 Thread Tuomas Suutari
Tuomas Suutari added the comment: Version 3 of the patch. Changes to v2: * Use raw-strings for the regexps. * Make the specifier regexp more robust with \A, \Z and re.DOTALL. * Add more test cases; especially g and e formats and negative fractions. -- Added file:

[issue23602] Implement __format__ for Fraction

2015-03-09 Thread Serhiy Storchaka
Changes by Serhiy Storchaka storch...@gmail.com: -- assignee: - serhiy.storchaka priority: normal - low ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23602 ___

[issue23602] Implement __format__ for Fraction

2015-03-08 Thread Eric V. Smith
Eric V. Smith added the comment: I'm not sure it needs fixing: it follows from the definition of using Decimal(num) / Decimal(denom). Plus, it's controllable with a decimal context: from decimal import localcontext with localcontext() as ctx: ... ctx.prec = 100 ... format(F(1, 3),

[issue23602] Implement __format__ for Fraction

2015-03-08 Thread Martin Panter
Martin Panter added the comment: I’ve never actually used the Fraction class, but I doubt its behaviour should depend on whatever settings are in the current decimal context. Maybe you can extract the precision out of the format string, and base the internal decimal object on that.

[issue23602] Implement __format__ for Fraction

2015-03-08 Thread Tuomas Suutari
Tuomas Suutari added the comment: Serhiy Storchaka wrote: from fractions import Fraction as F format(F(1, 3), '.30f') '0.00' Good catch! I'll try to fix this and add some more test cases. -- ___ Python tracker

[issue23602] Implement __format__ for Fraction

2015-03-08 Thread Martin Panter
Martin Panter added the comment: Regarding sharing fractions._parse_format_specifier(), perhaps have a look at _pydecimal._parse_format_specifier() -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23602

[issue23602] Implement __format__ for Fraction

2015-03-08 Thread Tuomas Suutari
Tuomas Suutari added the comment: Eric V. Smith wrote: I'm not sure it needs fixing: it follows from the definition of using Decimal(num) / Decimal(denom). Plus, it's controllable with a decimal context: Hmm... Even though it's tempting to agree with you and just ignore the precision bug,

[issue23602] Implement __format__ for Fraction

2015-03-08 Thread Tuomas Suutari
Tuomas Suutari added the comment: Here's the next round of the patch. For formatting fractions with any given precision I had to parse the precision from format specifier and at this point it seemed easier to just create a general parser for the Format Specification Mini-Language. In this

[issue23602] Implement __format__ for Fraction

2015-03-07 Thread Tuomas Suutari
New submission from Tuomas Suutari: Since Decimal supports __format__, it would be nice that Fraction did too. -- components: Library (Lib) messages: 237460 nosy: tuomas.suutari priority: normal severity: normal status: open title: Implement __format__ for Fraction versions: Python 3.5

[issue23602] Implement __format__ for Fraction

2015-03-07 Thread Ezio Melotti
Changes by Ezio Melotti ezio.melo...@gmail.com: -- nosy: +eric.smith, ezio.melotti, mark.dickinson, rhettinger, skrah stage: - patch review type: - enhancement ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23602

[issue23602] Implement __format__ for Fraction

2015-03-07 Thread Tuomas Suutari
Tuomas Suutari added the comment: Here's a patch that adds Fraction.__format__ implementation, test cases and documentation. -- keywords: +patch Added file: http://bugs.python.org/file38378/issue23602.patch ___ Python tracker rep...@bugs.python.org

[issue23602] Implement __format__ for Fraction

2015-03-07 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: from fractions import Fraction as F format(F(1, 3), '.30f') '0.00' -- nosy: +serhiy.storchaka ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23602

[issue23602] Implement __format__ for Fraction

2015-03-07 Thread Martin Panter
Changes by Martin Panter vadmium...@gmail.com: -- nosy: +vadmium ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23602 ___ ___ Python-bugs-list