Found 34494 Articles for Programming

Trigonometric methods in Java

karthikeya Boyini
Updated on 26-Jun-2020 09:04:16

327 Views

The java.lang.Math class contains methods for performing basic numeric operations such as the trigonometry, logarithm, etc.The following are some of the methods.Sr.NoMethods & Description1static double abs(double a)This method returns the absolute value of a double value.2static float abs(float a)This method returns the absolute value of a float value.3static int abs(int a)This method returns the absolute value of an int value.4static long abs(long a)This method returns the absolute value of a long value.5static double acos(double a)This method returns the arc cosine of a value; the returned angle is in the range 0.0 through pi.6static double asin(double a)This method returns the arc ... Read More

Format double type in Java

Samual Sam
Updated on 26-Jun-2020 09:05:22

2K+ Views

Let’s say we have the following three values −double val1 = 15.546; double val2 = 25.87; double val3 = Math.PI;Let us now format these double-type numbers. Firstly, we are formatting Euler’s number with Math.exp(). After that, we have also evaluated log. The %.3f you can see here is what we used for formatting the numbers.System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1));The following is an example where we have also shown other ways to format double in Java.Example Live Demopublic class Demo {    public static void main(String args[]) {       double val1 = 15.546;     ... Read More

Double isNaN() method in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:51:15

116 Views

The java.lang.Double.isNan() method returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.Let’s say the following are our Double values.Double val1 = new Double(3/0.); Double val2 = new Double(0/0.);Now, we will use the isNan() method to check whether the number is a NaN or not.val1.isNaN(); val2.isNaN()The following is our final example.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double val1 = new Double(3/0.);       Double val2 = new Double(0/0.);       System.out.println(val1.isNaN());       System.out.println(val2.isNaN());    } }Outputfalse true

Double isInfinite() method in Java

Samual Sam
Updated on 26-Jun-2020 08:51:51

100 Views

The Double isInfinite() method returns true if this Double value is infinitely large in magnitude, false otherwise.Let’s say we have the following Double values.Double val1 = new Double(3/0.); Double val2 = new Double(0/0.);Use the isInfinite() method now. The return value is a boolean.val1.isInfinite(); val2.isInfinite();The following is the final example.Example Live Demopublic class Demo {    public static void main(String args[]) {       Double val1 = new Double(3/0.);       Double val2 = new Double(0/0.);       System.out.println(val1.isInfinite());       System.out.println(val2.isInfinite());    } }Outputtrue false

Convert double primitive type to a Double object in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:52:45

6K+ Views

To convert double primitive type to a Double object, you need to use Double constructor.Let’s say the following is our double primitive.// double primitive double val = 23.78;To convert it to a Double object, use Double constructor.// Double object Double ob = new Double(val);Example Live Demopublic class Demo {    public static void main(String args[]) {       // double primitive       double val = 23.78;       // Double object       Double ob = new Double(val);       System.out.println(ob);    } }Output23.78

Format floating point number in Java

Samual Sam
Updated on 26-Jun-2020 08:53:34

1K+ Views

Let’s say we have the following two values.double val1 = 20.932; double val2 = 11.67;Let us now format these floating-point numbers. Firstly, we are formatting euler’s number withMath.exp(). After that, we have also evaluated log. The %.3f you can see here is what we used for formatting the numbers.System.out.printf("exp(%.3f) = %.3f%n", val1, Math.exp(val1)); System.out.printf("log = %.3f%n", val1, Math.log(val1));The following is an example where we have also shown other ways to format float in Java.Example Live Demopublic class Demo {    public static void main(String args[]) {       double val1 = 20.932;       double val2 = 11.67;   ... Read More

Check two float arrays for equality in Java

karthikeya Boyini
Updated on 26-Jun-2020 08:45:58

115 Views

To check two float arrays for equality, use the Arrays.equals() method.In our example, we have the following two float arrays.float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 3.2f, 5.5f, 5.3f };Let us now compare them for equality.if (Arrays.equals(floatVal1, floatVal2)) { System.out.println("Both are equal!"); }The following is an example wherein we compare arrays and with that also use == to check for other conditions.Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) {       float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f };       ... Read More

Java Program to compare Two Java float Arrays

Samual Sam
Updated on 26-Jun-2020 08:46:48

120 Views

To compare Java float arrays, use the Arrays.equals() method. The return value is a boolean. Let’s say we have the following float arrays −float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f }; float[] floatVal2 = new float[] { 8.3f, 8.8f, 9.2f }; float[] floatVal3 = new float[] { 6.2f, 6.9f, 9.9f }; float[] floatVal4 = new float[] { 3.2f, 5.5f, 5.3f };To compare them, the Arrays.equals() method is to be used −Arrays.equals(floatVal1, floatVal2); Arrays.equals(floatVal2, floatVal3); Arrays.equals(floatVal3, floatVal4);The following is the complete example wherein we compare all the arrays −Example Live Demoimport java.util.Arrays; public class Demo {    public static void ... Read More

Java Program to convert a String to a float type Number

karthikeya Boyini
Updated on 26-Jun-2020 08:47:30

154 Views

Use the parseFloat() method in Java to convert a String to a float type.Let’s say we have the following string.String str = "111.8";Now, the Float.parseFloat() method will convert it to a float type number.float floatVal = Float.parseFloat(str);The following is the complete example with output.Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = "111.8";       float floatVal = Float.parseFloat(str);       System.out.println("Float: "+floatVal);    } }OutputFloat: 111.8

Convert from String to float in Java

Samual Sam
Updated on 26-Jun-2020 08:48:08

895 Views

To convert String to float, use the valueOf() method.Let’s say we have the following string value.String str = "0.8";Converting the string to float.Float floatVal = Float.valueOf(str).floatValue();The following is the complete example.Example Live Demopublic class Demo {    public static void main(String args[]) {       String str = "0.8";       Float floatVal = Float.valueOf(str).floatValue();       System.out.println("Float: "+floatVal);    } }OutputFloat: 0.8

Advertisements