# Class Arithmetic

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

public class Arithmetic extends Object
Standard arithmetic functions including things like rounding, sign manipulation, and maximum/minimum functions. Phase folding operations, and a convenient form of the modulus operation on which they are based, are also provided.

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

## Method Summary

Modifier and Type
Method
Description
`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 double`
`mod(double a, double b)`
Returns the non-negative remainder of `a/b`.
`static double`
`phase(double t, double period)`
Returns the phase of a value within a period.
`static double`
`phase(double t, double period, double t0)`
Returns the phase of an offset value within a period.
`static double`
`phase(double t, double period, double t0, double phase0)`
Returns the offset phase of an offset value within a period.
`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 Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### 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`

### mod

public static double mod(double a, double b)
Returns the non-negative remainder of `a/b`. This is a modulo operation, but differs from the expression `a%b` in that the answer is always >=0 (as long as `b` is not zero).

Parameters:
  `a` - dividend
  `b` - divisor
Returns:
  non-negative remainder when dividing `a` by `b`
Examples:
  `modulo(14, 5) = 4`, `modulo(-14, 5) = 1`, `modulo(2.75, 0.5) = 0.25`

### phase

public static double phase(double t, double period)
Returns the phase of a value within a period.

For positive period, the returned value is in the range [0,1).

Parameters:
  `t` - value
  `period` - folding period
Returns:
  mod(t,period)/period
Examples:
  `phase(7, 4) = 0.75`, `phase(-1000.5, 2.5) = 0.8`, `phase(-3300, 33) = 0`

### phase

public static double phase(double t, double period, double t0)
Returns the phase of an offset value within a period. The reference value `t0` corresponds to phase zero.

For positive period, the returned value is in the range [0,1).

Parameters:
  `t` - value
  `period` - folding period
  `t0` - reference value, corresponding to phase zero
Returns:
  phase(t-t0, period)
Examples:
  `phase(5003,100,0) = 0.03`, `phase(5003,100,2) = 0.01`, `phase(5003,100,4) = 0.99`

### phase

public static double phase(double t, double period, double t0, double phase0)
Returns the offset phase of an offset value within a period. The reference value `t0` corresponds to integer phase value, and the phase offset `phase0` determines the starting value for the phase range.

For positive period, the returned value is in the range [`phase0`,`phase0+1`).

Parameters:
  `t` - value
  `period` - folding period
  `t0` - reference value, corresponding to phase zero
  `phase0` - offset for phase
Returns:
  offset phase
Examples:
  `phase(23,10,1,99) = 99.2`, `phase(8.6125,0.2,0.0125,-0.3) = 0`, `phase(8.6125,0.2,0.1125,-0.7) = -0.5`