Source Code Cross Referenced for Math.java in  » Apache-Harmony-Java-SE » java-package » java » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package java.lang;
019:
020:        /**
021:         * Class math provides various floating point support routines and some standard
022:         * constants.
023:         */
024:        public final class Math {
025:
026:            /**
027:             * Standard math constants.
028:             */
029:            public static final double E = 2.718281828459045;
030:
031:            public static final double PI = 3.141592653589793;
032:
033:            private static java.util.Random random;
034:
035:            /**
036:             * Prevents this class from being instantiated.
037:             */
038:            private Math() {
039:            }
040:
041:            /**
042:             * Answers the absolute value of the argument.
043:             * 
044:             * @param d
045:             *            the value to be converted
046:             * @return the argument if it is positive, otherwise the negation of the
047:             *         argument.
048:             */
049:            public static double abs(double d) {
050:                long bits = Double.doubleToLongBits(d);
051:                bits &= 0x7fffffffffffffffL;
052:                return Double.longBitsToDouble(bits);
053:            }
054:
055:            /**
056:             * Answers the absolute value of the argument.
057:             * 
058:             * @param f
059:             *            the value to be converted
060:             * @return the argument if it is positive, otherwise the negation of the
061:             *         argument.
062:             */
063:            public static float abs(float f) {
064:                int bits = Float.floatToIntBits(f);
065:                bits &= 0x7fffffff;
066:                return Float.intBitsToFloat(bits);
067:            }
068:
069:            /**
070:             * Answers the absolute value of the argument.
071:             * 
072:             * @param i
073:             *            the value to be converted
074:             * @return the argument if it is positive, otherwise the negation of the
075:             *         argument.
076:             */
077:            public static int abs(int i) {
078:                return i >= 0 ? i : -i;
079:            }
080:
081:            /**
082:             * Answers the absolute value of the argument.
083:             * 
084:             * @param l
085:             *            the value to be converted
086:             * @return the argument if it is positive, otherwise the negation of the
087:             *         argument.
088:             */
089:            public static long abs(long l) {
090:                return l >= 0 ? l : -l;
091:            }
092:
093:            /**
094:             * Answers the closest double approximation of the arc cosine of the
095:             * argument
096:             * 
097:             * @param d
098:             *            the value to compute acos of
099:             * @return the arc cosine of the argument.
100:             */
101:            public static native double acos(double d);
102:
103:            /**
104:             * Answers the closest double approximation of the arc sine of the argument
105:             * 
106:             * @param d
107:             *            the value to compute asin of
108:             * @return the arc sine of the argument.
109:             */
110:            public static native double asin(double d);
111:
112:            /**
113:             * Answers the closest double approximation of the arc tangent of the
114:             * argument
115:             * 
116:             * @param d
117:             *            the value to compute atan of
118:             * @return the arc tangent of the argument.
119:             */
120:            public static native double atan(double d);
121:
122:            /**
123:             * Answers the closest double approximation of the arc tangent of the result
124:             * of dividing the first argument by the second argument.
125:             * 
126:             * @param d1
127:             *            the numerator of the value to compute atan of
128:             * @param d2
129:             *            the denominator of the value to compute atan of
130:             * @return the arc tangent of d1/d2.
131:             */
132:            public static native double atan2(double d1, double d2);
133:
134:            /**
135:             * Answers the closest double approximation of the cube root of the
136:             * argument. The final result should be within 1ulp of the real result.
137:             * 
138:             * @param d
139:             *            the value to compute cube root of
140:             * @return the cube root of the argument.
141:             */
142:            public static native double cbrt(double d);
143:
144:            /**
145:             * Answers the double conversion of the most negative (i.e. closest to
146:             * negative infinity) integer value which is greater than the argument.
147:             * 
148:             * @param d
149:             *            the value to be converted
150:             * @return the ceiling of the argument.
151:             */
152:            public static double ceil(double d) {
153:                return -floor(-d);
154:            }
155:
156:            /**
157:             * Answers the closest double approximation of the cosine of the argument
158:             * 
159:             * @param d
160:             *            the value to compute cos of
161:             * @return the cosine of the argument.
162:             */
163:            public static native double cos(double d);
164:
165:            /**
166:             * Answers the closest double approximation of the hyperbolic cosine of the
167:             * argument. The final result should be within 2.5ulps of the real result.
168:             * 
169:             * @param d
170:             *            the value to compute hyperbolic cosine of
171:             * @return the hyperbolic cosine of the argument.
172:             */
173:            public static native double cosh(double d);
174:
175:            /**
176:             * Answers the closest double approximation of the raising "e" to the power
177:             * of the argument
178:             * 
179:             * @param d
180:             *            the value to compute the exponential of
181:             * @return the exponential of the argument.
182:             */
183:            public static native double exp(double d);
184:
185:            /**
186:             * Answers the closest double approximation of <i>e</i><sup>d</sup> - 1.
187:             * If the argument is very close to 0, it is much more accurate to use
188:             * expm1(d)+1 than exp(d).
189:             * 
190:             * The final result should be within 1 ulp of the real result. For any
191:             * finite input, the result should be no less than -1.0. If the real result
192:             * is within 0.5 ulp of -1, -1.0 should be answered.
193:             * 
194:             * @param d
195:             *            the value to compute the <i>e</i><sup>d</sup> - 1 of
196:             * @return the <i>e</i><sup>d</sup> - 1 value of the argument.
197:             */
198:            public static native double expm1(double d);
199:
200:            /**
201:             * Answers the double conversion of the most positive (i.e. closest to
202:             * positive infinity) integer value which is less than the argument.
203:             * 
204:             * @param d
205:             *            the value to be converted
206:             * @return the ceiling of the argument.
207:             */
208:            public static double floor(double d) {
209:                if (Double.isNaN(d) || Double.isInfinite(d) || d == 0) {
210:                    return d;
211:                }
212:                double res = (double) (long) d;
213:                return d > 0 || res == d ? res : res - 1;
214:            }
215:
216:            /**
217:             * Answers sqrt(<i>x</i><sup>2</sup>+<i>y</i><sup>2</sup>). The
218:             * final result is without medium underflow or overflow.
219:             * 
220:             * The final result should be within 1 ulp of the real result. If one
221:             * parameter remains constant, the result should be semi-monotonic.
222:             * 
223:             * @param x
224:             *            a double number
225:             * @param y
226:             *            a double number
227:             * @return the sqrt(<i>x</i><sup>2</sup>+<i>y</i><sup>2</sup>) value
228:             *         of the arguments.
229:             */
230:            public static native double hypot(double x, double y);
231:
232:            /**
233:             * Answers the remainder of dividing the first argument by the second using
234:             * the IEEE 754 rules.
235:             * 
236:             * @param d1
237:             *            the numerator of the operation
238:             * @param d2
239:             *            the denominator of the operation
240:             * @return the result of d1/d2.
241:             */
242:            public static native double IEEEremainder(double d1, double d2);
243:
244:            /**
245:             * Answers the closest double approximation of the natural logarithm of the
246:             * argument
247:             * 
248:             * @param d
249:             *            the value to compute the log of
250:             * @return the natural logarithm of the argument.
251:             */
252:            public static native double log(double d);
253:
254:            /**
255:             * Answers the closest double approximation of the base 10 logarithm of the
256:             * argument
257:             * 
258:             * @param d
259:             *            the value to compute the log10 of
260:             * @return the natural logarithm of the argument.
261:             */
262:            public static native double log10(double d);
263:
264:            /**
265:             * Answers the closest double approximation of the natural logarithm of the
266:             * sum of the argument and 1. If the argument is very close to 0, it is much
267:             * more accurate to use log1p(d) than log(1.0+d).
268:             * 
269:             * The final result should be within 1 ulp of the real result and be
270:             * semi-monotonic.
271:             * 
272:             * @param d
273:             *            the value to compute the ln(1+d) of
274:             * @return the natural logarithm of the sum of the argument and 1.
275:             */
276:            public static native double log1p(double d);
277:
278:            /**
279:             * Answers the most positive (i.e. closest to positive infinity) of the two
280:             * arguments.
281:             * 
282:             * @param d1
283:             *            the first argument to check
284:             * @param d2
285:             *            the second argument
286:             * @return the larger of d1 and d2.
287:             */
288:            public static double max(double d1, double d2) {
289:                if (d1 > d2)
290:                    return d1;
291:                if (d1 < d2)
292:                    return d2;
293:                /* if either arg is NaN, return NaN */
294:                if (d1 != d2)
295:                    return Double.NaN;
296:                /* max( +0.0,-0.0) == +0.0 */
297:                if (d1 == 0.0 && (d1 != -0.0d || d2 != -0.0d))
298:                    return 0.0;
299:                return d1;
300:            }
301:
302:            /**
303:             * Answers the most positive (i.e. closest to positive infinity) of the two
304:             * arguments.
305:             * 
306:             * @param f1
307:             *            the first argument to check
308:             * @param f2
309:             *            the second argument
310:             * @return the larger of f1 and f2.
311:             */
312:            public static float max(float f1, float f2) {
313:                if (f1 > f2)
314:                    return f1;
315:                if (f1 < f2)
316:                    return f2;
317:                /* if either arg is NaN, return NaN */
318:                if (f1 != f2)
319:                    return Float.NaN;
320:                /* max( +0.0,-0.0) == +0.0 */
321:                if (f1 == 0.0f && (f1 != -0.0f || f2 != -0.0f))
322:                    return 0.0f;
323:                return f1;
324:            }
325:
326:            /**
327:             * Answers the most positive (i.e. closest to positive infinity) of the two
328:             * arguments.
329:             * 
330:             * @param i1
331:             *            the first argument to check
332:             * @param i2
333:             *            the second argument
334:             * @return the larger of i1 and i2.
335:             */
336:            public static int max(int i1, int i2) {
337:                return i1 > i2 ? i1 : i2;
338:            }
339:
340:            /**
341:             * Answers the most positive (i.e. closest to positive infinity) of the two
342:             * arguments.
343:             * 
344:             * @param l1
345:             *            the first argument to check
346:             * @param l2
347:             *            the second argument
348:             * @return the larger of l1 and l2.
349:             */
350:            public static long max(long l1, long l2) {
351:                return l1 > l2 ? l1 : l2;
352:            }
353:
354:            /**
355:             * Answers the most negative (i.e. closest to negative infinity) of the two
356:             * arguments.
357:             * 
358:             * @param d1
359:             *            the first argument to check
360:             * @param d2
361:             *            the second argument
362:             * @return the smaller of d1 and d2.
363:             */
364:            public static double min(double d1, double d2) {
365:                if (d1 > d2)
366:                    return d2;
367:                if (d1 < d2)
368:                    return d1;
369:                /* if either arg is NaN, return NaN */
370:                if (d1 != d2)
371:                    return Double.NaN;
372:                /* min( +0.0,-0.0) == -0.0 */
373:                if (d1 == 0.0 && (d1 == -0.0d || d2 == -0.0d))
374:                    return -0.0d;
375:                return d1;
376:            }
377:
378:            /**
379:             * Answers the most negative (i.e. closest to negative infinity) of the two
380:             * arguments.
381:             * 
382:             * @param f1
383:             *            the first argument to check
384:             * @param f2
385:             *            the second argument
386:             * @return the smaller of f1 and f2.
387:             */
388:            public static float min(float f1, float f2) {
389:                if (f1 > f2)
390:                    return f2;
391:                if (f1 < f2)
392:                    return f1;
393:                /* if either arg is NaN, return NaN */
394:                if (f1 != f2)
395:                    return Float.NaN;
396:                /* min( +0.0,-0.0) == -0.0 */
397:                if (f1 == 0.0f && (f1 == -0.0f || f2 == -0.0f))
398:                    return -0.0f;
399:                return f1;
400:            }
401:
402:            /**
403:             * Answers the most negative (i.e. closest to negative infinity) of the two
404:             * arguments.
405:             * 
406:             * @param i1
407:             *            the first argument to check
408:             * @param i2
409:             *            the second argument
410:             * @return the smaller of i1 and i2.
411:             */
412:            public static int min(int i1, int i2) {
413:                return i1 < i2 ? i1 : i2;
414:            }
415:
416:            /**
417:             * Answers the most negative (i.e. closest to negative infinity) of the two
418:             * arguments.
419:             * 
420:             * @param l1
421:             *            the first argument to check
422:             * @param l2
423:             *            the second argument
424:             * @return the smaller of l1 and l2.
425:             */
426:            public static long min(long l1, long l2) {
427:                return l1 < l2 ? l1 : l2;
428:            }
429:
430:            /**
431:             * Answers the closest double approximation of the result of raising the
432:             * first argument to the power of the second.
433:             * 
434:             * @param d1
435:             *            the base of the operation.
436:             * @param d2
437:             *            the exponent of the operation.
438:             * @return d1 to the power of d2
439:             */
440:            public static native double pow(double d1, double d2);
441:
442:            /**
443:             * Answers the double conversion of the result of rounding the argument to
444:             * an integer.
445:             * 
446:             * @param d
447:             *            the value to be converted
448:             * @return the closest integer to the argument (as a double).
449:             */
450:            public static double rint(double d) {
451:                if (d == +0.0d || d == -0.0d) {
452:                    return d;
453:                }
454:                double res = floor(d + 0.5d);
455:                return res - d == 0.5d && res % 2 != 0 ? res - 1 : res;
456:            }
457:
458:            /**
459:             * Answers the result of rounding the argument to an integer.
460:             * 
461:             * @param d
462:             *            the value to be converted
463:             * @return the closest integer to the argument.
464:             */
465:            public static long round(double d) {
466:                // check for NaN
467:                if (d != d)
468:                    return 0L;
469:                return (long) floor(d + 0.5d);
470:            }
471:
472:            /**
473:             * Answers the result of rounding the argument to an integer.
474:             * 
475:             * @param f
476:             *            the value to be converted
477:             * @return the closest integer to the argument.
478:             */
479:            public static int round(float f) {
480:                // check for NaN
481:                if (f != f)
482:                    return 0;
483:                return (int) floor(f + 0.5f);
484:            }
485:
486:            /**
487:             * Answers the signum function of the argument. If the argument is less than
488:             * zero, it answers -1.0. If greater than zero, 1.0 is returned. It returns
489:             * zero if the argument is also zero.
490:             * 
491:             * @param d
492:             *            the value to compute signum function of
493:             * @return the value of the signum function.
494:             */
495:            public static double signum(double d) {
496:                return StrictMath.signum(d);
497:            }
498:
499:            /**
500:             * Answers the signum function of the argument. If the argument is less than
501:             * zero, it answers -1.0. If greater than zero, 1.0 is returned. It returns
502:             * zero if the argument is also zero.
503:             * 
504:             * @param f
505:             *            the value to compute signum function of
506:             * @return the value of the signum function.
507:             */
508:            public static float signum(float f) {
509:                return StrictMath.signum(f);
510:            }
511:
512:            /**
513:             * Answers the closest double approximation of the sine of the argument
514:             * 
515:             * @param d
516:             *            the value to compute sin of
517:             * @return the sine of the argument.
518:             */
519:            public static native double sin(double d);
520:
521:            /**
522:             * Answers the closest double approximation of the hyperbolic sine of the
523:             * argument. The final result should be within 2.5ulps of the real result.
524:             * 
525:             * @param d
526:             *            the value to compute hyperbolic sine of
527:             * @return the hyperbolic sine of the argument.
528:             */
529:            public static native double sinh(double d);
530:
531:            /**
532:             * Answers the closest double approximation of the square root of the
533:             * argument
534:             * 
535:             * @param d
536:             *            the value to compute sqrt of
537:             * @return the square root of the argument.
538:             */
539:            public static native double sqrt(double d);
540:
541:            /**
542:             * Answers the closest double approximation of the tangent of the argument
543:             * 
544:             * @param d
545:             *            the value to compute tan of
546:             * @return the tangent of the argument.
547:             */
548:            public static native double tan(double d);
549:
550:            /**
551:             * Answers the closest double approximation of the hyperbolic tangent of the
552:             * argument. The absolute value is always less than 1. The final result
553:             * should be within 2.5ulps of the real result. If the real result is 
554:             * within 0.5ulp of 1 or -1, it should answer exactly +1 or -1.
555:             * 
556:             * @param d
557:             *            the value to compute hyperbolic tangent of
558:             * @return the hyperbolic tangent of the argument.
559:             */
560:            public static native double tanh(double d);
561:
562:            /**
563:             * Returns a pseudo-random number between 0.0 and 1.0.
564:             * 
565:             * @return a pseudo-random number
566:             */
567:            public static double random() {
568:                if (random == null) {
569:                    random = new java.util.Random();
570:                }
571:                return random.nextDouble();
572:            }
573:
574:            /**
575:             * Returns the measure in radians of the supplied degree angle
576:             * 
577:             * @param angdeg
578:             *            an angle in degrees
579:             * @return the radian measure of the angle.
580:             */
581:            public static double toRadians(double angdeg) {
582:                return angdeg / 180d * PI;
583:            }
584:
585:            /**
586:             * Returns the measure in degrees of the supplied radian angle
587:             * 
588:             * @param angrad
589:             *            an angle in radians
590:             * @return the degree measure of the angle.
591:             */
592:            public static double toDegrees(double angrad) {
593:                return angrad * 180d / PI;
594:            }
595:
596:            /**
597:             * Answers the argument's ulp. The size of a ulp of a double value is the
598:             * positive distance between this value and the double value next larger
599:             * in magnitude. For non-NaN x, ulp(-x) == ulp(x).
600:             * 
601:             * @param d
602:             *            the floating-point value to compute ulp of
603:             * @return the size of a ulp of the argument.
604:             */
605:            public static double ulp(double d) {
606:                // special cases
607:                if (Double.isInfinite(d)) {
608:                    return Double.POSITIVE_INFINITY;
609:                } else if (d == Double.MAX_VALUE || d == -Double.MAX_VALUE) {
610:                    return pow(2, 971);
611:                }
612:                d = abs(d);
613:                return nextafter(d, Double.MAX_VALUE) - d;
614:            }
615:
616:            /**
617:             * Answers the argument's ulp. The size of a ulp of a float value is the
618:             * positive distance between this value and the float value next larger
619:             * in magnitude. For non-NaN x, ulp(-x) == ulp(x).
620:             * 
621:             * @param f
622:             *            the floating-point value to compute ulp of
623:             * @return the size of a ulp of the argument.
624:             */
625:            public static float ulp(float f) {
626:                // special cases
627:                if (Float.isNaN(f)) {
628:                    return Float.NaN;
629:                } else if (Float.isInfinite(f)) {
630:                    return Float.POSITIVE_INFINITY;
631:                } else if (f == Float.MAX_VALUE || f == -Float.MAX_VALUE) {
632:                    return (float) pow(2, 104);
633:                }
634:                f = abs(f);
635:                return nextafterf(f, Float.MAX_VALUE) - f;
636:            }
637:
638:            private native static double nextafter(double x, double y);
639:
640:            private native static float nextafterf(float x, float y);
641:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.