Class Math. Rounding functions
Before studying this topic, it is recommended that you familiarize yourself with the following topics:
Contents
- 1. List of rounding functions
- 2. Function abs(). Get the absolute value of the specified argument
- 3. Function ceil(). The smallest integer that is greater than or equal to the given argument
- 4. Function floor(). Largest integer that is less than or equal to the given argument
- 5. Function floorDiv(). Result of integer division of two integers
- 6. Function floorMod(). The smallest integer remainder of the division of two integers
- 7. Function max(). Determining the larger of two values
- 8. Function min(). Determining the smaller of two values
- 9. Function nextAfter(). Get the next value of the argument in the given direction
- 10. Function nextDown(). Get the next value that is less than the argument value
- 11. Function nextUp(). Get the next value from the argument in ascending direction
- 12. Function rint(). Get the nearest integer value to the specified argument
- 13. Function round(). Get argument rounded to nearest integer value
- 14. Function ulp(). Get the number of ones in the last digit for the specified argument
- Related topics
Search on other Web-resources:
1. List of rounding functions
In addition to trigonometric and exponential functions, the Math class contains functions for performing various rounding operations. Below is a list of these functions
- abs() – returns the absolute value of the argument;
- ceil() – the smallest integer greater than or equal to the specified argument;
- floor() – the largest integer that is less than or equal to the specified argument;
- floorDiv() – returns the result of integer division of two integers;
- floorMod() – get the smallest integer remainder after dividing two integers;
- max() – returns the greater of two values;
- min() – returns the smaller of two values;
- nextAfter() – get the next value of the argument in the given direction;
- nextDown() – get the next value that is less than the argument value;
- nextUp() – get the next value from the argument in the positive direction;
- rint() – get the closest integer value to the specified argument;
- round() – get the argument rounded to the nearest integer value;
- ulp() – get the number of units in the last digit for the specified argument.
⇑
2. Function abs(). Get the absolute value of the specified argument
Using the abs() function, you can get the absolute value of an argument. The general form of a function declaration for basic numeric types is:
static int abs(int value); static long abs(long value); static float abs(float value); static double abs(double value);
where
- value – the value of one of the primitive numeric types.
Example.
public class MathFunctions { public static void main(String[] args) { // The abs() function – the modulus of a number int a = -8; double x = -2.432; float f = -1.43f; int aa = Math.abs(a); System.out.println("aa = " + aa); double xx; xx = Math.abs(x); System.out.println("xx = " + xx); float ff = Math.abs(f); System.out.println("ff = " + ff); } }
Result
aa = 8 xx = 2.432 ff = 1.43
⇑
3. Function ceil(). The smallest integer that is greater than or equal to the given argument
The general form of a function declaration is:
static double ceil(double value);
where
- value – the argument from which to extract the integer value.
Example.
public class MathFunctions { public static void main(String[] args) { // The ceil() function is the closest double that is greater than the argument double x = -7.65; double xx; xx = Math.ceil(x); // xx = -7.0 System.out.println("xx = " + xx); float f = 11.43f; // ff = 12.0 float ff = (float)Math.ceil((double)f); System.out.println("ff = " + ff); } }
Result
xx = -7.0 ff = 12.0
⇑
4. Function floor(). Largest integer that is less than or equal to the given argument
The general form of a function declaration is:
static double floor(double value);
where
- value – argument from which the nearest double integer is obtained.
Example.
public class MathFunctions { public static void main(String[] args) { // Function floor() // 1. For type float float f1 = 2.83f; float resF = (float)Math.floor((double)f1); System.out.println("floor(2.83) = " + resF); // 2. For double type double d1 = -7.75; double resD = (double)Math.floor(d1); System.out.println("floor(-7.75) = " + resD); } }
Result
floor(2.83) = 2.0 floor(-7.75) = -8.0
⇑
5. Function floorDiv(). Result of integer division of two integers
Общая форма объявления функции для типов int и long:
static int floorDiv(int divisible, int divider); static int floorDiv(long divisible, long divider);
where
- divisible – divisible value;
- divider – value by which the number is divided.
Example.
public class MathFunctions { public static void main(String[] args) { // Function floorDiv() // 1. For int type int resInt = Math.floorDiv(117, 20); System.out.println("117/20 = " + resInt); // 2. For long type long resLong = Math.floorDiv(2300L, 83L); System.out.println("2300/83 = " + resLong); } }
Result
117/20 = 5 2300/83 = 27
⇑
6. Function floorMod(). The smallest integer remainder of the division of two integers
The floorMod() function returns the smallest remainder after dividing two integers. The general form of a function declaration is:
static int floorMod(int divisible, int divider); static int floorMod(long divisible, long divider);
where
- divisible – divisible value;
- divider – value by which the number is divided.
Example.
public class MathFunctions { public static void main(String[] args) { // Function floorMod() // 1. For int type int i1 = 23; int i2 = 11; int resI = Math.floorMod(i1, i2); System.out.println("230/11 = " + resI); // 2. For long type long l1 = 100L; long l2 = 9L; System.out.println("100L / 9 = " + Math.floorMod(l1, l2)); } }
Result
230/11 = 1 100L / 9 = 1
⇑
7. Function max(). Determining the larger of two values
The max() function returns the maximum of two given values. The general form of a function declaration for primitive numeric types is:
static int max(int x, int y); static long max(long x, long y); static float max(float x, float y); static double max(double x, double y);
where
- x, y – values from which the larger one is determined.
Example.
public class MathFunctions { public static void main(String[] args) { // Function max() - maximum of two numbers // 1. For int type int i1 = 35; int i2 = 110; int maxInt = Math.max(i1, i2); System.out.println("max(35, 110) = " + maxInt); // 2. For double type System.out.println("max(2.88, 3.11) = " + Math.max(2.88, 3.11)); } }
Result
max(35, 110) = 110 max(2.88, 3.11) = 3.11
⇑
8. Function min(). Determining the smaller of two values
The min() function returns the minimum of the two given values. According to the documentation, function declarations for primitive numeric types look like:
static int min(int x, int y); static long min(long x, long y); static float min(float x, float y); static double min(double x, double y);
where
- x, y – values from which the smallest is determined.
Example.
public class MathFunctions { public static void main(String[] args) { // Function min() - minimum of two numbers // 1. For type long long i1 = 35; long i2 = 110; long minInt = Math.min(i1, i2); System.out.println("min(35, 110) = " + minInt); // 2. For type float System.out.println("min(2.88, 3.11) = " + Math.min(2.88, 3.11)); } }
Result
min(35, 110) = 35 min(2.88, 3.11) = 2.88
⇑
9. Function nextAfter(). Get the next value of the argument in the given direction
Using the nextAfter() function, you can get the next value of the argument in the given direction for floating point numbers. According to the documentation, the function declaration is as follows:
static double nextAfter(double start, double direction); static float nextAfter(float start, double direction);
where
- start – initial value from which the direction is set;
- direction – a number that defines the direction of change of the initial value.
Example.
public class MathFunctions { public static void main(String[] args) { // nextAfter() function - get the value of the argument in the given direction // 1. For double type double start1 = 35.2; double direction1 = 1.5; double resD = Math.nextAfter(start1, direction1); System.out.println("nextAfter(35.2, 1.5) = " + resD); // 2. For float type float start2 = -18.3f; float direction2 = -2.4f; float resF = Math.nextAfter(start2, direction2); System.out.println("nextAfter(-18.3, -2.4) = " + resF); } }
Result
nextAfter(35.2, 1.5) = 35.199999999999996 nextAfter(-18.3, -2.4) = -18.299997
⇑
10. Function nextDown(). Get the next value that is less than the argument value
Using the nextDown() function, you can get the nearest next value that is less than the value of the argument. According to the documentation, the function declaration is as follows:
static float nextDown(float val); static double nextDown(double val);
where
- val – value relative to which the previous value is obtained.
Example.
public class MathFunctions { public static void main(String[] args) { // The nextDown() function - get the next value of the argument in descending direction // 1. For double type double value = 35.2; double resD = Math.nextDown(value); System.out.println("nextDown(35.2) = " + resD); // 2. For float type float value2 = 8.3f; System.out.println("nextDown(8.3f) = " + Math.nextDown(value2)); } }
Result
nextDown(35.2) = 35.199999999999996 nextDown(8.3f) = 8.299999
⇑
11. Function nextUp(). Get the next value from the argument in ascending direction
According to the function declaration documentation the following:
static float nextUp(float val); static double nextUp(double val);
where
- val – value relative to which the next value is obtained.
Example.
public class MathFunctions { public static void main(String[] args) { // The nextUp() function - get the next value of the argument in the direction of growth // 1. For double type double value = 35.2; double resD = Math.nextUp(value); System.out.println("nextUp(35.2) = " + resD); // 2. For float type float value2 = 8.3f; System.out.println("nextUp(8.3f) = " + Math.nextUp(value2)); } }
Result
nextUp(35.2) = 35.20000000000001 nextUp(8.3f) = 8.300001
⇑
12. Function rint(). Get the nearest integer value to the specified argument
According to the documentation, the function declaration is as follows:
static double rint(double arg);
where
- arg – source argument.
Example.
public class MathFunctions { public static void main(String[] args) { // The rint() function - get the nearest integer value to the specified argument // 1. Positive number double arg = 5.2; double res = Math.rint(arg); System.out.println("rint(5.2) = " + res); // 2. Negative number arg = -7.65; res = Math.rint(arg); System.out.println("rint(-7.65) = " + res); } }
Result
rint(5.2) = 5.0 rint(-7.65) = -8.0
⇑
13. Function round(). Get argument rounded to nearest integer value
According to the documentation, the function declaration is as follows:
static long round(double arg);
where
- arg – source argument.
Example.
public class MathFunctions { public static void main(String[] args) { // The round() function - get argument rounded to nearest integer value // 1. Positive number double arg = 5.6; double res = Math.round(arg); System.out.println("round(5.6) = " + res); res = Math.round(5.2); System.out.println("round(5.2) = " + res); // 2. Negative number arg = -7.65; res = Math.round(arg); System.out.println("round(-7.65) = " + res); res = Math.round(-7.15); System.out.println("round(-7.15) = " + res); } }
Result
round(5.6) = 6.0 round(5.2) = 5.0 round(-7.65) = -8.0 round(-7.15) = -7.0
⇑
14. Function ulp(). Get the number of ones in the last digit for the specified argument
According to the documentation, the function declaration is as follows:
static float ulp(float arg); static double ulp(double arg);
where
- arg – source argument.
Example.
public class MathFunctions { public static void main(String[] args) { // Function ulp() - get the number of ones in the last digit for the specified argument // 1. For double type double argD = 5.618; double resD = Math.ulp(argD); System.out.println("ulp(5.618) = " + resD); System.out.println("ulp(-2.3) = " + Math.ulp(-2.3)); // 2. For float type float argF = 5.618f; float resF = Math.ulp(argF); System.out.println("ulp(5.618f) = " + resF); System.out.println("ulp(-2.3f) = " + Math.ulp(-2.3f)); } }
Result
ulp(5.618) = 8.881784197001252E-16 ulp(-2.3) = 4.440892098500626E-16 ulp(5.618f) = 4.7683716E-7 ulp(-2.3f) = 2.3841858E-7
⇑
Related topics
⇑