List of usage examples for java.lang Math sqrt
@HotSpotIntrinsicCandidate public static double sqrt(double a)
From source file:InfNaN.java
public static void main(String[] argv) { double d = 123; double e = 0; if (d / e == Double.POSITIVE_INFINITY) System.out.println("Check for POSITIVE_INFINITY works"); double s = Math.sqrt(-1); if (s == Double.NaN) System.out.println("Comparison with NaN incorrectly returns true"); if (Double.isNaN(s)) System.out.println("Double.isNaN() correctly returns true"); }
From source file:Heron.java
public static void main(String[] args) { // Sides for triangle in float float af, bf, cf; float sf, areaf; // Ditto in double double ad, bd, cd; double sd, aread; // Area of triangle in float af = 12345679.0f;/* ww w . j a v a2 s . co m*/ bf = 12345678.0f; cf = 1.01233995f; sf = (af + bf + cf) / 2.0f; areaf = (float) Math.sqrt(sf * (sf - af) * (sf - bf) * (sf - cf)); System.out.println("Single precision: " + areaf); // Area of triangle in double ad = 12345679.0; bd = 12345678.0; cd = 1.01233995; sd = (ad + bd + cd) / 2.0d; aread = Math.sqrt(sd * (sd - ad) * (sd - bd) * (sd - cd)); System.out.println("Double precision: " + aread); }
From source file:QuadSolv.java
public static void main(String[] args) { double a, b, c, discr, root1, root2; // Apllying the quadratic formula // Obtain sides from user System.out.println("Applying the quadratic formula"); a = 1d;//w w w. ja va2 s.c o m b = 2d; c = 3d; // Solve the discriminant (SQRT (b^2 - 4ac) discr = Math.sqrt((b * b) - (4 * a * c)); System.out.println("Discriminant = " + discr); // Determine number of roots // if discr > 0 equation has 2 real roots // if discr == 0 equation has a repeated real root // if discr < 0 equation has imaginary roots // if discr is NaN equation has no roots // Test for NaN if (Double.isNaN(discr)) System.out.println("Equation has no roots"); if (discr > 0) { System.out.println("Equation has 2 roots"); root1 = (-b + discr) / 2 * a; root2 = (-b - discr) / 2 * a; System.out.println("First root = " + root1); System.out.println("Second roor = " + root2); } if (discr == 0) { System.out.println("Equation has 1 root"); root1 = (-b + discr) / 2 * a; System.out.println("Root = " + root1); } if (discr < 0) System.out.println("Equation has imaginary roots"); }
From source file:HypFun.java
public static void main(String[] args) { double rads, degs, sinHA, cosHA, tanHA, asinHA; // Obtain angle in degrees from user degs = 20d;//from ww w.j a v a2 s.c om // Convert degrees to radian rads = Math.toRadians(degs); // Calculate hyperbolic sine sinHA = (Math.exp(rads) - Math.exp(-rads)) / 2; System.out.println("Hyperbolic sine = " + sinHA); // Calculate Hyperbolic cosine cosHA = (Math.exp(rads) + Math.exp(-rads)) / 2; System.out.println("Hyperbolic cosine = " + cosHA); // Calculate hyperbolic tangent tanHA = sinHA / cosHA; System.out.println("Hyperbolic tangent = " + tanHA); // Calculate hyperbolic arc-sine asinHA = Math.log(sinHA + Math.sqrt((sinHA * sinHA) + 1.0)); degs = Math.toDegrees(asinHA); System.out.println("Arc hyperbolic sine = " + degs); }
From source file:Main.java
public static void main(String[] args) { int a = 10;// w w w .jav a 2s .co m int b = -50; int c = 3; double x = 25.0; double y = 3.0; double z = 4.0; System.out.println("abs(b) = " + Math.abs(b)); System.out.println("cbrt(x) = " + Math.cbrt(x)); System.out.println("exp(y) = " + Math.exp(z)); System.out.println("hypot(y, z)= " + Math.hypot(y, z)); System.out.println("log(y) = " + Math.log(y)); System.out.println("log10(y) = " + Math.log10(y)); System.out.println("max(a, b) = " + Math.max(a, b)); System.out.println("min(a, b) = " + Math.min(a, b)); System.out.println("pow(a, c) = " + Math.pow(a, c)); System.out.println("random() = " + Math.random()); System.out.println("signum(b) = " + Math.signum(b)); System.out.println("sqrt(x) = " + Math.sqrt(y)); }
From source file:FpError.java
public static void main(String[] args) { double res;/*w ww .jav a 2s . c o m*/ double divisor = 0; double dividend, root; // Get user input for numerator System.out.println("Forcing division by zero error"); dividend = 10d; res = dividend / divisor; // Test for negative invifinity if (res == Double.NEGATIVE_INFINITY) System.out.println("result is NEGATIVE_INFINITY"); if (res == Double.POSITIVE_INFINITY) System.out.println("result is POSITIVE_INFINITY"); // Test for either infinity if (Double.isInfinite(res)) System.out.println("result is infinite"); // Get user input for square root System.out.println("\nCalculating square root (try negative)"); root = 10d; res = Math.sqrt(root); if (Double.isNaN(res)) System.out.println("result is Nan"); else System.out.println("Square root = " + res); }
From source file:MainClass.java
public static void main(String args[]) { System.out.printf("Math.abs( 23.7 ) = %f\n", Math.abs(23.7)); System.out.printf("Math.abs( 0.0 ) = %f\n", Math.abs(0.0)); System.out.printf("Math.abs( -23.7 ) = %f\n", Math.abs(-23.7)); System.out.printf("Math.ceil( 9.2 ) = %f\n", Math.ceil(9.2)); System.out.printf("Math.ceil( -9.8 ) = %f\n", Math.ceil(-9.8)); System.out.printf("Math.cos( 0.0 ) = %f\n", Math.cos(0.0)); System.out.printf("Math.exp( 1.0 ) = %f\n", Math.exp(1.0)); System.out.printf("Math.exp( 2.0 ) = %f\n", Math.exp(2.0)); System.out.printf("Math.floor( 9.2 ) = %f\n", Math.floor(9.2)); System.out.printf("Math.floor( -9.8 ) = %f\n", Math.floor(-9.8)); System.out.printf("Math.log( Math.E ) = %f\n", Math.log(Math.E)); System.out.printf("Math.log( Math.E * Math.E ) = %f\n", Math.log(Math.E * Math.E)); System.out.printf("Math.max( 2.3, 12.7 ) = %f\n", Math.max(2.3, 12.7)); System.out.printf("Math.max( -2.3, -12.7 ) = %f\n", Math.max(-2.3, -12.7)); System.out.printf("Math.min( 2.3, 12.7 ) = %f\n", Math.min(2.3, 12.7)); System.out.printf("Math.min( -2.3, -12.7 ) = %f\n", Math.min(-2.3, -12.7)); System.out.printf("Math.pow( 2.0, 7.0 ) = %f\n", Math.pow(2.0, 7.0)); System.out.printf("Math.pow( 9.0, 0.5 ) = %f\n", Math.pow(9.0, 0.5)); System.out.printf("Math.sin( 0.0 ) = %f\n", Math.sin(0.0)); System.out.printf("Math.sqrt( 900.0 ) = %f\n", Math.sqrt(900.0)); System.out.printf("Math.sqrt( 9.0 ) = %f\n", Math.sqrt(9.0)); System.out.printf("Math.tan( 0.0 ) = %f\n", Math.tan(0.0)); }
From source file:Sieve.java
public static void main(String[] args) { // We will compute all primes less than the value specified on the // command line, or, if no argument, all primes less than 100. int max = 100; // Assign a default value try {//from w ww. j av a 2s . co m max = Integer.parseInt(args[0]); } // Parse user-supplied arg catch (Exception e) { } // Silently ignore exceptions. // Create an array that specifies whether each number is prime or not. boolean[] isprime = new boolean[max + 1]; // Assume that all numbers are primes, until proven otherwise. for (int i = 0; i <= max; i++) isprime[i] = true; // However, we know that 0 and 1 are not primes. Make a note of it. isprime[0] = isprime[1] = false; // To compute all primes less than max, we need to rule out // multiples of all integers less than the square root of max. int n = (int) Math.ceil(Math.sqrt(max)); // See java.lang.Math class // Now, for each integer i from 0 to n: // If i is a prime, then none of its multiples are primes, // so indicate this in the array. If i is not a prime, then // its multiples have already been ruled out by one of the // prime factors of i, so we can skip this case. for (int i = 0; i <= n; i++) { if (isprime[i]) // If i is a prime, for (int j = 2 * i; j <= max; j = j + i) // loop through multiples isprime[j] = false; // they are not prime. } // Now go look for the largest prime: int largest; for (largest = max; !isprime[largest]; largest--) ; // empty loop body // Output the result System.out.println("The largest prime less than or equal to " + max + " is " + largest); }
From source file:ExponentialDemo.java
public static void main(String[] args) { double x = 11.635; double y = 2.76; System.out.printf("The value of e is %.4f%n", Math.E); System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x)); System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x)); System.out.printf("pow(%.3f, %.3f) is %.3f%n", x, y, Math.pow(x, y)); System.out.printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x)); }
From source file:ExponentialDemo.java
public static void main(String[] args) { double x = 11.635; double y = 2.76; System.out.println("The value of e is " + Math.E); System.out.println("exp(" + x + ") is " + Math.exp(x)); System.out.println("log(" + x + ") is " + Math.log(x)); System.out.println("pow(" + x + ", " + y + ") is " + Math.pow(x, y)); System.out.println("sqrt(" + x + ") is " + Math.sqrt(x)); }