Rounding and Remainder Methods


Rounding and Remainder Methods

The Math and StrictMath classes contain a number of methods for rounding a value according to various schemes. Some will simply round a double value to the next higher or lower integer value. Others perform the rounding according to a slightly more complicated algorithm. There is also a method to return the IEEE remainder of two numbers .


   public  static  double  ceil(double  a)
   
   public  static  double  floor(double  a)
   
   public  static  double  IEEEremainder(double  f1,  double  f2)
   
   public  static  double  rint(double  a)
   
   public  static  long  round(double  a)
   
   public  static  int  round(float  a)

ceil() returns the smallest double value that is not less than the argument and is equal to a mathematical integer. If the input argument is equal to an integer value, then the input value is returned.

floor() returns the largest double value that is not greater than the argument and is equal to a mathematical integer. If the input argument is equal to an integer value, then the input value is returned.

IEEEremainder() returns the value f 1 nf 2 where n is the integer closest to the ratio f 1 / f 2 . If the ratio f 1 / f 2 is equally close to two integers, then n is set to the even integer.

rint() returns the double value equal to a mathematical integer that is closest to the input argument. If the input argument is equally close to two integer values, the even integer value is returned.

round() returns the closest integer to the input argument. There are two versions of this method. The first takes a double argument and returns a long value. The second version takes a float argument and returns an int value. In either case the return value is obtained by adding ½ to the argument and taking the floor of the result.

Example: Using Rounding and Remainder Methods

The RoundDemo class demonstrates the output for each of the rounding methods. The first value tested is 12.5, a floating point number exactly between two integer values. The ceil() and floor() methods return the value 13.0 and 12.0 respectively. The rint() method will return 12.0 because 12.0 and 13.0 are equally close to 12.5 and 12.0 is the even number. The round() method will return 13.0 because when you add 0.5 to 12.5 you get 13.0.

The same group of methods is called using the value 4.67. The ceil() and floor() methods return 5.0 and 4.0. The rint() and round() methods in this case return the same value, 5.0. The output of the IEEEremainder() method using 12.5 and 4.67 as inputs is shown. One thing to remember about the IEEEremainder() method is that the return value can be positive or negative.

 public class RoundDemo {   public static void main(String args[]) {     double value1 = 12.5;       System.out.println("initial value = " + value1);       System.out.println("ceiling = " +                           Math.ceil(value1));       System.out.println("floor = "+Math.floor(value1));       System.out.println("rint = "+Math.rint(value1));       System.out.println("round = "+Math.ceil(value1));       System.out.println();       double value2 = 4.67;       System.out.println("initial value = "+value2);       System.out.println("ceiling = " +                           Math.ceil(value2));       System.out.println("floor = "+Math.floor(value2));       System.out.println("rint = "+Math.rint(value2));       System.out.println("round = "+Math.ceil(value2));       System.out.println("IEEE remainder = " +                     Math.IEEEremainder(value1,value2));   } } 

Output ”

 initial value = 12.5 ceiling = 13.0 floor = 12.0 rint = 12.0 round = 13.0 initial value = 4.67 ceiling = 5.0 floor = 4.0 rint = 5.0 round = 5.0 IEEE remainder = -1.5099999999 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net