aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_fractions.py
Commit message (Collapse)AuthorAge
* gh-130662: Accept leading zeros in precision/width for Fraction's formatting ↵Sergey B Kirpichev6 days
| | | | | (#130663) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
* Test also error messages in test_limit_int. (GH-134018)Serhiy Storchaka2025-05-14
|
* Improve tests for str to Fraction conversion (GH-134010)Serhiy Storchaka2025-05-14
|
* gh-130104: Call __rpow__ in ternary pow() if necessary (GH-130251)Serhiy Storchaka2025-04-16
| | | | | Previously it was only called in binary pow() and the binary power operator.
* gh-121797: Add class method Fraction.from_number() (GH-121800)Serhiy Storchaka2024-10-14
| | | | | | | | It is an alternative constructor which only accepts a single numeric argument. Unlike to Fraction.from_float() and Fraction.from_decimal() it accepts any real numbers supported by the standard constructor (int, float, Decimal, Rational numbers, objects with as_integer_ratio()). Unlike to the standard constructor, it does not accept strings.
* Fix typos (#123775)algonell2024-09-09
|
* gh-82017: Support as_integer_ratio() in the Fraction constructor (GH-120271)Serhiy Storchaka2024-07-19
| | | | | Any objects that have the as_integer_ratio() method (e.g. numpy.float128) can now be converted to a fraction.
* gh-120417: Remove unused imports in tests (part 2) (#120630)Victor Stinner2024-06-17
|
* gh-119838: Treat Fraction as a real value in mixed arithmetic operations ↵Serhiy Storchaka2024-06-03
| | | | with complex (GH-119839)
* gh-119189: Fix the power operator for Fraction (GH-119242)Joshua Herman2024-05-31
| | | | | | 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.
* gh-109218: Deprecate weird cases in the complex() constructor (GH-119620)Serhiy Storchaka2024-05-30
| | | | | | | * Passing a string as the "real" keyword argument is now an error; it should only be passed as a single positional argument. * Passing a complex number as the "real" or "imag" argument is now deprecated; it should only be passed as a single positional argument.
* gh-119594: Improve pow(fraction.Fraction(), b, modulo) error message (#119593)Wim Jeantine-Glenn2024-05-29
| | | | | | 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>
* gh-119189: Add yet more tests for mixed Fraction arithmetic (GH-119298)Serhiy Storchaka2024-05-21
|
* gh-119189: Add more tests for mixed Fraction arithmetic (GH-119236)Serhiy Storchaka2024-05-20
|
* gh-102840: Fix confused traceback when floordiv or mod operations happens ↵Kirill Podoprigora2024-02-10
| | | | between Fraction and complex objects (GH-102842)
* gh-114014: Update `fractions.Fraction()`'s rational parsing regex (#114015)Crowthebird2024-01-13
| | | | | | | | 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>
* gh-67790: Support basic formatting for Fraction (#111320)Mark Dickinson2023-12-16
| | | | | | | | | | | 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>
* gh-108303: Update test_fractions for new Lib/test/mathdata/ (#109686)Victor Stinner2023-09-21
|
* gh-109546: Add more tests for formatting floats and fractions (GH-109548)Serhiy Storchaka2023-09-19
|
* gh-101773: Optimize creation of Fractions in private methods (#101780)Sergey B Kirpichev2023-02-27
| | | | | | 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>
* gh-67790: Support float-style formatting for Fraction instances (#100161)Mark Dickinson2023-01-22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 ```
* gh-100488: Add is_integer method to fractions.Fraction (#100489)Shantanu2023-01-01
|
* Allow whitespace around a slash in fraction string inputs (GH-96496)Raymond Hettinger2022-09-02
|
* bpo-44547: Make Fractions objects instances of typing.SupportsInt (GH-27851)Mark Dickinson2021-10-22
| | | Co-authored-by: Łukasz Langa <lukasz@langa.pl>
* bpo-44258: support PEP 515 for Fraction's initialization from string (GH-26422)Sergey B Kirpichev2021-06-07
| | | | | | | | | | | | | | | | | | | * 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:...
* bpo-44154: optimize Fraction pickling (GH-26186)Sergey B Kirpichev2021-05-17
|
* bpo-43420: Simple optimizations for Fraction's arithmetics (GH-24779)Sergey B Kirpichev2021-03-21
| | | | | | | 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>
* Revert "bpo-26680: Incorporate is_integer in all built-in and standard ↵Raymond Hettinger2020-10-07
| | | | | library numeric types (GH-6121)" (GH-22584) This reverts commit 58a7da9e125422323f79c4ee95ac5549989d8162.
* bpo-26680: Incorporate is_integer in all built-in and standard library ↵Robert Smallshire2020-10-01
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | numeric types (GH-6121) * bpo-26680: Adds support for int.is_integer() for compatibility with float.is_integer(). The int.is_integer() method always returns True. * bpo-26680: Adds a test to ensure that False.is_integer() and True.is_integer() are always True. * bpo-26680: Adds Real.is_integer() with a trivial implementation using conversion to int. This default implementation is intended to reduce the workload for subclass implementers. It is not robust in the presence of infinities or NaNs and may have suboptimal performance for other types. * bpo-26680: Adds Rational.is_integer which returns True if the denominator is one. This implementation assumes the Rational is represented in it's lowest form, as required by the class docstring. * bpo-26680: Adds Integral.is_integer which always returns True. * bpo-26680: Adds tests for Fraction.is_integer called as an instance method. The tests for the Rational abstract base class use an unbound method to sidestep the inability to directly instantiate Rational. These tests check that everything works correct as an instance method. * bpo-26680: Updates documentation for Real.is_integer and built-ins int and float. The call x.is_integer() is now listed in the table of operations which apply to all numeric types except complex, with a reference to the full documentation for Real.is_integer(). Mention of is_integer() has been removed from the section 'Additional Methods on Float'. The documentation for Real.is_integer() describes its purpose, and mentions that it should be overridden for performance reasons, or to handle special values like NaN. * bpo-26680: Adds Decimal.is_integer to the Python and C implementations. The C implementation of Decimal already implements and uses mpd_isinteger internally, we just expose the existing function to Python. The Python implementation uses internal conversion to integer using to_integral_value(). In both cases, the corresponding context methods are also implemented. Tests and documentation are included. * bpo-26680: Updates the ACKS file. * bpo-26680: NEWS entries for int, the numeric ABCs and Decimal. Co-authored-by: Robert Smallshire <rob@sixty-north.com>
* bpo-40443: Remove unused imports in tests (GH-19804)Victor Stinner2020-04-30
|
* bpo-39350: Fix fractions for int subclasses (GH-18375)Victor Stinner2020-02-07
| | | | | | | 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.
* bpo-39274: Ensure Fraction.__bool__() returns a bool (GH-18017)Sebastian Berg2020-02-06
| | | | | 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.
* bpo-39350: Remove deprecated fractions.gcd() (GH-18021)Victor Stinner2020-01-16
| | | | Remove fractions.gcd() function, deprecated since Python 3.5 (bpo-22486): use math.gcd() instead.
* bpo-37819: Add Fraction.as_integer_ratio() (GH-15212)Raymond Hettinger2019-08-11
|
* bpo-35588: Speed up mod, divmod and floordiv operations for Fraction type ↵Stefan Behnel2019-01-02
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (#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.
* bpo-32968: Make modulo and floor division involving Fraction and float ↵Elias Zamaria2018-08-27
| | | | | | | 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.
* Issue #27832: Make _normalize parameter to Fraction.__init__ keyword-only.Mark Dickinson2016-08-23
|
* Issue #27539: Merge from 3.5.Mark Dickinson2016-08-22
|\
| * Issue #27539: Fix unnormalised Fraction.__pow__ result for negative exponent ↵Mark Dickinson2016-08-22
| | | | | | | | and base. Thanks Vedran Čačić.
* | Issue #25971: Optimized creating Fractions from floats by 2 times and fromSerhiy Storchaka2015-12-29
|/ | | | | | Decimals by 3 times. Unified error messages in float.as_integer_ratio(), Decimal.as_integer_ratio(), and Fraction constructors.
* Issue #22486: Added the math.gcd() function. The fractions.gcd() function ↵Serhiy Storchaka2015-05-13
| | | | | | now is deprecated. Based on patch by Mark Dickinson.
* Issue #21741: Update 147 test modules to use test discovery.Zachary Ware2015-04-13
| | | | | | | I have compared output between pre- and post-patch runs of these tests to make sure there's nothing missing and nothing broken, on both Windows and Linux. The only differences I found were actually tests that were previously *not* run.
* Issue #21136: Avoid unnecessary normalization in Fractions resulting from ↵Mark Dickinson2014-04-05
| | | | power and other operations.
* Issue #16469: Fraction(float('nan')) and Fraction(float('inf')) now raise ↵Mark Dickinson2012-11-15
| | | | ValueError and OverflowError (resp.), not TypeError.
* Issue #7652: Integrate the decimal floating point libmpdec library to speedStefan Krah2012-03-21
| | | | | up the decimal module. Performance gains of the new C implementation are between 12x and 80x, depending on the application.
* #14089: increase coverage of the fractions module. Patch by Oleg Plakhotnyuk.Ezio Melotti2012-02-29
|
* Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.Eric Smith2010-12-04
|
* #9424: Replace deprecated assert* methods in the Python test suite.Ezio Melotti2010-11-20
|
* Make Fraction(-1).__hash__() return -2 rather than -1 (see issue 10356).Mark Dickinson2010-11-13
|
* Issue #8188: Comparisons between Decimal objects and other numericMark Dickinson2010-06-11
| | | | objects (Fraction, float, complex, int) now all function as expected.