com.levigo.util.base
Class Numbers

java.lang.Object
  extended by com.levigo.util.base.Numbers

public class Numbers
extends Object

Credit for the floor(float), ceil(float) and round(float) methods: riven: http://riven8192.blogspot.com/2010/02/fastmath-fast-floor.html


Method Summary
static void assertIsWithin(String name, double value, double start, double end)
          Throws an IllegalArgumentException if double value does not lie within the closed double interval [start..stop].
static void assertIsWithin(String name, float value, float start, float end)
          Throws an IllegalArgumentException if float value does not lie within the closed float interval [start..stop].
static void assertIsWithin(String name, int value, int start, int end)
          Throws an IllegalArgumentException if integer value does not lie within the closed integer interval [start..stop].
static int ceil(double x)
          A fast implementation of Math.ceil(double).
static int ceil(float x)
          A fast implementation of Math.ceil(double) for floats.
static double clamp(double value, double min, double max)
          Clamp the value into the range [min..max].
static float clamp(float value, float min, float max)
          Clamp the value into the range [min..max].
static int clamp(int value, int min, int max)
          Clamp the value into the range [min..max].
static long clamp(long value, long min, long max)
          Clamp the value into the range [min..max].
static short doubleTof2Dot14(double d)
          Converts double to 2 bytes with F2Dot14 notation
static double f2Dot14ToDouble(short i)
          Converts 16-bit (2 bytes) with F2Dot14 notation to double
static int floor(double x)
          A fast implementation of Math.floor(double).
static int floor(float x)
          A fast implementation of Math.floor(double) for floats.
static boolean isNeighbor(double value, double p, double epsilon)
          Return whether the given value lies within a neighborhood of size 2*epsilon of p.
static boolean isNeighbor(int value, int p, int epsilon)
          Return whether the given value lies within a neighborhood of size 2*epsilon of p.
static boolean isWithin(double value, double start, double end)
          Return whether the given value lies within the closed interval [begin..end].
static boolean isWithin(float value, float start, float end)
          Return whether the given value lies within the closed interval [begin..end].
static boolean isWithin(int value, int start, int end)
          Return whether the given value lies within the closed interval [begin..end].
static void longToBytesBE(long l, byte[] buffer, int offset)
          Store the given long in big-endian format into the given buffer at the given offset.
static void longToBytesLE(long l, byte[] buffer, int offset)
          Store the given long in little-endian format into the given buffer at the given offset.
static float max(float... v)
          Return the maximum of all values in the given array.
static int max(int... v)
          Return the maximum of all values in the given array.
static float min(float... v)
          Return the minimum of all values in the given array.
static int min(int... v)
          Return the minimum of all values in the given array.
static int round(double x)
          A fast implementation of Math.round(double).
static int round(float x)
          A fast implementation of Math.round(double) for floats.
static int toInfinity(double d)
          Round to the next integer so that the rounded value is equal or farther away from zero than the input value.
static int toZero(double d)
          Round to the next integer so that the rounded value is equal or closer to zero than the input value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

clamp

public static int clamp(int value,
                        int min,
                        int max)
Clamp the value into the range [min..max].

Parameters:
value -
min -
max -
Returns:
the clamped value

clamp

public static float clamp(float value,
                          float min,
                          float max)
Clamp the value into the range [min..max].

Parameters:
value -
min -
max -
Returns:
the clamped value

clamp

public static double clamp(double value,
                           double min,
                           double max)
Clamp the value into the range [min..max].

Parameters:
value -
min -
max -
Returns:
the clamped value

clamp

public static long clamp(long value,
                         long min,
                         long max)
Clamp the value into the range [min..max].

Parameters:
value -
min -
max -
Returns:
the clamped value

min

public static int min(int... v)
Return the minimum of all values in the given array. If the array is empty, Integer.MAX_VALUE will be returned.


max

public static int max(int... v)
Return the maximum of all values in the given array. If the array is empty, Integer.MIN_VALUE will be returned.


min

public static float min(float... v)
Return the minimum of all values in the given array. If the array is empty, Float.MAX_VALUE will be returned.


max

public static float max(float... v)
Return the maximum of all values in the given array. If the array is empty, Float.MIN_VALUE will be returned.


isWithin

public static boolean isWithin(int value,
                               int start,
                               int end)
Return whether the given value lies within the closed interval [begin..end].

Parameters:
value -
start -
end -
Returns:

isWithin

public static boolean isWithin(float value,
                               float start,
                               float end)
Return whether the given value lies within the closed interval [begin..end].

Parameters:
value -
start -
end -
Returns:

isWithin

public static boolean isWithin(double value,
                               double start,
                               double end)
Return whether the given value lies within the closed interval [begin..end].

Parameters:
value -
start -
end -
Returns:

assertIsWithin

public static void assertIsWithin(String name,
                                  int value,
                                  int start,
                                  int end)
Throws an IllegalArgumentException if integer value does not lie within the closed integer interval [start..stop].

Parameters:
name - the name of the value variable: use in the exception message
value - the integer to be tested
start - the lower end of the interval: if value is less than start then throw an IllegalArgumentException
end - the upper end of the interval: if value is greater than end then throw an IllegalArgumentException

assertIsWithin

public static void assertIsWithin(String name,
                                  float value,
                                  float start,
                                  float end)
Throws an IllegalArgumentException if float value does not lie within the closed float interval [start..stop].

Parameters:
name - the name of the value variable: use in the exception message
value - the float to be tested
start - the lower end of the interval: if value is less than start then throw an IllegalArgumentException
end - the upper end of the interval: if value is greater than end then throw an IllegalArgumentException

assertIsWithin

public static void assertIsWithin(String name,
                                  double value,
                                  double start,
                                  double end)
Throws an IllegalArgumentException if double value does not lie within the closed double interval [start..stop].

Parameters:
name - the name of the value variable: use in the exception message
value - the double to be tested
start - the lower end of the interval: if value is less than start then throw an IllegalArgumentException
end - the upper end of the interval: if value is greater than end then throw an IllegalArgumentException

isNeighbor

public static boolean isNeighbor(double value,
                                 double p,
                                 double epsilon)
Return whether the given value lies within a neighborhood of size 2*epsilon of p.

Parameters:
value -
p - the point
epsilon -
Returns:

isNeighbor

public static boolean isNeighbor(int value,
                                 int p,
                                 int epsilon)
Return whether the given value lies within a neighborhood of size 2*epsilon of p.

Parameters:
value -
p - the point
epsilon -
Returns:

floor

public static int floor(float x)
A fast implementation of Math.floor(double) for floats.

Parameters:
x - the argument
Returns:

round

public static int round(float x)
A fast implementation of Math.round(double) for floats.

Parameters:
x - the argument
Returns:

ceil

public static int ceil(float x)
A fast implementation of Math.ceil(double) for floats.

Parameters:
x - the argument
Returns:

floor

public static int floor(double x)
A fast implementation of Math.floor(double).

Parameters:
x - the argument
Returns:

round

public static int round(double x)
A fast implementation of Math.round(double).

Parameters:
x - the argument
Returns:

ceil

public static int ceil(double x)
A fast implementation of Math.ceil(double).

Parameters:
x - the argument
Returns:

toZero

public static int toZero(double d)
Round to the next integer so that the rounded value is equal or closer to zero than the input value. In other words: for values > zero round like floor(double) would, otherwise like ceil(double).

Parameters:
d -
Returns:

toInfinity

public static int toInfinity(double d)
Round to the next integer so that the rounded value is equal or farther away from zero than the input value. In other words: for values > zero round like ceil(double) would, otherwise like floor(double).

Parameters:
d -
Returns:

longToBytesBE

public static void longToBytesBE(long l,
                                 byte[] buffer,
                                 int offset)
Store the given long in big-endian format into the given buffer at the given offset.

Parameters:
l -
buffer -
offset -

longToBytesLE

public static void longToBytesLE(long l,
                                 byte[] buffer,
                                 int offset)
Store the given long in little-endian format into the given buffer at the given offset.

Parameters:
l -
buffer -
offset -

f2Dot14ToDouble

public static double f2Dot14ToDouble(short i)
Converts 16-bit (2 bytes) with F2Dot14 notation to double

See Also:
Microsoft OpenType Font File Data Types (applicable for TTF)

doubleTof2Dot14

public static short doubleTof2Dot14(double d)
Converts double to 2 bytes with F2Dot14 notation

See Also:
Microsoft OpenType Font File Data Types (applicable for TTF)


Copyright © 1995-2020 levigo holding gmbh. All Rights Reserved.