Skip to content

Commit cd074e3

Browse files
committed
add ceil and Limits
1 parent d898a40 commit cd074e3

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.sculk.math;
2+
3+
public class Limits {
4+
5+
public static final int UINT16_MAX = 0xFFFF;
6+
public static final int UINT16_MIN = 0;
7+
public static final int UINT32_MAX = 0xFFFFFFFF;
8+
public static final int UINT32_MIN = 0;
9+
}

src/main/java/org/sculk/math/SculkMath.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,62 @@
1717
*/
1818
public class SculkMath {
1919

20-
public static long log2(long N)
21-
{
20+
private static final float[] trigonometry = new float[Limits.UINT16_MAX + 1];
21+
22+
static final double M_2PI = Math.PI * 2.0D;
23+
24+
static final double M_PI2 = Math.PI / 2.0D;
2225

23-
// calculate log2 N indirectly
24-
// using log() method
26+
static {
27+
int short_max1 = Limits.UINT16_MAX + 1;
28+
for (int i = 0; i < short_max1; i++)
29+
trigonometry[i] = (float) Math.sin(i * M_2PI / short_max1);
30+
}
31+
32+
public static float sqrt(float paramFloat) {
33+
return (float) Math.sqrt(paramFloat);
34+
}
35+
36+
public static float sin(float paramFloat) {
37+
return trigonometry[((int) (paramFloat * 10430.378F) & Limits.UINT16_MAX)];
38+
}
39+
40+
public static float cos(float paramFloat) {
41+
return trigonometry[((int) (paramFloat * 10430.378F + 16384.0F) & Limits.UINT16_MAX)];
42+
}
43+
44+
public static float sin(double paramFloat) {
45+
return trigonometry[((int) (paramFloat * 10430.378F) & Limits.UINT16_MAX)];
46+
}
2547

48+
public static float cos(double paramFloat) {
49+
return trigonometry[((int) (paramFloat * 10430.378F + 16384.0F) & Limits.UINT16_MAX)];
50+
}
51+
52+
public static long log2(long N)
53+
{
2654
return (long)(Math.log(N) / Math.log(2));
2755
}
2856

2957
public static int log2(int N)
3058
{
59+
return (int)(Math.log(N) / Math.log(2));
60+
}
61+
62+
public static int ceil(float value)
63+
{
64+
int truncate = (int) value;
65+
return value > truncate ? truncate + 1 : truncate;
66+
}
67+
public static int clamp(int check, int min, int max) {
68+
return check > max ? max : (Math.max(check, min));
69+
}
3170

32-
// calculate log2 N indirectly
33-
// using log() method
71+
public static double denormalizeClamp(double lowerBnd, double upperBnd, double slide) {
72+
return slide < 0.0D ? lowerBnd : (slide > 1.0D ? upperBnd : lowerBnd + (upperBnd - lowerBnd) * slide);
73+
}
3474

35-
return (int)(Math.log(N) / Math.log(2));
75+
public static float denormalizeClamp(float lowerBnd, float upperBnd, float slide) {
76+
return slide < 0.0f ? lowerBnd : (slide > 1.0f ? upperBnd : lowerBnd + (upperBnd - lowerBnd) * slide);
3677
}
3778
}

0 commit comments

Comments
 (0)