262 Commits

Author SHA1 Message Date
Sergey B Kirpichev d3812b1194 Amend bc96a889 2026-08-21 08:25:46 +03:00
Sergey B Kirpichev bc96a88926 Apply naive algorithm for huge exponents 2026-08-21 04:14:33 +03:00
Sergey B Kirpichev 0d2af59d5d Speedup exponent computation 2026-08-20 12:46:40 +03:00
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
Vincent Gao 2de2d509ec Widen to_str's digit window so str/nstr rounds near-boundary values correctly (#1139)
to_str extracted only dps+10 digits, narrower than format_scientific and
format_fixed which cover the whole mantissa.  A value just above a decimal
boundary was then extracted as "...99999" one ULP low, so directed rounding
through str/nstr fell one ULP short of the 'e' format and the exact value.
Widen the base-10 window to match the sibling formatters.

Also fixed test for from_str(), coming from 30e8001e
2026-07-22 00:05:32 +03:00
Sergey B Kirpichev 65dca57ca7 Use CoW pattern to manage constant_memo() cache
Closes #1135
2026-07-19 10:28:41 +03:00
Sergey B Kirpichev d8c9fd2ea3 Refactor round_digits()
* add exponent argument
* pass original number
* make inexact flag local
2026-07-19 04:07:42 +03:00
Vincent Gao 101a8379a8 Fix last-digit rounding of the "f" format type (#1132)
format_fixed() extracts a handful of guard digits and lets round_digits()
decide, so a nonzero remainder lying past them was invisible: directed and
nearest rounding truncated the last digit instead of rounding it up.  The
value is dyadic, so its exactness at the extracted digits is decidable;
pass that as a inexact flag and round on it.  The "e" path and MPFR already
round these correctly.

Values whose leading digit falls past the last requested place skipped
round_digits() altogether and always printed zeros, even under rounding
away from zero; round them to one unit in the last place in that case.

Closes #1131

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
2026-07-18 07:33:23 +03:00
Sergey B Kirpichev c44efa2ca2 Remove py2 remnants (cast round() to int) 2026-07-01 08:08:19 +03:00
Sergey B Kirpichev 6f1fbd275d Fix repr_dps() to produce a correct estimate
That should work at lest for 32-bit integers.  Though, I think that
the repr/str representation loses all sence for precisions much
less than that ;-)

Closes #1116
2026-07-01 05:36:13 +03:00
Sergey B Kirpichev 1d89343973 Add jacobi_symbol() to libintmath 2026-06-23 07:16:42 +03:00
Sergey B Kirpichev 16d5fb332c Merge pull request #1091 from skirpichev/misc
Misc fixes
2026-05-26 08:39:00 +03:00
Sergey B Kirpichev 52e0daaaeb Drop private alias round_fast 2026-05-26 07:57:20 +03:00
Sergey B Kirpichev 44859bf6cd Change default rounding for private mpf/mpc_expj/jpi() 2026-05-26 07:57:20 +03:00
Sergey B Kirpichev 34691c49f8 Change default rounding mode for private formatting helpers 2026-05-26 07:57:20 +03:00
Sergey B Kirpichev f7579c0a0d Merge pull request #1089 from skirpichev/restrict-libmp-exports/704
Restrict libmp exports to public API
2026-05-26 04:53:45 +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 b06b1f51ce Add extra precision for summation in mpf_hypot()
Also, respect rounding option.

Closes #1011
2026-05-25 03:32:24 +03:00
Sergey B Kirpichev 1eb70ed3d8 Clarify to_float() docstring
Second paragraph rephrased to:
> If the magnitude of *rounded* number is too large to represent as
> a regular float, it will be converted to infinity.

Closes #1085
2026-05-24 05:50:52 +03:00
Sergey B Kirpichev 87cfbc42a8 Correct to_float() conversion for subnormals
Closes #1078
2026-05-18 07:46:12 +03:00
Sergey B Kirpichev 5393b8d55e Support special numbers in mpf_frexp() like math.frexp()
Closes #1080
2026-05-14 06:10:42 +03:00
Sergey B Kirpichev eefbdcb6ad Improve documentation about rounding in the mp context
See #1076

Also (small optimization) unify _normalize and normalize.
2026-05-13 07:55:29 +03:00
Sergey B Kirpichev 8a965fe43b Speedup removal trailing zeros in _normalize/from_man_exp()
| Benchmark      | ref     | patch                   |
|----------------|:-------:|:-----------------------:|
| 2              | 404 ns  | 398 ns: 1.01x faster    |
| 2**3           | 403 ns  | 398 ns: 1.01x faster    |
| 2**10          | 1.96 us | 1.91 us: 1.03x faster   |
| 2**100         | 6.21 us | 2.52 us: 2.47x faster   |
| 2**10000       | 1.04 ms | 5.90 us: 175.48x faster |
| Geometric mean | (ref)   | 2.78x faster            |

Benchmark hidden because not significant (1): 2**10000 + 1

$ cat bench.py
import pyperf
from mpmath.libmp import from_int
runner = pyperf.Runner()
for s in ['2', '2**3', '2**10', '2**100',
          '2**10000', '2**10000 + 1']:
    i = eval(s)
    runner.bench_func(s, from_int, i)

Closes #1073

Co-authored-by: Fredrik Johansson <fredrik.johansson@gmail.com>
2026-04-19 06:27:32 +03:00
Sergey B Kirpichev 893bb431aa Merge pull request #1057 from skirpichev/drop-deprecated1
Remove deprecated behavior
2026-03-07 04:18:42 +03:00
Sergey B Kirpichev 8ffd139b16 Merge pull request #1059 from skirpichev/misc
Misc fixes
2026-03-07 04:17:26 +03:00
Sergey B Kirpichev 4b84174154 Fix typo in mpc_fibonacci() 2026-03-07 03:30:49 +03:00
Sergey B Kirpichev 22bbe9ad4c Amend 5ac8dcda 2026-03-07 03:01:50 +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 81204d1105 Drop deprecated to/from_pickable() functions 2026-03-04 07:28:50 +03:00
Sergey B Kirpichev 754c073e7d Drop deprecated bitcount() function 2026-03-04 07:28:49 +03:00
Sergey B Kirpichev 60de9789bf Keep forever deprecated aliases for mpc/mpf_log() 2026-03-04 07:28:49 +03:00
Sergey B Kirpichev 9677a2a939 Amend docstring for round_digits() 2026-03-04 07:23:41 +03:00
Sergey B Kirpichev 2f6a5d52f3 Drop support for CPython 3.9
(Following https://github.com/sympy/sympy/pull/28816)
2026-03-04 07:14:17 +03:00
Sergey B Kirpichev 90ccd8c40e Amend beb5029
Closes #1035
2026-02-21 01:50:50 +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 510d3c8617 Don't require double_fac/fib from backend 2025-12-29 11:38:41 +03:00
hnb22 baaccb59aa Switch logarithm implementation to taylor series when input is close to 1 (#957)
This modifies the logarithm implementation for cases where taylor series
is more optimal (cancellation >= working precision), providing a faster
path for low precision cases depending on how close input is to 1.
2025-09-03 05:31:22 +03:00
Sergey B Kirpichev ad91359172 Use threading.local storage for global caches 2025-09-01 06:21:07 +03:00
Sergey B Kirpichev 8870f1d258 Add aliases for mpf/mpc_log() as mpf/mpc_ln()
Closes #988
2025-08-06 06:02:12 +03:00
Sergey B Kirpichev b6af936582 Make mpc_hash() compatible with complex hash
Closes #985
2025-08-06 05:26:42 +03:00
Sergey B Kirpichev 5d66b10f53 Use Fraction's with the python-gmp 2025-07-03 22:16:18 +03:00
Sergey B Kirpichev 027bfd36ed Merge pull request #973 from skirpichev/references/224
Move references to docs/references.rst
2025-06-19 19:10:24 +03:00
Sergey B Kirpichev 2cad89428e Move references to docs/references.rst
Closes #224
2025-06-18 18:03:46 +03:00
That's ¬a problem d5f370a68a Fix typo in mpf_gamma() docstring 2025-06-18 07:53:03 +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 936039cd6c precision -> dps in formatting helpers 2025-06-08 13:42:27 +03:00
Sergey B Kirpichev ed1ad32051 Rename kwarg rounding -> rnd in new helpers 2025-06-08 12:59:50 +03:00
Sergey B Kirpichev 4b26ebf276 Rename argument rounding -> rnd in private *_besseljn() functions 2025-06-08 12:59:45 +03:00