Skip to content

Commit b0a6376

Browse files
Fix issue with quotient of very large numbers (#823)
Hi, could you please review patch to calculate the quotient using the `//` operator. It fixes #822. Thanks Co-authored-by: ikappaki <[email protected]> Co-authored-by: Chris Rink <[email protected]>
1 parent 9c34ff1 commit b0a6376

File tree

5 files changed

+23
-13
lines changed

5 files changed

+23
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3636
* Fix an issue where the constructors of types created by `deftype` and `defrecord` could not be called if they contained `-` characters (#777)
3737
* Fix issue with the variadic ampersand operator treated as a binding in macros (#772)
3838
* Fix a bug the variadic arg symbol was not correctly bound to `nil` when no variadic arguments were provided (#801)
39+
* Fix a bug where the quotient of very large numbers was incorrect (#822)
3940

4041
### Removed
4142
* Removed support for PyPy 3.8 (#785)

docs/differencesfromclojure.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ Libs
133133

134134
Support for Clojure libs is `planned <https://github.com/basilisp-lang/basilisp/issues/668>`_\.
135135

136+
.. _core_lib_differences:
137+
138+
core
139+
----------
140+
141+
The :lpy:fn:`quot` function utilizes the ``\\`` operator, resulting in a floor operation towards negative infinity. In contrast, Clojure rounds the result towards zero. Consequently, negative results exhibit a difference of -1 between Basilisp and Clojure.
142+
136143
.. _refs_and_transactions_differences:
137144

138145
Refs and Transactions
@@ -199,4 +206,4 @@ Basilisp includes ports of some of the standard libraries from Clojure which sho
199206
* :lpy:ns:`basilisp.shell` is a port of ``clojure.java.shell``
200207
* :lpy:ns:`basilisp.string` is a port of ``clojure.string``
201208
* :lpy:ns:`basilisp.test` is a port of ``clojure.test``
202-
* :lpy:ns:`basilisp.walk` is a port of ``clojure.walk``
209+
* :lpy:ns:`basilisp.walk` is a port of ``clojure.walk``

src/basilisp/core.lpy

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,14 +1094,16 @@
10941094
(operator/mod num div))
10951095

10961096
(defn ^:inline quot
1097-
"Returns the quotient of ``num`` and ``div``\\."
1097+
"Returns the quotient of ``num`` and ``div``\\.
1098+
1099+
The result is rounded towards negative infinity."
10981100
[num div]
1099-
(basilisp.lang.runtime/quotient num div))
1101+
(operator/floordiv num div))
11001102

1101-
(defn ^:inline rem
1103+
(defn rem
11021104
"Returns the remainder of ``num`` and ``div``\\."
11031105
[num div]
1104-
(- num (* div (quot num div))))
1106+
(math/remainder num div))
11051107

11061108
(defn ^:inline inc
11071109
"Increment the argument by 1."

src/basilisp/lang/runtime.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,11 +1342,6 @@ def _divide_ints(x: int, y: LispNumber) -> LispNumber:
13421342
return x / y
13431343

13441344

1345-
def quotient(num: LispNumber, div: LispNumber) -> LispNumber:
1346-
"""Return the integral quotient resulting from the division of num by div."""
1347-
return math.trunc(num / div)
1348-
1349-
13501345
@functools.singledispatch
13511346
def compare(x, y) -> int:
13521347
"""Return either -1, 0, or 1 to indicate the relationship between x and y.

tests/basilisp/core_test.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,15 @@ def test_mod(self, result, x, y):
414414
(3, 11, 3),
415415
(4, 12, 3),
416416
(1.0, 5.9, 3),
417-
(-1.0, -5.9, 3),
418-
(-3, -10, 3),
419-
(-3, 10, -3),
417+
(-2.0, -5.9, 3),
418+
(-4, -10, 3),
419+
(-4, 10, -3),
420420
(3, 10, 3),
421+
(
422+
44879032948094820938438942938402938402984209842098984209449032094205874758758475837584759347,
423+
448790329480948209384389429384029384029842098420989842094490320942058747587584758375847593471,
424+
10,
425+
),
421426
],
422427
)
423428
def test_quot(self, result, x, y):

0 commit comments

Comments
 (0)