Exponential Functions – Selected API Classes

Exponential Functions

Click here to view code image

static double pow(double d1, double d2)

Returns the value of d1 raised to the power of d2 (i.e., d1d2).

Click here to view code image

static double exp(double d)

Returns the exponential number e raised to the power of d (i.e., ed).

Click here to view code image

static double log(double d)

Returns the natural logarithm (base e) of d (i.e., loged).

Click here to view code image

static double sqrt(double d)

Returns the square root of d (i.e., d0.5). For a NaN or a negative argument, the result is a NaN.

Some examples of exponential functions:

Click here to view code image

double r = Math.pow(2.0, 4.0);           // 16.0
double v = Math.exp(2.0);                // 7.38905609893065
double l = Math.log(Math.E);             // 0.9999999999999981
double c = Math.sqrt(25.0);              // 5.0

Exact Integer Arithmetic Functions

Integer arithmetic operators (like +, -, *) do not report overflow errors—that is, the integer values wrap around (§2.8, p. 59). However, if it is important to detect overflow errors, the Math class provides methods that report overflow errors by throwing an ArithmeticException when performing integer arithmetic operations. The relevant methods have the postfix “Exact” in their name, and are shown below.

The code snippets below illustrate exact integer arithmetic operations provided by the Math class, where we assume the field System.out is statically imported. The results computed at (1a), (2a), and (3a) are incorrect due to overflow, but the standard arithmetic operators do not report overflow errors. (1b), (2b), and (3b) use exact arithmetic operations and throw an ArithmeticException when an overflow is detected. (1c), (2c), and (3c) compute correct results as there is no overflow in performing the operation.

Click here to view code image

out.println(Integer.MAX_VALUE + 1);                 // (1a) -2147483648
out.println(Math.addExact(Integer.MAX_VALUE, 1));   // (1b) ArithmeticException
out.println(Math.addExact(1_000_000, 1_000));       // (1c) 1001000
out.println(Integer.MAX_VALUE * 100);               // (2a) -100
out.println(
     Math.multiplyExact(Integer.MAX_VALUE, 100)     // (2b) ArithmeticException
);
out.println(Math.multiplyExact(1_000_000, 1_000));  // (2c) 1000000000

out.println((int)Long.MAX_VALUE);                   // (3a) -1
out.println(Math.toIntExact(Long.MAX_VALUE));       // (3b) ArithmeticException
out.println(Math.toIntExact(1_000_000));            // (3c) 1000000

Selected exact integer arithmetic methods in the Math class are shown below.

Click here to view code image

static int absExact(int a)
static long absExact(long a)

Return the mathematical absolute value of an int or a long value if it is exactly representable as an int or a long, throwing ArithmeticException if the argument is Integer.MIN_VALUE or Long.MIN_VALUE, as these argument values would overflow the positive int or long range, respectively.

Click here to view code image

static int  addExact(int x, int y)
static long addExact(long x, long y)
static int  subtractExact(int x, int y)
static long subtractExact(long x, long y)

Return the sum or difference of the given arguments, throwing Arithmetic-Exception if the result overflows an int or a long, respectively.

Click here to view code image

static int  multiplyExact(int x, int y)
static long multiplyExact(long x, int y)
static long multiplyExact(long x, long y)

Return the product of the arguments, throwing ArithmeticException if the result overflows an int in the first method or a long in the last two methods.

Click here to view code image

static int  incrementExact(int a)
static long incrementExact(long a)
static int  decrementExact(int a)
static long decrementExact(long a)

Return the argument incremented or decremented by 1, throwing Arithmetic-Exception if the result overflows an int or a long, respectively.

Click here to view code image

static int  negateExact(int a)
static long negateExact(long a)

Return the negation of the argument, throwing ArithmeticException if the result overflows an int or a long, respectively.

Click here to view code image

static int toIntExact(long value)

Returns the value of the long argument as an int, throwing ArithmeticException if the value overflows an int.

Leave a Reply

Your email address will not be published. Required fields are marked *