Commit Graph

56 Commits

Author SHA1 Message Date
Serhiy Storchaka c8d2630995
gh-82017: Support as_integer_ratio() in the Fraction constructor (GH-120271)
Any objects that have the as_integer_ratio() method (e.g. numpy.float128)
can now be converted to a fraction.
2024-07-19 08:06:53 +03:00
Serhiy Storchaka d7fcaa73b7
gh-119838: Treat Fraction as a real value in mixed arithmetic operations with complex (GH-119839) 2024-06-03 12:29:01 +03:00
Joshua Herman b9965ef282
gh-119189: Fix the power operator for Fraction (GH-119242)
When using the ** operator or pow() with Fraction as the base
and an exponent that is not rational, a float, or a complex, the
fraction is no longer converted to a float.
2024-05-31 10:05:09 +00:00
Wim Jeantine-Glenn fcca08ec2f
gh-119594: Improve pow(fraction.Fraction(), b, modulo) error message (#119593)
If one calls pow(fractions.Fraction, x, module) with modulo not None, the error message now says that the types are incompatible rather than saying pow only takes 2 arguments.  Implemented by having fractions.Fraction __pow__ accept optional modulo argument and return NotImplemented if not None.  pow() then raises with appropriate message.
---------

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
2024-05-29 13:46:20 -04:00
Kirill Podoprigora 5319c66550
gh-102840: Fix confused traceback when floordiv or mod operations happens between Fraction and complex objects (GH-102842) 2024-02-10 16:37:19 +02:00
Crowthebird dd56b57483
gh-114014: Update `fractions.Fraction()`'s rational parsing regex (#114015)
Fix a bug in the regex used for parsing a string input to the `fractions.Fraction` constructor. That bug led to an inconsistent exception message being given for some inputs.

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
2024-01-13 12:02:39 +00:00
Mark Dickinson fe479fb8a9
gh-67790: Support basic formatting for Fraction (#111320)
PR #100161 added fancy float-style formatting for the Fraction type,
but left us in a state where basic formatting for fractions (alignment,
fill, minimum width, thousands separators) still wasn't supported.

This PR adds that support.

---------

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
2023-12-16 10:58:31 +00:00
Sergey B Kirpichev 4624987b29
gh-101825: Clarify that as_integer_ratio() output is always normalized (#101843)
Make docstrings for `as_integer_ratio` consistent across types, and document that
the returned pair is always normalized (coprime integers, with positive denominator).

---------

Co-authored-by: Owain Davies <116417456+OTheDev@users.noreply.github.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
2023-02-27 19:11:28 +00:00
Sergey B Kirpichev 4f3786b761
gh-101773: Optimize creation of Fractions in private methods (#101780)
This PR adds a private `Fraction._from_coprime_ints` classmethod for internal creations of `Fraction` objects, replacing the use of `_normalize=False` in the existing constructor. This speeds up creation of `Fraction` objects arising from calculations. The `_normalize` argument to the `Fraction` constructor has been removed.

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
2023-02-27 18:53:22 +00:00
Mark Dickinson 3e09f3152e
gh-67790: Support float-style formatting for Fraction instances (#100161)
This PR adds support for float-style formatting for `Fraction` objects: it supports the `"e"`, `"E"`, `"f"`, `"F"`, `"g"`, `"G"` and `"%"` presentation types, and all the various bells and whistles of the formatting mini-language for those presentation types. The behaviour almost exactly matches that of `float`, but the implementation works with the exact `Fraction` value and does not do an intermediate conversion to `float`, and so avoids loss of precision or issues with numbers that are outside the dynamic range of the `float` type.

Note that the `"n"` presentation type is _not_ supported. That support could be added later if people have a need for it.

There's one corner-case where the behaviour differs from that of float: for the `float` type, if explicit alignment is specified with a fill character of `'0'` and alignment type `'='`, then thousands separators (if specified) are inserted into the padding string:

```python
>>> format(3.14, '0=11,.2f')
'0,000,003.14'
```

The exact same effect can be achieved by using the `'0'` flag:

```python
>>> format(3.14, '011,.2f')
'0,000,003.14'
```

For `Fraction`, only the `'0'` flag has the above behaviour with respect to thousands separators: there's no special-casing of the particular `'0='` fill-character/alignment combination. Instead, we treat the fill character `'0'` just like any other:

```python
>>> format(Fraction('3.14'), '0=11,.2f')
'00000003.14'
>>> format(Fraction('3.14'), '011,.2f')
'0,000,003.14'
```

The `Fraction` formatter is also stricter about combining these two things: it's not permitted to use both the `'0'` flag _and_ explicit alignment, on the basis that we should refuse the temptation to guess in the face of ambiguity. `float` is less picky:

```python
>>> format(3.14, '0<011,.2f')
'3.140000000'
>>> format(Fraction('3.14'), '0<011,.2f')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mdickinson/Repositories/python/cpython/Lib/fractions.py", line 414, in __format__
    raise ValueError(
ValueError: Invalid format specifier '0<011,.2f' for object of type 'Fraction'; can't use explicit alignment when zero-padding
```
2023-01-22 18:44:49 +00:00
Sergey B Kirpichev 909982e82a
gh-91851: Micro optimizations for arithmetic between Fractions (#25518)
Adapted from
https://github.com/python/cpython/pull/24779/commits/046c84e8f9

This makes arithmetic between Fractions with small components
just as fast as before python/cpython#24779, at some expense of
mixed arithmetic (e.g. Fraction + int).
2023-01-08 00:34:20 -08:00
Sergey B Kirpichev 0e640260da
gh-91851: Trivial optimizations in Fraction (#100791)
Make some trivial performance optimizations in Fraction

Uses private class attributes `_numerator` and `_denominator` in place of the `numerator` and `denominator` property accesses.

Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
2023-01-06 15:37:34 +00:00
Shantanu e83f88a455
gh-100488: Add is_integer method to fractions.Fraction (#100489) 2023-01-01 01:44:48 -07:00
Raymond Hettinger 3eaf70d836
GH-96465: Cache hashes for Fraction instances (GH-96483) 2022-09-07 10:31:50 -05:00
Raymond Hettinger 656167db81
Allow whitespace around a slash in fraction string inputs (GH-96496) 2022-09-02 11:10:58 -05:00
Mark Dickinson 420f0df862
Minor optimization for Fractions.limit_denominator (GH-93730)
When we construct the upper and lower candidates in limit_denominator,
the numerator and denominator are already relatively prime (and the
denominator positive) by construction, so there's no need to go through
the usual normalisation in the constructor. This saves a couple of
potentially expensive gcd calls.

Suggested by Michael Scott Asato Cuthbert in GH-93477.
2022-06-21 21:36:35 +02:00
Mark Dickinson d1b24775b4
bpo-44547: Make Fractions objects instances of typing.SupportsInt (GH-27851)
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-10-22 00:09:47 +02:00
Sergey B Kirpichev 89e50ab36f
bpo-44258: support PEP 515 for Fraction's initialization from string (GH-26422)
* bpo-44258: support PEP 515 for Fraction's initialization from string

* regexps's version

* A different regexps version, which doesn't suffer from catastrophic backtracking

* revert denom -> den

* strip "_" from the decimal str, add few tests

* drop redundant tests

* Add versionchanged & whatsnew entry

* Amend Fraction constructor docs

* Change .. versionchanged:...
2021-06-07 08:06:33 +01:00
Sergey B Kirpichev 5ffa58cb97
Trivial change in fractions module docs: real -> rational numbers (GH-25009)
Obviously, the former was a little misleading: not only rationals
may be considered as "infinite-precision, real numbers" - but, for
example, any real finite extension of the rationals.
2021-05-29 19:49:31 -03:00
Sergey B Kirpichev b102dd598d
bpo-44154: optimize Fraction pickling (GH-26186) 2021-05-17 00:20:02 -07:00
Sergey B Kirpichev 690aca7811
bpo-43420: Simple optimizations for Fraction's arithmetics (GH-24779)
bpo-43420: Implement standard transformations in + - * / that can often reduce the size of intermediate integers needed. For rationals with large components, this can yield dramatic speed improvements, but for small rationals can run 10-20% slower, due to increased fixed overheads in the longer-winded code. If those slowdowns turn out to be a problem, see the PR discussion for low-level implementation tricks that could cut other fixed overheads.

Co-authored-by: Tim Peters <tim.peters@gmail.com>
Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
2021-03-21 21:30:55 -05:00
Victor Stinner dc7a50d73a
bpo-39350: Fix fractions for int subclasses (GH-18375)
Fix regression in fractions.Fraction if the numerator and/or the
denominator is an int subclass. The math.gcd() function is now
used to normalize the numerator and denominator. math.gcd() always
return a int type. Previously, the GCD type depended on numerator
and denominator.
2020-02-07 23:42:51 +01:00
Sebastian Berg 427c84f13f
bpo-39274: Ensure Fraction.__bool__() returns a bool (GH-18017)
Some numerator types used (specifically NumPy) decides to not
return a Python boolean for the "a != b" operation. Using the equivalent
call to bool() guarantees a bool return also for such types.
2020-02-06 15:54:05 +01:00
Victor Stinner 4691a2f2a2
bpo-39350: Remove deprecated fractions.gcd() (GH-18021)
Remove fractions.gcd() function, deprecated since Python 3.5
(bpo-22486): use math.gcd() instead.
2020-01-16 11:02:51 +01:00
Tim Peters 29bb227a0c
Add a minor `Fraction.__hash__()` optimization (GH-15313)
* Add a minor `Fraction.__hash__` optimization that got lost in the shuffle.

Document the optimizations.
2019-08-16 21:09:16 -05:00
Raymond Hettinger f3cb68f2e4
bpo-37863: Optimize Fraction.__hash__() (#15298) 2019-08-15 20:58:26 -07:00
Raymond Hettinger f03b4c8a48
bpo-37819: Add Fraction.as_integer_ratio() (GH-15212) 2019-08-11 14:40:59 -07:00
Jakub Molinski a9a28808e5 bpo-36625: Remove obsolete comments from docstrings in fractions module (GH-12822)
Remove left-over references to Python 3.0 as the future in Fraction class docstrings.
2019-04-15 13:37:04 +01:00
Stefan Behnel 3a374e0c5a bpo-35588: Speed up mod, divmod and floordiv operations for Fraction type (#11322)
* bpo-35588: Implement mod and divmod operations for Fraction type by spelling out the numerator/denominator calculation, instead of instantiating and normalising Fractions along the way. This speeds up '%' and divmod() by 2-3x.

* bpo-35588: Also reimplement Fraction.__floordiv__() using integer operations to make it ~4x faster.

* Improve code formatting.

Co-Authored-By: scoder <stefan_ml@behnel.de>

* bpo-35588: Fix return type of divmod(): the result of the integer division should be an integer.

* bpo-35588: Further specialise __mod__() and inline the original helper function _flat_divmod() since it's no longer reused.

* bpo-35588: Add some tests with large numerators and/or denominators.

* bpo-35588: Use builtin "divmod()" function for implementing __divmod__() in order to simplify the implementation, even though performance results are mixed.

* Rremove accidentally added empty line.

* bpo-35588: Try to provide more informative output on test failures.

* bpo-35588: Improve wording in News entry.

Co-Authored-By: scoder <stefan_ml@behnel.de>

* Remove stray space.
2019-01-02 14:22:06 +02:00
Elias Zamaria 393f1ff62e bpo-32968: Make modulo and floor division involving Fraction and float consistent with other operations (#5956)
Make mixed-type `%` and `//` operations involving `Fraction` and `float` objects behave like all other mixed-type arithmetic operations: first the `Fraction` object is converted to a `float`, then the `float` operation is performed as normal. This fixes some surprising corner cases, like `Fraction('1/3') % inf` giving a NaN.

Thanks Elias Zamaria for the patch.
2018-08-27 07:59:28 +01:00
Mark Dickinson 7caf908c64 Issue #27832: Make _normalize parameter to Fraction.__init__ keyword-only. 2016-08-23 16:16:52 +01:00
Mark Dickinson 0add84b9b8 Issue #27539: Merge from 3.5. 2016-08-22 10:56:06 +01:00
Mark Dickinson 844796530a Issue #27539: Fix unnormalised Fraction.__pow__ result for negative exponent and base. Thanks Vedran Čačić. 2016-08-22 10:50:53 +01:00
Serhiy Storchaka 0d250bc119 Issue #25971: Optimized creating Fractions from floats by 2 times and from
Decimals by 3 times.
Unified error messages in float.as_integer_ratio(), Decimal.as_integer_ratio(),
and Fraction constructors.
2015-12-29 22:34:23 +02:00
Serhiy Storchaka 48e47aaa28 Issue #22486: Added the math.gcd() function. The fractions.gcd() function now is
deprecated.  Based on patch by Mark Dickinson.
2015-05-13 00:19:51 +03:00
Georg Brandl 40f9735486 #22464: Speed up common Fraction operations by special-casing several
operations for int-type arguments: constructor and equality test.

Also avoid redundant property lookups in addition and subtraction.
2014-09-24 08:37:55 +02:00
Serhiy Storchaka 465e60e654 Issue #22033: Reprs of most Python implemened classes now contain actual
class name instead of hardcoded one.
2014-07-25 23:36:00 +03:00
Mark Dickinson 3c286e2e0d Issue #21136: Avoid unnecessary normalization in Fractions resulting from power and other operations. 2014-04-05 09:29:00 +01:00
Mark Dickinson 73726aac0f Issue #16469: Fraction(float('nan')) and Fraction(float('inf')) now raise ValueError and OverflowError (resp.), not TypeError. 2012-11-15 20:58:40 +00:00
Mark Dickinson fec6620dfb Make Fraction(-1).__hash__() return -2 rather than -1 (see issue 10356). 2010-11-13 10:27:38 +00:00
Mark Dickinson dc787d2055 Issue #8188: Introduce a new scheme for computing hashes of numbers
(instances of int, float, complex, decimal.Decimal and
fractions.Fraction) that makes it easy to maintain the invariant that
hash(x) == hash(y) whenever x and y have equal value.
2010-05-23 13:33:13 +00:00
Mark Dickinson 98127c3716 Merged revisions 79629 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r79629 | mark.dickinson | 2010-04-02 23:27:36 +0100 (Fri, 02 Apr 2010) | 2 lines

  Issue #8294:  Allow float and Decimal arguments in Fraction constructor.
........
2010-04-03 11:18:52 +00:00
Mark Dickinson 327f02c60c Merged revisions 79455 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r79455 | mark.dickinson | 2010-03-27 11:09:29 +0000 (Sat, 27 Mar 2010) | 2 lines

  Make Fraction to complex comparisons with <=, <, >= or > raise TypeError.
........
2010-03-27 11:11:13 +00:00
Mark Dickinson dfb1562410 Merged revisions 76456 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r76456 | mark.dickinson | 2009-11-23 16:23:43 +0000 (Mon, 23 Nov 2009) | 2 lines

  Issue #7379:  Fix incorrect doctest for Fraction.limit_denominator.
........
2009-11-23 16:27:17 +00:00
Mark Dickinson 85424c9354 Issue #6431: Fix Fraction comparisons to return NotImplemented when
the Fraction type doesn't know how to handle the comparison without
loss of accuracy.  Also, make sure that comparisons between Fractions
and float infinities or nans do the right thing.
2009-07-18 14:41:42 +00:00
Matthias Klose b0a8be5f1b - remove svn:executable property from some library files 2009-06-22 14:17:00 +00:00
Mark Dickinson d4d95f8eac Merged revisions 71832 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r71832 | mark.dickinson | 2009-04-24 14:56:07 +0100 (Fri, 24 Apr 2009) | 3 lines

  Issue #5812: The two-argument form of the Fraction constructor
  now accepts arbitrary Rational instances.
........
2009-04-24 14:06:19 +00:00
Mark Dickinson cf63f2fb88 Issue #5812: Make Fraction('1e6') valid. The Fraction constructor now
accepts all strings accepted by the float and Decimal constructors,
with the exception of strings representing NaNs or infinities.
2009-04-22 17:50:21 +00:00
Georg Brandl 3a9b062f5b Manually merge r68096,68189 from 3.0 branch. 2009-01-03 22:07:57 +00:00
Georg Brandl 2ee470f7f9 Merged revisions 64722,64729,64753,64845-64846,64849,64871,64880-64882,64885,64888,64897,64900-64901,64915,64926-64929,64938-64941,64944,64961,64966,64973 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r64722 | georg.brandl | 2008-07-05 12:13:36 +0200 (Sat, 05 Jul 2008) | 4 lines

  #2663: support an *ignore* argument to shutil.copytree(). Patch by Tarek Ziade.

  This is a new feature, but Barry authorized adding it in the beta period.
........
  r64729 | mark.dickinson | 2008-07-05 13:33:52 +0200 (Sat, 05 Jul 2008) | 5 lines

  Issue 3188: accept float('infinity') as well as float('inf').  This
  makes the float constructor behave in the same way as specified
  by various other language standards, including C99, IEEE 754r,
  and the IBM Decimal standard.
........
  r64753 | gregory.p.smith | 2008-07-06 05:35:58 +0200 (Sun, 06 Jul 2008) | 4 lines

  - Issue #2862: Make int and float freelist management consistent with other
    freelists.  Changes their CompactFreeList apis into ClearFreeList apis and
    calls them via gc.collect().
........
  r64845 | raymond.hettinger | 2008-07-10 16:03:19 +0200 (Thu, 10 Jul 2008) | 1 line

  Issue 3301:  Bisect functions behaved badly when lo was negative.
........
  r64846 | raymond.hettinger | 2008-07-10 16:34:57 +0200 (Thu, 10 Jul 2008) | 1 line

  Issue 3285: Fractions from_float() and from_decimal() accept Integral arguments.
........
  r64849 | andrew.kuchling | 2008-07-10 16:43:31 +0200 (Thu, 10 Jul 2008) | 1 line

  Wording changes
........
  r64871 | raymond.hettinger | 2008-07-11 14:00:21 +0200 (Fri, 11 Jul 2008) | 1 line

  Add cautionary note on the use of PySequence_Fast_ITEMS.
........
  r64880 | amaury.forgeotdarc | 2008-07-11 23:28:25 +0200 (Fri, 11 Jul 2008) | 5 lines

  #3317 in zipfile module, restore the previous names of global variables:
  some applications relied on them.

  Also remove duplicated lines.
........
  r64881 | amaury.forgeotdarc | 2008-07-11 23:45:06 +0200 (Fri, 11 Jul 2008) | 3 lines

  #3342: In tracebacks, printed source lines were not indented since r62555.
  #3343: Py_DisplaySourceLine should be a private function. Rename it to _Py_DisplaySourceLine.
........
  r64882 | josiah.carlson | 2008-07-12 00:17:14 +0200 (Sat, 12 Jul 2008) | 2 lines

  Fix for the AttributeError in test_asynchat.
........
  r64885 | josiah.carlson | 2008-07-12 01:26:59 +0200 (Sat, 12 Jul 2008) | 2 lines

  Fixed test for asyncore.
........
  r64888 | matthias.klose | 2008-07-12 09:51:48 +0200 (Sat, 12 Jul 2008) | 2 lines

  - Fix bashisms in Tools/faqwiz/move-faqwiz.sh
........
  r64897 | benjamin.peterson | 2008-07-12 22:16:19 +0200 (Sat, 12 Jul 2008) | 1 line

  fix various doc typos #3320
........
  r64900 | alexandre.vassalotti | 2008-07-13 00:06:53 +0200 (Sun, 13 Jul 2008) | 2 lines

  Fixed typo.
........
  r64901 | benjamin.peterson | 2008-07-13 01:41:19 +0200 (Sun, 13 Jul 2008) | 1 line

  #1778443 robotparser fixes from Aristotelis Mikropoulos
........
  r64915 | nick.coghlan | 2008-07-13 16:52:36 +0200 (Sun, 13 Jul 2008) | 1 line

  Fix issue 3221 by emitting a RuntimeWarning instead of raising SystemError when the parent module can't be found during an absolute import (likely due to non-PEP 361 aware code which sets a module level __package__ attribute)
........
  r64926 | martin.v.loewis | 2008-07-13 22:31:49 +0200 (Sun, 13 Jul 2008) | 2 lines

  Add turtle into the module index.
........
  r64927 | alexandre.vassalotti | 2008-07-13 22:42:44 +0200 (Sun, 13 Jul 2008) | 3 lines

  Issue #3274: Use a less common identifier for the temporary variable
  in Py_CLEAR().
........
  r64928 | andrew.kuchling | 2008-07-13 23:43:25 +0200 (Sun, 13 Jul 2008) | 1 line

  Re-word
........
  r64929 | andrew.kuchling | 2008-07-13 23:43:52 +0200 (Sun, 13 Jul 2008) | 1 line

  Add various items; move ctypes items into a subsection of their own
........
  r64938 | andrew.kuchling | 2008-07-14 02:35:32 +0200 (Mon, 14 Jul 2008) | 1 line

  Typo fixes
........
  r64939 | andrew.kuchling | 2008-07-14 02:40:55 +0200 (Mon, 14 Jul 2008) | 1 line

  Typo fix
........
  r64940 | andrew.kuchling | 2008-07-14 03:18:16 +0200 (Mon, 14 Jul 2008) | 1 line

  Typo fix
........
  r64941 | andrew.kuchling | 2008-07-14 03:18:31 +0200 (Mon, 14 Jul 2008) | 1 line

  Expand the multiprocessing section
........
  r64944 | gregory.p.smith | 2008-07-14 08:06:48 +0200 (Mon, 14 Jul 2008) | 7 lines

  Fix posix.fork1() / os.fork1() to only call PyOS_AfterFork() in the child
  process rather than both parent and child.

  Does anyone actually use fork1()?  It appears to be a Solaris thing
  but if Python is built with pthreads on Solaris, fork1() and fork()
  should be the same.
........
  r64961 | jesse.noller | 2008-07-15 15:47:33 +0200 (Tue, 15 Jul 2008) | 1 line

  multiprocessing/connection.py patch to remove fqdn oddness for issue 3270
........
  r64966 | nick.coghlan | 2008-07-15 17:40:22 +0200 (Tue, 15 Jul 2008) | 1 line

  Add missing NEWS entry for r64962
........
  r64973 | jesse.noller | 2008-07-15 20:29:18 +0200 (Tue, 15 Jul 2008) | 1 line

  Revert 3270 patch: self._address is in pretty widespread use, need to revisit
........
2008-07-16 12:55:28 +00:00