Showing posts with label Maths. Show all posts
Showing posts with label Maths. Show all posts

ulp() float in Java

ulp(): This method is available in the Math class of Java.

Syntax:

float java.lang.Math.ulp(float f)

This method takes one argument of type float as its parameter. This method returns the size of an ulp of the argument. An ulp, unit in the last place, of a float value, is the positive distance between this floating-point value and the float value next larger in magnitude.

Note: For non-NaN x, ulp(-x) == ulp(x).

Special Cases: 

1. If the argument is NaN, then the result is NaN.

2. If the argument is positive or negative infinity, then the result is positive infinity.

3. If the argument is positive or negative zero, then the result is Float.MIN_VALUE.

4. If the argument is ±Float.MAX_VALUE, then the result is equal to 2104. 

Parameters: One parameter is required for this method.

f: the floating-point value whose ulp is to be returned.

Returns: the size of an ulp of the argument.

For Example:

Math.ulp(6.7) = > It returns 4.7683716E-7

Approach

Java

public class UlpFloat {
    public static void main(String[] args) {

        float f = (float6.7;
        System.out.println(Math.ulp(f));

    }
}

Output:

4.7683716E-7

ulp() double in Java

ulp(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.ulp(double d)

This method takes one argument of type double as its parameter. This method returns the size of an ulp of the argument. An ulp, unit in the last place, of a double value, is the positive distance between this floating-point value and the double value next larger in magnitude. 

Note: For non-NaN x,ulp(-x) == ulp(x).

Special Cases: 

1. If the argument is NaN, then the result is NaN.

2. If the argument is positive or negative infinity, then the result is positive infinity.

3. If the argument is positive or negative zero, then the result is Double.MIN_VALUE.

4. If the argument is ±Double.MAX_VALUE, then the result is equal to 2971.

Parameters: One parameter is required for this method.

d: the floating-point value whose ulp is to be returned.

Returns: the size of an ulp of the argument.

For Example:

Math.ulp(199.9) = > It returns 2.8421709430404007E-14

Approach

Java

public class UlpDouble {
    public static void main(String[] args) {

        double d = 199.9;
        System.out.println(Math.ulp(d));

    }
}

Output:

2.8421709430404007E-14

toRadians() in Java

toRadians(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.toRadians(double angdeg)

This method takes one argument of type double as its parameter. This method converts an angle measured in degrees to an approximately equivalent angle measured in radians.

Parameters: One parameter is required for this method.

angdeg: an angle, in degrees.

Returns: the measurement of the angle angdeg in radians.

For Example:

Math.toRadians(180.0) = > It returns 3.141592653589793

Approach

Java

public class ToRadians {
    public static void main(String[] args) {

        double angdeg = 180.0;
        System.out.println(Math.toRadians(angdeg));

    }
}

Output:

3.141592653589793

toIntExact() in Java

toIntExact(): This method is available in the Math class of Java.

Syntax:

int java.lang.Math.toIntExact(long value)

This method takes one argument of type long as its parameter. This method returns the value of the long argument, throwing an exception if the value overflows an int. 

Parameters: One parameter is required for this method.

value: the long value.

Returns: the argument as an int.

Throws: ArithmeticException - if the argument overflows an int.

For Example:

Math.toIntExact(18088) = > It returns  18088.

Approach

Java

public class TointExact {
    public static void main(String[] args) {

        long value = 18088;
        System.out.println(Math.toIntExact(value));

    }
}

Output:

18088

toDegrees() in Java

toDegrees(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.toDegrees(double angrad)

This method takes one argument of type double as its parameter. It converts an angle measured in radians to an approximately equivalent angle measured in degrees.

Parameters: One parameter is required for this method.

angrad: an angle, in radians

Returns: the measurement of the angle angrad in degrees.

For Example:

Math.toDegrees(3.14) = > It returns 179.9087476710785

Approach

Java

public class Todegrees {
    public static void main(String[] args) {

        double angrad = 3.14;
        System.out.println(Math.toDegrees(angrad));

    }
}

Output:

179.9087476710785

tanh() in Java

tanh(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.tanh(double x)

This method takes one argument of type double as its parameter. This method returns the hyperbolic tangent of a double value.The hyperbolic tangent of x is defined to be(e^x - e^-x)/(e^x + e^-x),in other words, sinh(x)/cosh(x)

Note: The absolute value of the exact tanh is always less than 1.

Special cases:

1. If the argument is NaN, then the result is NaN.

2. If the argument is zero, then the result is a zero with the same sign as the argument.

3. If the argument is positive infinity, then the result is +1.0.

4. If the argument is negative infinity, then the result is -1.0.

Parameters: One parameter is required for this method.

x: The number whose hyperbolic tangent is to be returned.

Returns: The hyperbolic tangent of x.

For Example:

Math.tanh(0.5) = > It returns 0.46211715726000974

Approach

Java

public class Tanh {
    public static void main(String[] args) {

        double x = 0.5;
        System.out.println(Math.tanh(x));

    }
}

Output:

0.46211715726000974

tan() in Java

tan(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.tan(double a)

This method takes one argument of type double as its parameter. This method returns the trigonometric tangent of an angle. 

Special cases:

1. If the argument is NaN or an infinity, then the result is NaN.

2.If the argument is zero, then the result is a zero with the same sign as the argument.

Parameters: One parameter is required for this method.

a: an angle, in radians.

Returns: the tangent of the argument.

For Example:

Math.tan(0.5) = > It returns 0.5463024898437905

Approach

Java

public class Tan {
    public static void main(String[] args) {

        double a = 0.5;
        System.out.println(Math.tan(a));

    }
}

Output:

0.5463024898437905

subtractExact() long in Java

subtractExact(): This method is available in the Math class of Java.

Syntax:

long java.lang.Math.subtractExact(long x, long y)

This method takes two arguments of type long as its parameters. This method returns the difference of the arguments, throwing an exception if the result overflows a long.

Parameters: Two parameters are required for this method.

x: the first value.

y: the second value to subtract from the first.

Returns: the result.

Throws: ArithmeticException - if the result overflows a long.

For Example:

Math.subtractExact(1919199,1981)  => It returns 1917218.

Approach

Java

public class SubtractExactLong {
    public static void main(String[] args) {

        long x = 1919199, y = 1981;
        System.out.println(Math.subtractExact(x, y));

    }
}

Output:

1917218

subtractExact() int in Java

subtractExact(): This method is available in the Math class of Java.

Syntax:

int java.lang.Math.subtractExact(int x, int y)

This method takes two arguments of type int as its parameters. This method returns the difference of the arguments, throwing an exception if the result overflows an int. 

Parameters: Two parameters are required for this method.

x: the first value.

y: the second value to subtract from the first.

Returns: the result.

Throws: ArithmeticException - if the result overflows an int.

For Example:

Math.subtractExact(56,12) = > It returns  44.

Approach

Java

public class SubtractExactInt {
    public static void main(String[] args) {

        int x = 56, y = 12;
        System.out.println(Math.subtractExact(x, y));

    }
}

Output:

44

sqrt() in Java

sqrt(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.sqrt(double a)

This method takes one argument of type double as its parameter. This method returns the correctly rounded positive square root of a double value.

Special cases:

1. If the argument is NaN or less than zero, then the result is NaN.

2. If the argument is positive infinity, then the result is positive infinity.

3. If the argument is positive zero or negative zero, then the result is the same as the argument. Otherwise, the result is the double value closest to the true mathematical square root of the argument value.

Parameters: One parameter is required for this method.

a: value.

Returns: the positive square root of a.If the argument is NaN or less than zero, the result is NaN.

For Example:

Math.sqrt(256) = > It returns 16.0

Approach

Java

public class Sqrt {
    public static void main(String[] args) {

        double a = 256;
        System.out.println(Math.sqrt(a));

    }
}

Output:

16.0

sinh() in Java

sinh(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.sinh(double x)

This method takes one argument of type double as its parameter. This method returns the hyperbolic sine of a double value. 

The hyperbolic sine of x is defined to be (e^x - e^-x)/2

where e is Euler's number.

Special cases:

1. If the argument is NaN, then the result is NaN.

2. If the argument is infinite, then the result is an infinity with the same sign as the argument.

3. If the argument is zero, then the result is a zero with the same sign as the argument.

Parameters: One parameter is required for this method.

x: The number whose hyperbolic sine is to be returned.

Returns: The hyperbolic sine of x.

For Example:

Math.sinh(2.7) = > It returns 7.406263106066543

Approach

Java

public class Sinh {
    public static void main(String[] args) {

        double x = 2.7;
        System.out.println(Math.sinh(x));

    }
}

Output:

7.406263106066543

sin() in Java

sin(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.sin(double a)

This method takes one argument of type double as its parameter. This method returns the trigonometric sine of an angle. 

Special cases:

1. If the argument is NaN or an infinity, then the result is NaN.

2. If the argument is zero, then the result is a zero with the same sign as the argument.

Parameters: One parameter is required for this method.

a: an angle, in radians.

Returns: the sine of the argument.

For Example:

Math.sin(0.5) = > It returns 0.479425538604203

Approach

Java

public class Sin {
    public static void main(String[] args) {

        double a = 0.5;
        System.out.println(Math.sin(a));

    }
}

Output:

0.479425538604203

signum() float in Java

signum(): This method is available in the Math class of Java.

Syntax:

float java.lang.Math.signum(float f)

This method takes one argument of type float as its parameter. It returns the signum function of the argument

1. Zero if the argument is zero.

2. 1.0f if the argument is greater than zero.

3. -1.0f if the argument is less than zero.

Special Cases:

1. If the argument is NaN, then the result is NaN.

2. If the argument is positive zero or negative zero, then the result is the same as the argument.

Parameters: One parameter is required for this method.

f: the floating-point value whose signum is to be returned.

Returns: the signum function of the argument.

For Example:

Math.signum(-19.9) = > It returns  -1.0

Approach

Java

public class SignumFloat {
    public static void main(String[] args) {

        float f = (float) -19.9;
        System.out.println(Math.signum(f));

    }
}

Output:

-1.0

signum() double in Java

signum(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.signum(double d)

This method takes one argument of type double as its parameter. It returns the signum function of the argument.

1. Zero if the argument is zero

2. 1.0 if the argument is greater than zero.

3.  -1.0 if the argument is less than zero.

Special Cases:

1. If the argument is NaN, then the result is NaN.

2. If the argument is positive zero or negative zero, then the result is the same as the argument.

Parameters: One parameter is required for this method.

d: the floating-point value whose signum is to be returned.

Returns: the signum function of the argument.

For Example:

Math.signum(19.8282) = > It returns 1.0

Approach

Java

public class SignumDouble {
    public static void main(String[] args) {

        double d = 19.8282;
        System.out.println(Math.signum(d));

    }
}

Output:

1.0

scalb() float in Java

scalb(): This method is available in the Math class of Java.

Syntax:

float java.lang.Math.scalb(float f, int scaleFactor)

This method takes two arguments, one of type float and another of type int as its parameter. This method returns f ×2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the float value set.

Note:

1. If the exponent of the result is between Float.MIN_EXPONENT and Float.MAX_EXPONENT, the answer is calculated exactly. 

2. If the exponent of the result would be larger than Float.MAX_EXPONENT, an infinity is returned.

Special cases:

1. If the first argument is NaN, NaN is returned.

2. If the first argument is infinite, then an infinity of the same sign is returned.

3. If the first argument is zero, then a zero of the same sign is returned.

Parameters: Two parameters are required for this method.

f: number to be scaled by a power of two.

scaleFactor: the power of 2 used to scale f.

Returns: f × 2scaleFactor.

For Example:

Math.scalb(19.8,4) = > It returns 316.8

Approach

Java

public class ScalbFloat {
    public static void main(String[] args) {

        float f = (float19.8;
        int scaleFactor = 4;
        System.out.println(Math.scalb(f, scaleFactor));

    }
}

Output:

316.8

scalb() double in Java

scalb(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.scalb(double d, int scaleFactor)

This method has two arguments one of type double and another of type int as its parameters. This method returns d ×2scaleFactor rounded as if performed by a single correctly rounded floating-point multiply to a member of the double value set.

Note:

1. If the exponent of the result is between Double.MIN_EXPONENT and Double.MAX_EXPONENT, the answer is calculated exactly. 

2. If the exponent of the result would be larger than Double.MAX_EXPONENT, an infinity is returned.

Special cases:

1. If the first argument is NaN, NaN is returned.

2. If the first argument is infinite, then an infinity of the same sign is returned.

3. If the first argument is zero, then a zero of the same sign is returned.

Parameters: Two parameters are required for this method.

d: number to be scaled by a power of two.scaleFactor power of 2 used to scale d

Returns: d × 2scaleFactor.

For Example:

Math.scalb(19.82,5) = > It returns 634.24

Approach

Java

public class ScalbDouble {
    public static void main(String[] args) {

        double d = 19.82;
        int scaleFactor = 5;
        System.out.println(Math.scalb(d, scaleFactor));

    }
}

Output:

634.24

round() float in Java

round(): This method is available in the Math class of Java.

Syntax:

int java.lang.Math.round(float a)

This method takes one argument of type float as its parameter. This method returns the closest int to the argument, with ties rounding to positive infinity.

Special cases:

1. If the argument is NaN, the result is 0.

2. If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.

3. If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.

Parameters: One parameter is required for this method.

a: a floating-point value to be rounded to an integer.

Returns: the value of the argument rounded to the nearest int value.

For Example:

Math.round(17.34) = > It returns 17.

Approach

Java

public class RoundFloat {
    public static void main(String[] args) {

        float a = (float17.34;
        System.out.println(Math.round(a));
    }
}

Output:

17

round() double in Java

round(): This method is available in the Math class of Java.

Syntax:

long java.lang.Math.round(double a)

This method takes one argument of type double as its parameter. This method returns the closest long to the argument, with ties rounding to positive infinity.

Special cases:

1. If the argument is NaN, the result is 0.

2. If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE, the result is equal to the value of Long.MIN_VALUE.

3. If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE, the result is equal to the value of Long.MAX_VALUE.

Parameters: One parameter is required for this method.

a: a floating-point value to be rounded to a long.

Returns: the value of the argument rounded to the nearest long value.

For Example:

Math.round(199.892) = > It returns 200.

Approach

Java

public class RoundDouble {
    public static void main(String[] args) {

        double a = 199.892;
        System.out.println(Math.round(a));

    }
}

Output:

200

rint() in Java

rint(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.rint(double a)

This method takes one argument of type double as its parameter. This method returns the double value that is closest in value to the argument and is equal to a mathematical integer. If two double values that are mathematical integers are equally close, the result is the integer value that is even.

Special cases:

1. If the argument value is already equal to a mathematical integer, then the result is the same as the argument.

2. If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument. 

Parameters: One parameter is required for this method.

a: a double value.

Returns: the closest floating-point value to a that is equal to a mathematical integer.

For Example:

Math.rint(10919.9191) = > It returns 10920.0

Approach

Java

public class Rint {
    public static void main(String[] args) {

        double a = 10919.9191;
        System.out.println(Math.rint(a));

    }
}

Output:

10920.0