KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > MathUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 /**
6  * Various math utilities.
7  */

8 public class MathUtil {
9
10     /**
11      * Generates pseudo-random long from specific range. Generated number is
12      * great or equals to min parameter value and less then max parameter value.
13      * Uses {@link Math#random()}.
14      *
15      * @param min lower (inclusive) boundary
16      * @param max higher (exclusive) boundary
17      *
18      * @return pseudo-random value
19      */

20
21     public static long randomLong(long min, long max) {
22         return min + (long)(Math.random() * (max - min));
23     }
24
25
26     /**
27      * Generates pseudo-random integer from specific range. Generated number is
28      * great or equals to min parameter value and less then max parameter value.
29      * Uses {@link Math#random()}.
30      *
31      * @param min lower (inclusive) boundary
32      * @param max higher (exclusive) boundary
33      *
34      * @return pseudo-random value
35      */

36     public static int randomInt(int min, int max) {
37         return min + (int)(Math.random() * (max - min));
38     }
39
40
41     // ---------------------------------------------------------------- compare
42

43     /**
44      * Compares two doubles for order.
45      *
46      * @param lhs the first <code>double</code>
47      * @param rhs the second <code>double</code>
48      * @return <code>-1</code> if lhs is less, <code>+1</code> if greater,
49      * <code>0</code> if equal to rhs
50      */

51     public static int compare(double lhs, double rhs) {
52         if (lhs < rhs) {
53             return -1;
54         }
55         if (lhs > rhs) {
56             return +1;
57         }
58         long lhsBits = Double.doubleToLongBits(lhs);
59         long rhsBits = Double.doubleToLongBits(rhs);
60         if (lhsBits == rhsBits) {
61             return 0;
62         }
63         if (lhsBits < rhsBits) {
64             return -1;
65         } else {
66             return +1;
67         }
68     }
69
70     /**
71      * Compares two floats for order.
72      *
73      * @param lhs the first <code>float</code>
74      * @param rhs the second <code>float</code>
75      * @return <code>-1</code> if lhs is less, <code>+1</code> if greater,
76      * <code>0</code> if equal to rhs
77      */

78     public static int compare(float lhs, float rhs) {
79         if (lhs < rhs) {
80             return -1;
81         }
82         if (lhs > rhs) {
83             return +1;
84         }
85         int lhsBits = Float.floatToIntBits(lhs);
86         int rhsBits = Float.floatToIntBits(rhs);
87         if (lhsBits == rhsBits) {
88             return 0;
89         }
90         if (lhsBits < rhsBits) {
91             return -1;
92         } else {
93             return +1;
94         }
95     }
96
97 }
98
Popular Tags