Skip to content

Commit

Permalink
add ceil and Limits
Browse files Browse the repository at this point in the history
  • Loading branch information
SenseiTarzan committed Dec 26, 2024
1 parent d898a40 commit cd074e3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/sculk/math/Limits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sculk.math;

public class Limits {

public static final int UINT16_MAX = 0xFFFF;
public static final int UINT16_MIN = 0;
public static final int UINT32_MAX = 0xFFFFFFFF;
public static final int UINT32_MIN = 0;
}
55 changes: 48 additions & 7 deletions src/main/java/org/sculk/math/SculkMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,62 @@
*/
public class SculkMath {

public static long log2(long N)
{
private static final float[] trigonometry = new float[Limits.UINT16_MAX + 1];

static final double M_2PI = Math.PI * 2.0D;

static final double M_PI2 = Math.PI / 2.0D;

// calculate log2 N indirectly
// using log() method
static {
int short_max1 = Limits.UINT16_MAX + 1;
for (int i = 0; i < short_max1; i++)
trigonometry[i] = (float) Math.sin(i * M_2PI / short_max1);
}

public static float sqrt(float paramFloat) {
return (float) Math.sqrt(paramFloat);
}

public static float sin(float paramFloat) {
return trigonometry[((int) (paramFloat * 10430.378F) & Limits.UINT16_MAX)];
}

public static float cos(float paramFloat) {
return trigonometry[((int) (paramFloat * 10430.378F + 16384.0F) & Limits.UINT16_MAX)];
}

public static float sin(double paramFloat) {
return trigonometry[((int) (paramFloat * 10430.378F) & Limits.UINT16_MAX)];
}

public static float cos(double paramFloat) {
return trigonometry[((int) (paramFloat * 10430.378F + 16384.0F) & Limits.UINT16_MAX)];
}

public static long log2(long N)
{
return (long)(Math.log(N) / Math.log(2));
}

public static int log2(int N)
{
return (int)(Math.log(N) / Math.log(2));
}

public static int ceil(float value)
{
int truncate = (int) value;
return value > truncate ? truncate + 1 : truncate;
}
public static int clamp(int check, int min, int max) {
return check > max ? max : (Math.max(check, min));
}

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

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

0 comments on commit cd074e3

Please sign in to comment.