Package ucar.nc2.util
Class Misc
- java.lang.Object
-
- ucar.nc2.util.Misc
-
public class Misc extends Object
Miscellaneous static routines.
-
-
Field Summary
Fields Modifier and Type Field Description static double
defaultDiffFLoat
The default maximum relative difference for floats, when comparing as doubles.static double
defaultMaxRelativeDiffDouble
The default maximumrelative difference
that two doubles can have in order to be deemednearly equal
.static float
defaultMaxRelativeDiffFloat
The default maximumrelative difference
that two floats can have in order to be deemednearly equal
.static int
objectSize
Estimates object size in bytesstatic int
referenceSize
Estimates pointer size in bytes
-
Constructor Summary
Constructors Constructor Description Misc()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
absoluteDifference(double a, double b)
The absolute difference between two doubles, i.e.static float
absoluteDifference(float a, float b)
The absolute difference between two floats, i.e.static boolean
compare(byte[] raw1, byte[] raw2, Formatter f)
For testing, use Truth.assertThat(raw1).isEqualTo(raw2)static boolean
compare(float[] raw1, float[] raw2, Formatter f)
For testing, use Truth.assertThat(raw1).isEqualTo(raw2)static boolean
compare(int[] raw1, int[] raw2, Formatter f)
For testing, use Truth.assertThat(raw1).isEqualTo(raw2)static boolean
nearlyEquals(double a, double b)
RelativeDifference is less thandefaultMaxRelativeDiffDouble
.static boolean
nearlyEquals(double a, double b, double maxRelDiff)
RelativeDifference is less than maxRelDiff.static boolean
nearlyEquals(float a, float b)
RelativeDifference is less thandefaultMaxRelativeDiffFloat
.static boolean
nearlyEquals(float a, float b, float maxRelDiff)
RelativeDifference is less than maxRelDiff.static boolean
nearlyEqualsAbs(double a, double b, double maxAbsDiff)
AbsoluteDifference is less than maxAbsDiff.static boolean
nearlyEqualsAbs(float a, float b, float maxAbsDiff)
AbsoluteDifference is less than maxAbsDiff.static double
relativeDifference(double a, double b)
Same asrelativeDifference(float, float)
, but for doubles.static float
relativeDifference(float a, float b)
Returns the relative difference between two numbers, i.e.static String
showBits(byte[] bytes)
static void
showClassPath()
-
-
-
Field Detail
-
referenceSize
public static final int referenceSize
Estimates pointer size in bytes- See Also:
- Constant Field Values
-
objectSize
public static final int objectSize
Estimates object size in bytes- See Also:
- Constant Field Values
-
defaultMaxRelativeDiffFloat
public static final float defaultMaxRelativeDiffFloat
The default maximumrelative difference
that two floats can have in order to be deemednearly equal
.- See Also:
- Constant Field Values
-
defaultDiffFLoat
public static final double defaultDiffFLoat
The default maximum relative difference for floats, when comparing as doubles.- See Also:
- Constant Field Values
-
defaultMaxRelativeDiffDouble
public static final double defaultMaxRelativeDiffDouble
The default maximumrelative difference
that two doubles can have in order to be deemednearly equal
.- See Also:
- Constant Field Values
-
-
Method Detail
-
absoluteDifference
public static float absoluteDifference(float a, float b)
The absolute difference between two floats, i.e.|a - b|
.
-
absoluteDifference
public static double absoluteDifference(double a, double b)
The absolute difference between two doubles, i.e.|a - b|
.
-
relativeDifference
public static float relativeDifference(float a, float b)
Returns the relative difference between two numbers, i.e.|a - b| / max(|a|, |b|)
.For cases where
a == 0
,b == 0
, ora
andb
are extremely close, traditional relative difference calculation breaks down. So, in those instances, we compute the difference relative toFloat.MIN_NORMAL
, i.e.|a - b| / Float.MIN_NORMAL
.- Parameters:
a
- first number.b
- second number.- Returns:
- the relative difference.
- See Also:
- The Floating-Point Guide, Comparing Floating Point Numbers, 2012 Edition
-
relativeDifference
public static double relativeDifference(double a, double b)
Same asrelativeDifference(float, float)
, but for doubles.
-
nearlyEquals
public static boolean nearlyEquals(float a, float b)
RelativeDifference is less thandefaultMaxRelativeDiffFloat
.
-
nearlyEquals
public static boolean nearlyEquals(float a, float b, float maxRelDiff)
RelativeDifference is less than maxRelDiff.
-
nearlyEquals
public static boolean nearlyEquals(double a, double b)
RelativeDifference is less thandefaultMaxRelativeDiffDouble
.
-
nearlyEquals
public static boolean nearlyEquals(double a, double b, double maxRelDiff)
RelativeDifference is less than maxRelDiff.
-
nearlyEqualsAbs
public static boolean nearlyEqualsAbs(float a, float b, float maxAbsDiff)
AbsoluteDifference is less than maxAbsDiff.
-
nearlyEqualsAbs
public static boolean nearlyEqualsAbs(double a, double b, double maxAbsDiff)
AbsoluteDifference is less than maxAbsDiff.
-
showBits
public static String showBits(byte[] bytes)
-
compare
public static boolean compare(byte[] raw1, byte[] raw2, Formatter f)
For testing, use Truth.assertThat(raw1).isEqualTo(raw2)
-
compare
public static boolean compare(float[] raw1, float[] raw2, Formatter f)
For testing, use Truth.assertThat(raw1).isEqualTo(raw2)
-
compare
public static boolean compare(int[] raw1, int[] raw2, Formatter f)
For testing, use Truth.assertThat(raw1).isEqualTo(raw2)
-
showClassPath
public static void showClassPath()
-
-