TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Math.round Method

Use the Math.round method to round numbers up and down. Benchmark rounding performance.
Math.round. Our numbers are 10.1 and 10.5. We want to round them to 10 and 11. With Math.round, part of java.lang.Math, we can do this with a single method call.Math
For performance, Math.round seems fairly efficient. We benchmark it against a common cache collection (HashMap) just to ensure it is fast.
First example, double array. This program introduces an array of 4 doubles. These all must be rounded up or down. We use a for-each loop to call Math.round on each double.

Result: The Math.round method changes 10.1 to 10, and 10.5 to 11. So it rounds down and up depending on the number's fractional value.

Java program that uses Math.round on doubles import java.lang.Math; public class Program { public static void main(String[] args) { double[] values = { 10.1, 10.5, 10.9, 10 }; // Round some double values. for (double v : values) { // This Math.round receives a double and returns along. long result = Math.round(v); System.out.println(v + ": " + result); } } } Output 10.1: 10 10.5: 11 10.9: 11 10.0: 10
Floats. With a float, we have a 4-byte floating-point value (half the size of a double). The Math.round method receives floats and returns ints.
Java program that uses Math.round on floats import java.lang.Math; public class Program { public static void main(String[] args) { float value1 = (float) 10.1; float value2 = (float) 10.5; // Use Math.round on floats. int result1 = Math.round(value1); int result2 = Math.round(value2); // Display results. System.out.println(value1 + ": " + result1); System.out.println(value2 + ": " + result2); } } Output 10.1: 10 10.5: 11
Benchmark, HashMap. Here is an experiment. I wanted to see if a HashMap can retrieve a value faster than a call to Math.round. I found that Math.round is faster.HashMap

Version 1: This version of the code repeatedly calls Math.round to get a round number from a double.

Version 2: Here we store the result of Math.round in a HashMap, and then repeatedly call get() to access the cached result.

Result: It appears that Math.round is faster than a HashMap lookup. This defeats the optimization.

Tip: Lookup tables often improve program performance, but only if the lookup time is less than the original computation time.

Java program that benchmarks Math.round, HashMap import java.util.HashMap; public class Program { public static void main(String[] args) { // Place a Double key in this HashMap. HashMap<Double, Long> roundCache = new HashMap<>(); double value = 1000.5; roundCache.put(value, Math.round(value)); long t1 = System.currentTimeMillis(); // Version 1: use round. for (int i = 0; i < 1000000; i++) { for (int y = 0; y < 200; y++) { // Use Math.round method directly. long result = Math.round(value); if (result != 1001) { return; } } } long t2 = System.currentTimeMillis(); // Version 2: use get() to look up a value. for (int i = 0; i < 1000000; i++) { for (int y = 0; y < 200; y++) { // Use get. long result = roundCache.get(value); if (result != 1001) { return; } } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(t2 - t1); System.out.println(t3 - t2); } } Output 7 ms: Math.round calls 358 ms: HashMap get calls
A summary. Math.round is a fast way to round up or down based on the fractional value of a number (a double or float). For double arguments, it returns long. And for floats, it returns int.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf