From 128743b0a615056a3153c86281db696d97806e42 Mon Sep 17 00:00:00 2001 From: Verity Scheel Date: Wed, 14 Feb 2024 12:14:21 -0600 Subject: [PATCH] Fix intDiv for bigints by not using floats Agrees with behavior of JS Prelude for negative integers --- src/Data/EuclideanRing.erl | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/Data/EuclideanRing.erl b/src/Data/EuclideanRing.erl index 48078387..c215568a 100644 --- a/src/Data/EuclideanRing.erl +++ b/src/Data/EuclideanRing.erl @@ -1,20 +1,11 @@ -module(data_euclideanRing@foreign). -export([intDegree/1, intDiv/2, intMod/2, numDiv/2]). -intDegree(X) -> abs(X). +intDegree(X) -> erlang:abs(X). intDiv(_, 0) -> 0; -intDiv(X, Y) when Y > 0 -> floor_(X / Y); -intDiv(X, Y) -> -floor_(X / -Y). - -% Not supported natively until erlang 20 -floor_(X) -> - T = erlang:trunc(X), - case (X - T) of - Neg when Neg < 0 -> T - 1; - Pos when Pos > 0 -> T; - _ -> T - end. +intDiv(X, Y) when X < 0 -> (X - erlang:abs(Y) + 1) div (Y); +intDiv(X, Y) -> X div Y. intMod(_, 0) -> 0; intMod(X, Y) ->