~ubuntu-branches/ubuntu/precise/classpath/precise

« back to all changes in this revision

Viewing changes to java/lang/Math.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-05-27 16:11:15 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060527161115-h6e39eposdt5snb6
Tags: 2:0.91-3
* Install header files to /usr/include/classpath.
* debian/control: classpath: Conflict with jamvm < 1.4.3 and
  cacao < 0.96 (Closes: #368172).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* java.lang.Math -- common mathematical functions, native allowed
2
 
   Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
 
1
/* java.lang.Math -- common mathematical functions, native allowed (VMMath)
 
2
   Copyright (C) 1998, 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
3
3
 
4
4
This file is part of GNU Classpath.
5
5
 
52
52
 * @author Paul Fisher
53
53
 * @author John Keiser
54
54
 * @author Eric Blake (ebb9@email.byu.edu)
 
55
 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
55
56
 * @since 1.0
56
57
 */
57
58
public final class Math
58
59
{
 
60
 
 
61
  // FIXME - This is here because we need to load the "javalang" system
 
62
  // library somewhere late in the bootstrap cycle. We cannot do this
 
63
  // from VMSystem or VMRuntime since those are used to actually load
 
64
  // the library. This is mainly here because historically Math was
 
65
  // late enough in the bootstrap cycle to start using System after it
 
66
  // was initialized (called from the java.util classes).
 
67
  static
 
68
  {
 
69
    if (Configuration.INIT_LOAD_LIBRARY)
 
70
      {
 
71
        System.loadLibrary("javalang");
 
72
      }
 
73
  }
 
74
 
59
75
  /**
60
76
   * Math is non-instantiable
61
77
   */
63
79
  {
64
80
  }
65
81
 
66
 
  static
67
 
  {
68
 
    if (Configuration.INIT_LOAD_LIBRARY)
69
 
      {
70
 
        System.loadLibrary("javalang");
71
 
      }
72
 
  }
73
 
 
74
82
  /**
75
83
   * A random number generator, initialized on first use.
76
84
   */
298
306
   * @param a the angle (in radians)
299
307
   * @return sin(a)
300
308
   */
301
 
  public static native double sin(double a);
 
309
  public static double sin(double a)
 
310
  {
 
311
    return VMMath.sin(a);
 
312
  }
302
313
 
303
314
  /**
304
315
   * The trigonometric function <em>cos</em>. The cosine of NaN or infinity is
307
318
   * @param a the angle (in radians)
308
319
   * @return cos(a)
309
320
   */
310
 
  public static native double cos(double a);
 
321
  public static double cos(double a)
 
322
  {
 
323
    return VMMath.cos(a);
 
324
  }
311
325
 
312
326
  /**
313
327
   * The trigonometric function <em>tan</em>. The tangent of NaN or infinity
317
331
   * @param a the angle (in radians)
318
332
   * @return tan(a)
319
333
   */
320
 
  public static native double tan(double a);
 
334
  public static double tan(double a)
 
335
  {
 
336
    return VMMath.tan(a);
 
337
  }
321
338
 
322
339
  /**
323
340
   * The trigonometric function <em>arcsin</em>. The range of angles returned
328
345
   * @param a the sin to turn back into an angle
329
346
   * @return arcsin(a)
330
347
   */
331
 
  public static native double asin(double a);
 
348
  public static double asin(double a)
 
349
  {
 
350
    return VMMath.asin(a);
 
351
  }
332
352
 
333
353
  /**
334
354
   * The trigonometric function <em>arccos</em>. The range of angles returned
339
359
   * @param a the cos to turn back into an angle
340
360
   * @return arccos(a)
341
361
   */
342
 
  public static native double acos(double a);
 
362
  public static double acos(double a)
 
363
  {
 
364
    return VMMath.acos(a);
 
365
  }
343
366
 
344
367
  /**
345
368
   * The trigonometric function <em>arcsin</em>. The range of angles returned
351
374
   * @return arcsin(a)
352
375
   * @see #atan2(double, double)
353
376
   */
354
 
  public static native double atan(double a);
 
377
  public static double atan(double a)
 
378
  {
 
379
    return VMMath.atan(a);
 
380
  }
355
381
 
356
382
  /**
357
383
   * A special version of the trigonometric function <em>arctan</em>, for
400
426
   * @return <em>theta</em> in the conversion of (x, y) to (r, theta)
401
427
   * @see #atan(double)
402
428
   */
403
 
  public static native double atan2(double y, double x);
 
429
  public static double atan2(double y, double x)
 
430
  {
 
431
    return VMMath.atan2(y,x);
 
432
  }
404
433
 
405
434
  /**
406
435
   * Take <em>e</em><sup>a</sup>.  The opposite of <code>log()</code>. If the
414
443
   * @see #log(double)
415
444
   * @see #pow(double, double)
416
445
   */
417
 
  public static native double exp(double a);
 
446
  public static double exp(double a)
 
447
  {
 
448
    return VMMath.exp(a);
 
449
  }
418
450
 
419
451
  /**
420
452
   * Take ln(a) (the natural log).  The opposite of <code>exp()</code>. If the
430
462
   * @return the natural log of <code>a</code>
431
463
   * @see #exp(double)
432
464
   */
433
 
  public static native double log(double a);
 
465
  public static double log(double a)
 
466
  {
 
467
    return VMMath.log(a);
 
468
  }
434
469
 
435
470
  /**
436
471
   * Take a square root. If the argument is NaN or negative, the result is
438
473
   * infinity; and if the result is either zero, the result is the same.
439
474
   * This is accurate within the limits of doubles.
440
475
   *
441
 
   * <p>For other roots, use pow(a, 1 / rootNumber).
 
476
   * <p>For a cube root, use <code>cbrt</code>.  For other roots, use
 
477
   * <code>pow(a, 1 / rootNumber)</code>.</p>
442
478
   *
443
479
   * @param a the numeric argument
444
480
   * @return the square root of the argument
 
481
   * @see #cbrt(double)
445
482
   * @see #pow(double, double)
446
483
   */
447
 
  public static native double sqrt(double a);
 
484
  public static double sqrt(double a)
 
485
  {
 
486
    return VMMath.sqrt(a);
 
487
  }
448
488
 
449
489
  /**
450
490
   * Raise a number to a power. Special cases:<ul>
514
554
   * @param b the power to raise it to
515
555
   * @return a<sup>b</sup>
516
556
   */
517
 
  public static native double pow(double a, double b);
 
557
  public static double pow(double a, double b)
 
558
  {
 
559
    return VMMath.pow(a,b);
 
560
  }
518
561
 
519
562
  /**
520
563
   * Get the IEEE 754 floating point remainder on two numbers. This is the
530
573
   * @return the IEEE 754-defined floating point remainder of x/y
531
574
   * @see #rint(double)
532
575
   */
533
 
  public static native double IEEEremainder(double x, double y);
 
576
  public static double IEEEremainder(double x, double y)
 
577
  {
 
578
    return VMMath.IEEEremainder(x,y);
 
579
  }
534
580
 
535
581
  /**
536
582
   * Take the nearest integer that is that is greater than or equal to the
541
587
   * @param a the value to act upon
542
588
   * @return the nearest integer &gt;= <code>a</code>
543
589
   */
544
 
  public static native double ceil(double a);
 
590
  public static double ceil(double a)
 
591
  {
 
592
    return VMMath.ceil(a);
 
593
  }
545
594
 
546
595
  /**
547
596
   * Take the nearest integer that is that is less than or equal to the
551
600
   * @param a the value to act upon
552
601
   * @return the nearest integer &lt;= <code>a</code>
553
602
   */
554
 
  public static native double floor(double a);
 
603
  public static double floor(double a)
 
604
  {
 
605
    return VMMath.floor(a);
 
606
  }
555
607
 
556
608
  /**
557
609
   * Take the nearest integer to the argument.  If it is exactly between
561
613
   * @param a the value to act upon
562
614
   * @return the nearest integer to <code>a</code>
563
615
   */
564
 
  public static native double rint(double a);
 
616
  public static double rint(double a)
 
617
  {
 
618
    return VMMath.rint(a);
 
619
  }
565
620
 
566
621
  /**
567
622
   * Take the nearest integer to the argument.  This is equivalent to
647
702
  {
648
703
    return (rads * 180) / PI;
649
704
  }
 
705
 
 
706
  /**
 
707
   * <p>
 
708
   * Take a cube root. If the argument is <code>NaN</code>, an infinity or
 
709
   * zero, then the original value is returned.  The returned result is
 
710
   * within 1 ulp of the exact result.  For a finite value, <code>x</code>,
 
711
   * the cube root of <code>-x</code> is equal to the negation of the cube root
 
712
   * of <code>x</code>. 
 
713
   * </p>
 
714
   * <p>
 
715
   * For a square root, use <code>sqrt</code>.  For other roots, use
 
716
   * <code>pow(a, 1 / rootNumber)</code>.
 
717
   * </p>
 
718
   *
 
719
   * @param a the numeric argument
 
720
   * @return the cube root of the argument
 
721
   * @see #sqrt(double)
 
722
   * @see #pow(double, double)
 
723
   * @since 1.5
 
724
   */
 
725
  public static double cbrt(double a)
 
726
  {
 
727
    return VMMath.cbrt(a);
 
728
  }
 
729
 
 
730
  /**
 
731
   * <p>
 
732
   * Returns the hyperbolic cosine of the given value.  For a value,
 
733
   * <code>x</code>, the hyperbolic cosine is <code>(e<sup>x</sup> + 
 
734
   * e<sup>-x</sup>)/2</code>
 
735
   * with <code>e</code> being <a href="#E">Euler's number</a>.  The returned
 
736
   * result is within 2.5 ulps of the exact result.
 
737
   * </p>
 
738
   * <p>
 
739
   * If the supplied value is <code>NaN</code>, then the original value is
 
740
   * returned.  For either infinity, positive infinity is returned.
 
741
   * The hyperbolic cosine of zero is 1.0.
 
742
   * </p>
 
743
   * 
 
744
   * @param a the numeric argument
 
745
   * @return the hyperbolic cosine of <code>a</code>.
 
746
   * @since 1.5
 
747
   */
 
748
  public static double cosh(double a)
 
749
  {
 
750
    return VMMath.cosh(a);
 
751
  }
 
752
 
 
753
  /**
 
754
   * <p>
 
755
   * Returns <code>e<sup>a</sup> - 1.  For values close to 0, the
 
756
   * result of <code>expm1(a) + 1</code> tend to be much closer to the
 
757
   * exact result than simply <code>exp(x)</code>.  The result is within
 
758
   * 1 ulp of the exact result, and results are semi-monotonic.  For finite
 
759
   * inputs, the returned value is greater than or equal to -1.0.  Once
 
760
   * a result enters within half a ulp of this limit, the limit is returned.
 
761
   * </p>   
 
762
   * <p>
 
763
   * For <code>NaN</code>, positive infinity and zero, the original value
 
764
   * is returned.  Negative infinity returns a result of -1.0 (the limit).
 
765
   * </p>
 
766
   * 
 
767
   * @param a the numeric argument
 
768
   * @return <code>e<sup>a</sup> - 1</code>
 
769
   * @since 1.5
 
770
   */
 
771
  public static double expm1(double a)
 
772
  {
 
773
    return VMMath.expm1(a);
 
774
  }
 
775
 
 
776
  /**
 
777
   * <p>
 
778
   * Returns the hypotenuse, <code>a<sup>2</sup> + b<sup>2</sup></code>,
 
779
   * without intermediate overflow or underflow.  The returned result is
 
780
   * within 1 ulp of the exact result.  If one parameter is held constant,
 
781
   * then the result in the other parameter is semi-monotonic.
 
782
   * </p>
 
783
   * <p>
 
784
   * If either of the arguments is an infinity, then the returned result
 
785
   * is positive infinity.  Otherwise, if either argument is <code>NaN</code>,
 
786
   * then <code>NaN</code> is returned.
 
787
   * </p>
 
788
   * 
 
789
   * @param a the first parameter.
 
790
   * @param b the second parameter.
 
791
   * @return the hypotenuse matching the supplied parameters.
 
792
   * @since 1.5
 
793
   */
 
794
  public static double hypot(double a, double b)
 
795
  {
 
796
    return VMMath.hypot(a,b);
 
797
  }
 
798
 
 
799
  /**
 
800
   * <p>
 
801
   * Returns the base 10 logarithm of the supplied value.  The returned
 
802
   * result is within 1 ulp of the exact result, and the results are
 
803
   * semi-monotonic.
 
804
   * </p>
 
805
   * <p>
 
806
   * Arguments of either <code>NaN</code> or less than zero return
 
807
   * <code>NaN</code>.  An argument of positive infinity returns positive
 
808
   * infinity.  Negative infinity is returned if either positive or negative
 
809
   * zero is supplied.  Where the argument is the result of
 
810
   * <code>10<sup>n</sup</code>, then <code>n</code> is returned.
 
811
   * </p>
 
812
   *
 
813
   * @param a the numeric argument.
 
814
   * @return the base 10 logarithm of <code>a</code>.
 
815
   * @since 1.5
 
816
   */
 
817
  public static double log10(double a)
 
818
  {
 
819
    return VMMath.log10(a);
 
820
  }
 
821
 
 
822
  /**
 
823
   * <p>
 
824
   * Returns the natural logarithm resulting from the sum of the argument,
 
825
   * <code>a</code> and 1.  For values close to 0, the
 
826
   * result of <code>log1p(a)</code> tend to be much closer to the
 
827
   * exact result than simply <code>log(1.0+a)</code>.  The returned
 
828
   * result is within 1 ulp of the exact result, and the results are
 
829
   * semi-monotonic.
 
830
   * </p>
 
831
   * <p>
 
832
   * Arguments of either <code>NaN</code> or less than -1 return
 
833
   * <code>NaN</code>.  An argument of positive infinity or zero
 
834
   * returns the original argument.  Negative infinity is returned from an
 
835
   * argument of -1.
 
836
   * </p>
 
837
   *
 
838
   * @param a the numeric argument.
 
839
   * @return the natural logarithm of <code>a</code> + 1.
 
840
   * @since 1.5
 
841
   */
 
842
  public static double log1p(double a)
 
843
  {
 
844
    return VMMath.log1p(a);
 
845
  }
 
846
 
 
847
  /**
 
848
   * <p>
 
849
   * Returns the sign of the argument as follows:
 
850
   * </p>
 
851
   * <ul>
 
852
   * <li>If <code>a</code> is greater than zero, the result is 1.0.</li>
 
853
   * <li>If <code>a</code> is less than zero, the result is -1.0.</li>
 
854
   * <li>If <code>a</code> is <code>NaN</code>, the result is <code>NaN</code>.
 
855
   * <li>If <code>a</code> is positive or negative zero, the result is the
 
856
   * same.</li>
 
857
   * </ul>
 
858
   * 
 
859
   * @param a the numeric argument.
 
860
   * @return the sign of the argument.
 
861
   * @since 1.5.
 
862
   */
 
863
  public static double signum(double a)
 
864
  {
 
865
    if (Double.isNaN(a))
 
866
      return Double.NaN;
 
867
    if (a > 0)
 
868
      return 1.0;
 
869
    if (a < 0)
 
870
      return -1.0;
 
871
    return a;
 
872
  }
 
873
 
 
874
  /**
 
875
   * <p>
 
876
   * Returns the sign of the argument as follows:
 
877
   * </p>
 
878
   * <ul>
 
879
   * <li>If <code>a</code> is greater than zero, the result is 1.0f.</li>
 
880
   * <li>If <code>a</code> is less than zero, the result is -1.0f.</li>
 
881
   * <li>If <code>a</code> is <code>NaN</code>, the result is <code>NaN</code>.
 
882
   * <li>If <code>a</code> is positive or negative zero, the result is the
 
883
   * same.</li>
 
884
   * </ul>
 
885
   * 
 
886
   * @param a the numeric argument.
 
887
   * @return the sign of the argument.
 
888
   * @since 1.5.
 
889
   */
 
890
  public static float signum(float a)
 
891
  {
 
892
    if (Float.isNaN(a))
 
893
      return Float.NaN;
 
894
    if (a > 0)
 
895
      return 1.0f;
 
896
    if (a < 0)
 
897
      return -1.0f;
 
898
    return a;
 
899
  }
 
900
 
 
901
  /**
 
902
   * <p>
 
903
   * Returns the hyperbolic sine of the given value.  For a value,
 
904
   * <code>x</code>, the hyperbolic sine is <code>(e<sup>x</sup> - 
 
905
   * e<sup>-x</sup>)/2</code>
 
906
   * with <code>e</code> being <a href="#E">Euler's number</a>.  The returned
 
907
   * result is within 2.5 ulps of the exact result.
 
908
   * </p>
 
909
   * <p>
 
910
   * If the supplied value is <code>NaN</code>, an infinity or a zero, then the
 
911
   * original value is returned.
 
912
   * </p>
 
913
   * 
 
914
   * @param a the numeric argument
 
915
   * @return the hyperbolic sine of <code>a</code>.
 
916
   * @since 1.5
 
917
   */
 
918
  public static double sinh(double a)
 
919
  {
 
920
    return VMMath.sinh(a);
 
921
  }
 
922
 
 
923
  /**
 
924
   * <p>
 
925
   * Returns the hyperbolic tangent of the given value.  For a value,
 
926
   * <code>x</code>, the hyperbolic tangent is <code>(e<sup>x</sup> - 
 
927
   * e<sup>-x</sup>)/(e<sup>x</sup> + e<sup>-x</sup>)</code>
 
928
   * (i.e. <code>sinh(a)/cosh(a)</code>)
 
929
   * with <code>e</code> being <a href="#E">Euler's number</a>.  The returned
 
930
   * result is within 2.5 ulps of the exact result.  The absolute value
 
931
   * of the exact result is always less than 1.  Computed results are thus
 
932
   * less than or equal to 1 for finite arguments, with results within
 
933
   * half a ulp of either positive or negative 1 returning the appropriate
 
934
   * limit value (i.e. as if the argument was an infinity).
 
935
   * </p>
 
936
   * <p>
 
937
   * If the supplied value is <code>NaN</code> or zero, then the original
 
938
   * value is returned.  Positive infinity returns +1.0 and negative infinity
 
939
   * returns -1.0.
 
940
   * </p>
 
941
   * 
 
942
   * @param a the numeric argument
 
943
   * @return the hyperbolic tangent of <code>a</code>.
 
944
   * @since 1.5
 
945
   */
 
946
  public static double tanh(double a)
 
947
  {
 
948
    return VMMath.tanh(a);
 
949
  }
 
950
 
 
951
  /**
 
952
   * Return the ulp for the given double argument.  The ulp is the
 
953
   * difference between the argument and the next larger double.  Note
 
954
   * that the sign of the double argument is ignored, that is,
 
955
   * ulp(x) == ulp(-x).  If the argument is a NaN, then NaN is returned.
 
956
   * If the argument is an infinity, then +Inf is returned.  If the
 
957
   * argument is zero (either positive or negative), then
 
958
   * {@link Double#MIN_VALUE} is returned.
 
959
   * @param d the double whose ulp should be returned
 
960
   * @return the difference between the argument and the next larger double
 
961
   * @since 1.5
 
962
   */
 
963
  public static double ulp(double d)
 
964
  {
 
965
    if (Double.isNaN(d))
 
966
      return d;
 
967
    if (Double.isInfinite(d))
 
968
      return Double.POSITIVE_INFINITY;
 
969
    // This handles both +0.0 and -0.0.
 
970
    if (d == 0.0)
 
971
      return Double.MIN_VALUE;
 
972
    long bits = Double.doubleToLongBits(d);
 
973
    final int mantissaBits = 52;
 
974
    final int exponentBits = 11;
 
975
    final long mantMask = (1L << mantissaBits) - 1;
 
976
    long mantissa = bits & mantMask;
 
977
    final long expMask = (1L << exponentBits) - 1;
 
978
    long exponent = (bits >>> mantissaBits) & expMask;
 
979
 
 
980
    // Denormal number, so the answer is easy.
 
981
    if (exponent == 0)
 
982
      {
 
983
        long result = (exponent << mantissaBits) | 1L;
 
984
        return Double.longBitsToDouble(result);
 
985
      }
 
986
 
 
987
    // Conceptually we want to have '1' as the mantissa.  Then we would
 
988
    // shift the mantissa over to make a normal number.  If this underflows
 
989
    // the exponent, we will make a denormal result.
 
990
    long newExponent = exponent - mantissaBits;
 
991
    long newMantissa;
 
992
    if (newExponent > 0)
 
993
      newMantissa = 0;
 
994
    else
 
995
      {
 
996
        newMantissa = 1L << -(newExponent - 1);
 
997
        newExponent = 0;
 
998
      }
 
999
    return Double.longBitsToDouble((newExponent << mantissaBits) | newMantissa);
 
1000
  }
 
1001
 
 
1002
  /**
 
1003
   * Return the ulp for the given float argument.  The ulp is the
 
1004
   * difference between the argument and the next larger float.  Note
 
1005
   * that the sign of the float argument is ignored, that is,
 
1006
   * ulp(x) == ulp(-x).  If the argument is a NaN, then NaN is returned.
 
1007
   * If the argument is an infinity, then +Inf is returned.  If the
 
1008
   * argument is zero (either positive or negative), then
 
1009
   * {@link Float#MIN_VALUE} is returned.
 
1010
   * @param f the float whose ulp should be returned
 
1011
   * @return the difference between the argument and the next larger float
 
1012
   * @since 1.5
 
1013
   */
 
1014
  public static float ulp(float f)
 
1015
  {
 
1016
    if (Float.isNaN(f))
 
1017
      return f;
 
1018
    if (Float.isInfinite(f))
 
1019
      return Float.POSITIVE_INFINITY;
 
1020
    // This handles both +0.0 and -0.0.
 
1021
    if (f == 0.0)
 
1022
      return Float.MIN_VALUE;
 
1023
    int bits = Float.floatToIntBits(f);
 
1024
    final int mantissaBits = 23;
 
1025
    final int exponentBits = 8;
 
1026
    final int mantMask = (1 << mantissaBits) - 1;
 
1027
    int mantissa = bits & mantMask;
 
1028
    final int expMask = (1 << exponentBits) - 1;
 
1029
    int exponent = (bits >>> mantissaBits) & expMask;
 
1030
 
 
1031
    // Denormal number, so the answer is easy.
 
1032
    if (exponent == 0)
 
1033
      {
 
1034
        int result = (exponent << mantissaBits) | 1;
 
1035
        return Float.intBitsToFloat(result);
 
1036
      }
 
1037
 
 
1038
    // Conceptually we want to have '1' as the mantissa.  Then we would
 
1039
    // shift the mantissa over to make a normal number.  If this underflows
 
1040
    // the exponent, we will make a denormal result.
 
1041
    int newExponent = exponent - mantissaBits;
 
1042
    int newMantissa;
 
1043
    if (newExponent > 0)
 
1044
      newMantissa = 0;
 
1045
    else
 
1046
      {
 
1047
        newMantissa = 1 << -(newExponent - 1);
 
1048
        newExponent = 0;
 
1049
      }
 
1050
    return Float.intBitsToFloat((newExponent << mantissaBits) | newMantissa);
 
1051
  }
650
1052
}