uk.ac.starlink.ttools.func
Class Arithmetic

java.lang.Object
  extended by uk.ac.starlink.ttools.func.Arithmetic

public class Arithmetic
extends Object

Standard arithmetic functions including things like rounding, sign manipulation, and maximum/minimum functions.

Since:
2 Sep 2004
Author:
Mark Taylor (Starlink)

Method Summary
static double abs(double x)
          Returns the absolute value of a floating point value.
static int abs(int x)
          Returns the absolute value of an integer value.
static int max(int a, int b)
          Returns the greater of two integer values.
static double maxNaN(double a, double b)
          Returns the greater of two floating point values.
static double maxReal(double a, double b)
          Returns the greater of two floating point values, ignoring blanks.
static int min(int a, int b)
          Returns the smaller of two integer values.
static double minNaN(double a, double b)
          Returns the smaller of two floating point values.
static double minReal(double a, double b)
          Returns the smaller of two floating point values, ignoring blanks.
static int round(double x)
          Rounds a value to the nearest integer.
static float roundDecimal(double x, int dp)
          Rounds a value to a given number of decimal places.
static int roundDown(double x)
          Rounds a value down to an integer value.
static int roundUp(double x)
          Rounds a value up to an integer value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

roundUp

public static int roundUp(double x)
Rounds a value up to an integer value. Formally, returns the smallest (closest to negative infinity) integer value that is not less than the argument.

Parameters:
x - a value.
Returns:
x rounded up

roundDown

public static int roundDown(double x)
Rounds a value down to an integer value. Formally, returns the largest (closest to positive infinity) integer value that is not greater than the argument.

Parameters:
x - a value
Returns:
x rounded down

round

public static int round(double x)
Rounds a value to the nearest integer. Formally, returns the integer that is closest in value to the argument. If two integers are equally close, the result is the even one.

Parameters:
x - a floating point value.
Returns:
x rounded to the nearest integer

roundDecimal

public static float roundDecimal(double x,
                                 int dp)
Rounds a value to a given number of decimal places. The result is a float (32-bit floating point value), so this is only suitable for relatively low-precision values. It's intended for truncating the number of apparent significant figures represented by a value which you know has been obtained by combining other values of limited precision. For more control, see the functions in the Formats class.

Parameters:
x - a floating point value
dp - number of decimal places (digits after the decimal point) to retain
Returns:
floating point value close to x but with a limited apparent precision
Examples:
roundDecimal(PI,2) = 3.14f

abs

public static int abs(int x)
Returns the absolute value of an integer value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

Parameters:
x - the argument whose absolute value is to be determined
Returns:
the absolute value of the argument.

abs

public static double abs(double x)
Returns the absolute value of a floating point value. If the argument is not negative, the argument is returned. If the argument is negative, the negation of the argument is returned.

Parameters:
x - the argument whose absolute value is to be determined
Returns:
the absolute value of the argument.

max

public static int max(int a,
                      int b)
Returns the greater of two integer values. If the arguments have the same value, the result is that same value.

Multiple-argument maximum functions are also provided in the Arrays and Lists packages.

Parameters:
a - an argument.
b - another argument.
Returns:
the larger of a and b.

maxNaN

public static double maxNaN(double a,
                            double b)
Returns the greater of two floating point values. If the arguments have the same value, the result is that same value. If either value is blank, then the result is blank.

Parameters:
a - an argument.
b - another argument.
Returns:
the larger of a and b.

maxReal

public static double maxReal(double a,
                             double b)
Returns the greater of two floating point values, ignoring blanks. If the arguments have the same value, the result is that same value. If one argument is blank, the result is the other one. If both arguments are blank, the result is blank.

Multiple-argument maximum functions are also provided in the Arrays and Lists packages.

Parameters:
a - an argument
b - another argument
Returns:
the larger non-blank value of a and b

min

public static int min(int a,
                      int b)
Returns the smaller of two integer values. If the arguments have the same value, the result is that same value.

Multiple-argument minimum functions are also provided in the Arrays and Lists packages.

Parameters:
a - an argument.
b - another argument.
Returns:
the smaller of a and b.

minNaN

public static double minNaN(double a,
                            double b)
Returns the smaller of two floating point values. If the arguments have the same value, the result is that same value. If either value is blank, then the result is blank.

Parameters:
a - an argument.
b - another argument.
Returns:
the smaller of a and b.

minReal

public static double minReal(double a,
                             double b)
Returns the smaller of two floating point values, ignoring blanks. If the arguments have the same value, the result is that same value. If one argument is blank, the result is the other one. If both arguments are blank, the result is blank.

Multiple-argument minimum functions are also provided in the Arrays and Lists packages.

Parameters:
a - an argument
b - another argument
Returns:
the larger non-blank value of a and b


Copyright © 2017 Central Laboratory of the Research Councils. All Rights Reserved.