Skip to content

Commit

Permalink
Fix a bug where integer results of integer division are always return…
Browse files Browse the repository at this point in the history
…ed as fractions (#1141)

Fixes #1140
  • Loading branch information
chrisrink10 authored Nov 22, 2024
1 parent e83b059 commit 8fcc065
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix a bug where tags in data readers were resolved as Vars within syntax quotes, rather than using standard data readers rules (#1129)
* Fix a bug where `keyword` and `symbol` functions did not treat string arguments as potentially namespaced (#1131)
* Fix a bug where `condp` would throw an exception if a result expression was `nil` (#1137)
* Fix a bug where integer division which resulted in an integer would return a `fractions.Fraction` (#1140)

## [v0.3.2]
### Added
Expand Down
4 changes: 3 additions & 1 deletion src/basilisp/lang/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,9 @@ def divide(x: LispNumber, y: LispNumber) -> LispNumber:
@divide.register(int)
def _divide_ints(x: int, y: LispNumber) -> LispNumber:
if isinstance(y, int):
return Fraction(x, y)
frac = Fraction(x, y)
# fractions.Fraction.is_integer() wasn't added until 3.12
return frac.numerator if frac.denominator == 1 else frac
return x / y


Expand Down
18 changes: 18 additions & 0 deletions tests/basilisp/runtime_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fractions
import platform
import sys
from decimal import Decimal
Expand Down Expand Up @@ -599,6 +600,23 @@ def test_not_equals(v1, v2):
assert not runtime.equals(v2, v1)


@pytest.mark.parametrize(
"v1,v2,expected_result,result_type",
[
(3, 3, 1, int),
(3, 1, 3, int),
(3, 1.001, 3 / 1.001, float),
(3000, 1000, 3, int),
(3000, 1001, fractions.Fraction(3000, 1001), fractions.Fraction),
(3001, 1000, fractions.Fraction(3001, 1000), fractions.Fraction),
],
)
def test_divide(v1, v2, expected_result, result_type):
result = runtime.divide(v1, v2)
assert result == expected_result
assert isinstance(result, result_type)


def test_pop_thread_bindings():
with pytest.raises(runtime.RuntimeException):
runtime.pop_thread_bindings()
Expand Down

0 comments on commit 8fcc065

Please sign in to comment.