Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [Python] Fix modulo with negative numbers using Python floored semantics instead of .NET truncated semantics for bigint (fixes #4462) (by @dbrattli)
* [Beam] Fix `System.String.Concat` with 4+ arguments not being supported (by @dbrattli)
* [TS/Python] Fix invalid `this` argument type in structs (#4453) (by @ncave)
* [JS/TS] Fix `N` format specifier (`ToString("N0")`, `String.Format("{0:N0}", ...)`) producing a trailing dot when precision is 0 (fix #2582) (by @MangelMaxime)
Expand Down
1 change: 1 addition & 0 deletions src/Fable.Compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

* [Python] Fix modulo with negative numbers using Python floored semantics instead of .NET truncated semantics for bigint (fixes #4462) (by @dbrattli)
* [Beam] Fix `System.String.Concat` with 4+ arguments not being supported (by @dbrattli)
* [TS/Python] Fix invalid `this` argument type in structs (#4453) (by @ncave)
* [JS/TS] Fix `N` format specifier (`ToString("N0")`, `String.Format("{0:N0}", ...)`) producing a trailing dot when precision is 0 (fix #2582) (by @MangelMaxime)
Expand Down
7 changes: 6 additions & 1 deletion src/fable-library-py/fable_library/big_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ def op_division(a: int, b: int) -> int:


def op_modulus(a: int, b: int) -> int:
return a % b
# .NET uses truncated remainder, Python uses floored remainder.
# They differ when the dividend and divisor have different signs.
r = a % b
if r != 0 and ((a < 0) != (b < 0)):
r -= b
return r


def op_right_shift(a: int, num_bits: int) -> int:
Expand Down
18 changes: 18 additions & 0 deletions tests/Python/TestArithmetic.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ let ``test Integer division doesn't produce floats`` () =
let ``test Infix modulo can be generated`` () =
4 % 3 |> equal 1

[<Fact>]
let ``test Infix modulo with negative numbers`` () =
-5 % 3 |> equal -2
5 % -3 |> equal 2
-5 % -3 |> equal -2

// [<Fact>]
// let ``test Math.DivRem works with bytes`` () =
// Math.DivRem(5y, 2y) |> equal struct (2y, 1y)
Expand Down Expand Up @@ -384,6 +390,12 @@ let ``test Int64 Integer division doesn't produce floats`` () =
let ``test Int64 Infix modulo can be generated`` () =
4L % 3L |> equal 1L

[<Fact>]
let ``test Int64 Infix modulo with negative numbers`` () =
-5L % 3L |> equal -2L
5L % -3L |> equal 2L
-5L % -3L |> equal -2L

[<Fact>]
let ``test Int64 Evaluation order is preserved by generated code`` () =
(4L - 2L) * 2L + 1L |> equal 5L
Expand Down Expand Up @@ -446,6 +458,12 @@ let ``test BigInt Integer division doesn't produce floats`` () =
let ``test BigInt Infix modulo can be generated`` () =
4I % 3I |> equal 1I

[<Fact>]
let ``test BigInt Infix modulo with negative numbers`` () =
-5I % 3I |> equal -2I
5I % -3I |> equal 2I
-5I % -3I |> equal -2I

// [<Fact>]
// let ``test BigInt.DivRem works`` () = // See #1744
// let quotient,remainder = bigint.DivRem(5I,2I)
Expand Down
Loading