117 Commits

Author SHA1 Message Date
Sergey B Kirpichev 70b722800f Use shortest decimal representations for str/repr
Was:
```
$ python -m mpmath
>>> 0.1
0.10000000000000001
```

Now:
```
$ python -m mpmath --shortest-str
>>> 0.1
0.1
```

Closes #1028
2026-08-20 12:46:40 +03:00
Sergey B Kirpichev 4a3af8ef2b Use round_nearest in repr/str and as default mpf's rounding mode
Better alternative to #1137 (which was partially reverted).

Closes #1129
2026-08-05 09:47:43 +03:00
Sergey B Kirpichev fb69943530 Use explicit kwargs in public API, where possible
Following functions kept:
```pycon
>>> import inspect
... with_args = []
... with_kwargs = []
... for n in dir(mpmath):
...     m = getattr(mpmath, n)
...     try:
...         s = inspect.signature(m)
...     except:
...         continue
...     if any(_.kind == inspect._ParameterKind.VAR_POSITIONAL for _ in s.parameters.values()):
...         for name in s.parameters:
...             if s.parameters[name].kind == inspect._ParameterKind.VAR_POSITIONAL and name == 'args':
...                 with_args.append(n)
...                 break
...     if any(_.kind == inspect._ParameterKind.VAR_KEYWORD for _ in s.parameters.values()):
...         with_kwargs.append(n)
... print(with_args)
... print(with_kwargs)
...
['arange', 'ellipe', 'ellippi', 'linspace', 'matrix', 'ones', 'timing', 'zeros']
['multiplicity', 'timing']
```

We need support for multiple signatures in the first case.  In the
second - it's impossible to implement these functions without kwargs.

Closes #1056
2026-08-04 06:40:53 +03:00
Sergey B Kirpichev d30d632c39 Use reversed rounding mode for repr
Closes #1129
2026-07-19 04:59:27 +03:00
Sergey B Kirpichev c328efcd37 Change signature for wrapped libmp's functions
See #1056
2026-07-11 07:17:59 +03:00
Sergey B Kirpichev 0e1bd9496d Restrict libmp exports to public API
This is following namespace:
```
{'BACKEND', 'ComplexResult', 'MPZ', 'MPZ_ONE', 'NoConvergence',
 'catalan_fixed', 'dps_to_prec', 'euler_fixed', 'fhalf', 'finf',
 'fnan', 'fninf', 'fnone', 'fone', 'from_float', 'from_int', 'from_man_exp',
 'from_rational', 'from_str', 'fzero', 'giant_steps', 'ifac', 'ifib',
 'int_types', 'isqrt', 'mpc_abs', 'mpc_exp', 'mpc_pow', 'mpc_pow_int',
 'mpc_pow_mpf', 'mpc_sqrt', 'mpf_abs', 'mpf_add', 'mpf_atan', 'mpf_atan2',
 'mpf_bernoulli', 'mpf_ceil', 'mpf_cmp', 'mpf_cos', 'mpf_cosh_sinh', 'mpf_div',
 'mpf_e', 'mpf_eq', 'mpf_exp', 'mpf_floor', 'mpf_ge', 'mpf_gt', 'mpf_le',
 'mpf_log', 'mpf_lt', 'mpf_mod', 'mpf_mul', 'mpf_neg', 'mpf_pi', 'mpf_pow',
 'mpf_pow_int', 'mpf_shift', 'mpf_sign', 'mpf_sin', 'mpf_sqrt', 'mpf_sub',
 'mpf_tan', 'normalize', 'phi_fixed', 'prec_to_dps', 'repr_dps',
 'round_nearest', 'sqrtrem', 'to_float', 'to_int', 'to_man_exp', 'to_rational',
 'to_str'}
```

Closes #704
2026-05-25 08:31:19 +03:00
Sergey B Kirpichev ca0f67d9ab Use signed=True per default in to_man_exp() 2026-03-04 07:28:50 +03:00
Sergey B Kirpichev 7cf16b7828 Revert "Add isspecial() method for mp/fp contexts, deprecate isnormal()"
This reverts commit e6aa3b46b1.

Subnormals are properly handled by fp.isnormal().  Deprecate
fp.is_special() method.

Fixes #946
2026-02-23 04:08:07 +03:00
Sergey B Kirpichev bfe0ffdb52 Change behavior of 'b' formatting type to mimic 'a'
This behavior is compatible with the gmpy2.

Closes #1027
2026-02-01 04:03:19 +03:00
Sergey B Kirpichev bd46aacf5d Correct mpc.__format__() docstring 2026-01-21 04:34:23 +03:00
Warren Weckesser 82e72a72d7 Ensure exp, sin, tan, etc have a correct __name__ attribute 2025-09-06 16:32:42 -04:00
Sergey B Kirpichev cd52b6da6b Cleanup ctx_mp_python.py (consistent argument naming) 2025-06-18 07:53:01 +03:00
Sergey B Kirpichev 9ad6a13925 Fix from_man_exp to correctly reject non-integral mantissa
This partially reverts 25506567
2025-06-09 05:21:57 +03:00
Sergey B Kirpichev bad54feab6 Use context's rounding mode per default in the format() 2025-06-08 18:27:16 +03:00
Sergey B Kirpichev 2b72f9f3a0 Rename to_str() argument: rounding -> rnd 2025-06-08 15:28:06 +03:00
Sergey B Kirpichev cf22ea86be Amend 4582b137
Use mp._prec_rounding[1] instead of _rounding attribute
for compatibility with the custom MPContext context in
Diofant/SymPy (polys).
2025-06-03 22:00:37 +03:00
Sergey B Kirpichev 8b48864418 Fix mpf.__round__() to return int type per default (ndigits=None) 2025-06-03 11:24:13 +03:00
Sergey B Kirpichev 4582b13795 Support rounding property for the mp context 2025-06-01 13:28:51 +03:00
Sergey B Kirpichev 8778568951 Avoid using ";" in doctests, esp with side effects
The new CPython repl hide all output, except for the last statement.
See https://github.com/python/cpython/issues/131217.
2025-05-01 10:41:48 +03:00
Sergey B Kirpichev e6aa3b46b1 Add isspecial() method for mp/fp contexts, deprecate isnormal()
Closes #946
2025-04-28 08:36:15 +03:00
Sergey B Kirpichev 6c51c9d898 Fix new-style formatting without type specifier (repr_dps vs dps)
Closes #943
2025-04-23 07:04:24 +03:00
Sergey B Kirpichev a14901da43 Add pretty_dps context property to control number of printed digits
Closes #921
2025-03-30 09:13:43 +03:00
Sergey B Kirpichev 2550656706 Make sure we pass MPZ mantissa to from_man_exp() 2025-01-29 04:00:25 +03:00
Sergey B Kirpichev b9ac31f5cd Better introspection support for decorated functions
Closes #899
2025-01-19 07:33:28 +03:00
Sergey B Kirpichev fc3f041b42 Add MPFR-compatible aliases for rounding modes and documentation
Closes #890
2024-12-25 04:50:35 +03:00
Sergey B Kirpichev 9580374595 Use convert(strings=False) in _mpc.mpc_convert_lhs() 2024-10-03 14:36:07 +03:00
Sergey B Kirpichev 677683efcc Implement _mpf.mpf_convert_rhs() with ctx.convert() 2024-10-03 13:53:19 +03:00
Sergey B Kirpichev d3fccbc52d Cleanup mpmath/ctx_mp_python.py 2024-10-03 12:35:34 +03:00
Sergey B Kirpichev 51872ef8a4 Implement mpf.__floordiv__() and mpf.__divmod__()
Closes #333
2024-09-29 12:38:46 +03:00
Sergey B Kirpichev ea87cafabe Support 'b' (binary) format type for mpf/mpc
Closes #866
2024-09-23 07:50:35 +03:00
Sergey B Kirpichev 21fed47e6d Merge pull request #826 from skirpichev/round-455
mpf.__round__() returns mpf
2024-09-14 06:32:39 +03:00
Sergey B Kirpichev 4669219ed1 Add mpc.__format__()
Closes #846
2024-09-09 05:39:14 +03:00
Sergey B Kirpichev 40c16e98c1 Implement 'a'/'A' formating types for mpf.__format__ 2024-09-08 07:31:11 +03:00
Sergey B Kirpichev a545ecbd9e mpf.__round__() returns mpf
Probably this will look non-intuitive, but underlying floating-point
arithmetic is binary, not decimal.  Thus, round(x, n) sometimes will pick
up a decimal representation with more than n digits.  This is something
common for CPython < 2.7 and < 3.1:

    Python 2.6.6 (r266:84292, Aug 12 2014, 07:57:07)
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> round(0.42754287856598971, 2)
    0.42999999999999999

And with this patch as well:

    Python 3.12.5+ (heads/3.12:0181aa2e3e, Aug 29 2024, 14:55:08) [GCC 12.2.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from mpmath import *
    >>> round(mp.mpf(0.42754287856598971), 2)
    mpf('0.42999999999999999')
    >>> mp.pretty = True
    >>> round(mp.mpf(0.42754287856598971), 2)
    0.43

See also python/cpython#45921

Closes #455
2024-09-07 07:32:59 +03:00
Sergey B Kirpichev 4be9a0261e Reorder hasattr() checks in mpf.mpf_convert_arg() and mp.convert()
Taken from https://github.com/sagemath/sage/pull/38565#issuecomment-2311204724
2024-08-29 18:10:22 +03:00
Javier Garcia 9b99f418c7 Add documentation for mpf.__format__ (#850)
Adds a bit of documentation in the Utility Functions section.

Closes #337

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
2024-08-28 04:48:15 +03:00
Sergey B Kirpichev 4b9ebf41f4 Support '%' presentation type for mpf
Closes #837
2024-08-22 13:41:45 +03:00
Sergey B Kirpichev fc92ec7875 Normalize mpf in mp.npconvert() and lower precision in tests
On most systems np.float128 aren't IEEE 754 quadruples.

Closes #836
2024-08-18 12:35:12 +03:00
Sergey B Kirpichev 5a155f8e09 Drop sign from nstr(mpf('inf')) output
Closes #827
2024-08-01 12:27:58 +03:00
Sergey B Kirpichev 90a42bcb4f Fix mp.convert (missing rounding mode for from_rational) 2024-08-01 12:25:08 +03:00
Sergey B Kirpichev caadde2575 Support conversion from scalar ndarray's
Closes #824
2024-07-31 06:39:11 +03:00
Javier Garcia cead3b2bf9 Implement mpf.__format__ (#819)
Implemented the `__format__` method for the `_mpf` type.

Currently, `f`, `F`, `g`, `G`, `e`, and `E` formats are implemented, following the same specifications as specified for regular floats [here](https://docs.python.org/3/library/string.html#formatspec).  Only nearest rounding is implemented.

A partial fix for #337.
2024-07-24 19:37:04 +03:00
Sergey B Kirpichev 842cd42907 Amend d3c6428 (mp.prec default should match fp.prec) 2024-07-20 12:28:49 +03:00
Sergey B Kirpichev fdb8bc09f4 Add mpc_mpf_sub() 2024-06-05 10:40:32 +03:00
Sergey B Kirpichev b180c95c2b Move around mpc() methods... 2024-06-05 10:35:59 +03:00
Sergey B Kirpichev bfa51f7f1e Move string handling to _mpf.__new__() 2024-06-05 10:35:59 +03:00
Sergey B Kirpichev 08c55b7715 Drop special cases in arithmetic helpers of mpf 2024-06-05 10:35:59 +03:00
Sergey B Kirpichev 2283c89611 Revert dcc591c
This uses per-function workarounds to fall through to ctx.hypercomb()
2024-05-01 09:16:45 +03:00
Sergey B Kirpichev b5c4dda969 Temporary fix for SymPy, regression from #792 2024-04-30 19:19:59 +03:00
Sergey B Kirpichev 08cbde4bfb Merge pull request #792 from skirpichev/inf-prec-520
Fallback to fixed precision if prec change is infinite
2024-04-30 06:28:54 +03:00