classlib: add methods/fields from JDK 21 to Math (#784)

This commit is contained in:
Ivan Hetman 2023-10-08 12:45:27 +03:00 committed by GitHub
parent 7e761ca7e9
commit a493d003e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 0 deletions

View File

@ -27,6 +27,7 @@ import org.teavm.interop.Unmanaged;
public final class TMath extends TObject { public final class TMath extends TObject {
public static final double E = 2.71828182845904523536; public static final double E = 2.71828182845904523536;
public static final double PI = 3.14159265358979323846; public static final double PI = 3.14159265358979323846;
public static final double TAU = 2 * PI;
private TMath() { private TMath() {
} }
@ -516,4 +517,32 @@ public final class TMath extends TObject {
} }
return TFloat.intBitsToFloat(bits); return TFloat.intBitsToFloat(bits);
} }
public static int clamp(long value, int min, int max) {
if (min > max) {
throw new IllegalArgumentException();
}
return (int) Math.min(max, Math.max(value, min));
}
public static long clamp(long value, long min, long max) {
if (min > max) {
throw new IllegalArgumentException();
}
return Math.min(max, Math.max(value, min));
}
public static double clamp(double value, double min, double max) {
if (!(min < max) && (Double.isNaN(min) || Double.isNaN(max) || Double.compare(min, max) > 0)) {
throw new IllegalArgumentException();
}
return Math.min(max, Math.max(value, min));
}
public static float clamp(float value, float min, float max) {
if (!(min < max) && (Float.isNaN(min) || Float.isNaN(max) || Float.compare(min, max) > 0)) {
throw new IllegalArgumentException();
}
return Math.min(max, Math.max(value, min));
}
} }

View File

@ -18,6 +18,7 @@ package org.teavm.classlib.java.lang;
public final class TStrictMath extends TObject { public final class TStrictMath extends TObject {
public static final double E = 2.71828182845904523536; public static final double E = 2.71828182845904523536;
public static final double PI = 3.14159265358979323846; public static final double PI = 3.14159265358979323846;
public static final double TAU = 2 * PI;
private TStrictMath() { private TStrictMath() {
} }
@ -229,4 +230,20 @@ public final class TStrictMath extends TObject {
public static float nextUp(float f) { public static float nextUp(float f) {
return TMath.nextUp(f); return TMath.nextUp(f);
} }
public static int clamp(long value, int min, int max) {
return TMath.clamp(value, min, max);
}
public static long clamp(long value, long min, long max) {
return TMath.clamp(value, min, max);
}
public static double clamp(double value, double min, double max) {
return TMath.clamp(value, min, max);
}
public static float clamp(float value, float min, float max) {
return TMath.clamp(value, min, max);
}
} }