java

Reviews
Shared by: mifei
Stats
views:
23
rating:
not rated
reviews:
0
posted:
11/6/2009
language:
ENGLISH
pages:
0
Passing the Java Certified Programmer Exam java.lang.Math and java.lang.Double From the certification objective's page at Sun's site: Section 9 The java.lang package Write code using the following methods of the java.lang.Math class: abs, ceil, floor, max, min, random, round, sin, cos, tan, sqrt. java.lang.Math is a utility class. It contains static methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. It is useful to know how a double (and a float) are formatted: Java uses the IEEE 754 floating-point "double format" The highest bit (63) is the sign bit. A double representing a valid number is negative iff the sign bit is on. Apart from that bit, the top three nybbles (bits 5262) hold the exponent and the other bits 0-51 hold the mantissa (significand). Class java.lang.Double is a good class to understand when doing math. Constants. Double has 5 symbolic double constants: Double.MAX_VALUE Double.MIN_VALUE Double.POSITIVE_INFINITY Double.NEGATIVE_INFINITY Double.NaN the largest positive finite value the smallest positive value not a number – failed computation The most negative finite value is –Double.MAX_VALUE. Double.MAX_VALUE is 1.7976931348623157E308 Double.MIN_VALUE is 4.9E-324 Double.MIN_POSITIVE_INFINITY is Double.MIN_NEGATIVE_INFINITY is Double.NaN is = = = = = 7fe|fffffffffffff 000|0000000000001 7ff|0000000000000 fff|0000000000000 7ff|8000000000000 Related methods: boolean isInfinite() static boolean isInfinite(double x) boolean isNaN() static boolean isNaN(double x) positive or negative infinity 1 How to see the bits in a double. If you need to see the bit pattern like the ones displayed on the previous page, use the following Double functions. static long doubleToLongBits(double value) static long doubleToRawLongBits(double value) static double longBitsToDouble(long bits) The difference between the first two functions is because the IEEE standard allows for several bit patterns to represent "Not-A-Number" and some machines use these to indicate different reasons for failed computation. Java does not use this information. doubleToLongBits()collapses NaN values and always returns 7ff|8000000000000, while doubleToRawLongBits() doesn't collapse NaN values. Comparison. The issue of comparison is more complicated that you might first suppose. First, There are the equality and relational operators of Java: x == y  x != y x < y x <= y x > y x >= y How do they treat NaN? Since it represents an error value it is incomparable to any value: in other words, the follow expressions are false for any double x (including NaN and the positive and negative infinity.) x < NaN NaN < x x <= NaN NaN <= x x > NaN NaN > x x >= NaN NaN >= x x == NaN But NaN != x and x != NaN hold for every x, including NaN.  What about POSITIVE_INFINITY and NEGATIVE_INFINITY? Apart from the special treatment of NaN, positive infinity is greater than any finite number and negative infinity is less than any finite number. What about negative zero? There are two formats for zero: 0.0 or +0.0 is -0.0 is 000|0000000000000 800|0000000000000  They are treated as the same value - zero – but see how the later comparison functions differ. 2 Comparison: equals() and compareTo(). Double has the following methods. boolean equals(Object obj) int hashCode() int compareTo(Double anotherDouble) int compareTo(Object o) // compareTo((Double)o) equals . A double can only equal() another double object. In that case they are equal if method doubleToLongBits () returns the same bit pattern for both. In most cases, for two instances of class Double, d1 and d2, d1.equals(d2) if and only if d1.doubleValue() == d2.doubleValue() However, there are two exceptions:  If d1 and d2 both represent Double.NaN, then the equals method returns true, even though Double.NaN==Double.NaN has the value false.  If d1 represents +0.0 while d2 represents -0.0, or vice versa, the equal test has the value false, even though +0.0==-0.0 has the value true. This allows hash tables to operate properly. hashCode returns a value derived from doubleToLongBits (). This is necessary because equal objects must hash to the same int value. compareTo A Double can only be compared to another Double, otherwise a ClassCastException is thrown. There are two ways in which comparisons performed by this method differ from those performed by the language's comparison operators (<, <=, ==, >= >) when applied to primitive doubles:   Double.NaN is considered by this method to be equal to itself and greater than all other double values (including Double.POSITIVE_INFINITY). 0.0d is considered by this method to be greater than -0.0d. 3 Math functions  Unless noted, any function on floats or doubles returns NaN if any argument is NaN. int min(int x, int y) long min(long x, long y) float min(float x, float y) double min(double x, double y) int max(int x, int y) long max(long x, long y) float max(float x, float y) double max(double x, double y) static static static static static static static static  The above methods consider –0.0 to be less than 0.0, so the value of min(-0.0,0.0) is –0.0. double double double double double double double sin(double x) cos(double x) tan(double x) asin(double x) acos(double x) atan(double x) atan2(double y, double x) static static static static static static static  All mathematics is done in radians not degrees. To convert: //degrees * Math.PI / 180.0; //radians * 180.0 / Math.PI radians = Math.toRadians(degrees); degrees = Math.toDegrees(radians);   atan2() returns the angle made by the point (x,y) in the range –pi to pi. If the parameters for acos() or asin() are out of bounds, NaN is returned. 4 static static static static static static static static static static static   int abs(int x) long abs(long x) float abs(float x) double abs(double x) double floor(double x) double ceil(double x) int round(float x) long round(double x) double rint(double x) double sqrt(double x) double random()       Overflows with abs: abs(Integer.MIN_VALUE) == Integer.MIN_VALUE and abs(Long.MIN_VALUE) == Long.MIN_VALUE Casting a floating point number to an integral value always discards fractional data. This is different than both floor() and ceil() which round down and up, respectively. double's round() is equivalent to (long)Math.floor(a + 0.5d) round() maps NaN to 0  (And casting maps NaN to 0) rint() handles the XX.5 differently: it rounds to the even value. round(), floor(), ceil() all preserve +0.0 and –0.0. sqrt() preserves +0.0 and –0.0 and maps negative numbers to NaN. random() returns a double chosen to be in the range 0.0 <= x < 1.0. Behind the scenes it is using an instance of java.util.Random, a random number generator that has been seeded with the current time in milliseconds. Class Random has a number of convenient methods for producing random data: prefer it over Math.random(). Among other things, accessing Math-random() from different threads causes synchronizations which slow the threads. 5

Related docs
java
Views: 610  |  Downloads: 13
JAVA
Views: 397  |  Downloads: 3
JAVA
Views: 131  |  Downloads: 24
java
Views: 669  |  Downloads: 72
Java
Views: 78  |  Downloads: 13
Java-and-UML
Views: 2  |  Downloads: 1
Java-and-UML
Views: 15  |  Downloads: 5
Pengenalan Bahasa JAVA
Views: 142  |  Downloads: 13
webDynpro for java
Views: 787  |  Downloads: 43
Java Basics
Views: 473  |  Downloads: 63
Java
Views: 113  |  Downloads: 14
Java Interview Questions
Views: 285  |  Downloads: 77
Java
Views: 275  |  Downloads: 54
JAVA generics
Views: 116  |  Downloads: 36
premium docs
Other docs by mifei
词 汇 表
Views: 86  |  Downloads: 0
英语国家概况(下)模拟题1
Views: 37  |  Downloads: 0
[[n_m]_ gcd_n_m__ [F_n_F_m]_ gcd_F_n_F_m_]
Views: 35  |  Downloads: 0
ZURICH-AMERICAN INSURANCE GROUP
Views: 39  |  Downloads: 0
ZIMAKO U
Views: 36  |  Downloads: 0
Young Adult Literature Bibliography
Views: 65  |  Downloads: 0
WRITING
Views: 56  |  Downloads: 0
Why have a social media policy
Views: 43  |  Downloads: 1
WHATS NEW IN NORTH OGDEN
Views: 19  |  Downloads: 0
WEST CENTRAL SOCCER ASSOCIATION
Views: 18  |  Downloads: 0
West Bend Community Memorial Library
Views: 21  |  Downloads: 0
Wellness and Recovery Newsletter Vol 1 Issue 1
Views: 15  |  Downloads: 0