From 98f5c5da7390b3c473b771cbc3d369c3e910894f Mon Sep 17 00:00:00 2001 From: Ivan Hetman Date: Mon, 23 Nov 2020 10:49:33 +0200 Subject: [PATCH] Add Math.floorMod and Math.floorDiv (#544) --- .../org/teavm/classlib/java/lang/TMath.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java index 22ea409f0..8a10cdeba 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TMath.java @@ -127,6 +127,32 @@ public final class TMath extends TObject { return (long) (a + signum(a) * 0.5); } + public static int floorDiv(int a, int b) { + int div = a / b; + return (a ^ b) < 0 && div * b != a ? div - 1 : div; + } + + public static long floorDiv(long a, int b) { + return floorDiv(a, (long) b); + } + + public static long floorDiv(long a, long b) { + long div = a / b; + return (a ^ b) < 0 && div * b != a ? div - 1 : div; + } + + public static int floorMod(int a, int b) { + return a - floorDiv(a, b) * b; + } + + public static int floorMod(long a, int b) { + return (int) (a - floorDiv(a, b) * b); + } + + public static long floorMod(long a, long b) { + return a - floorDiv(a, b) * b; + } + @Unmanaged public static double random() { return PlatformDetector.isC() ? randomC() : randomImpl();