Home » Java programming language

Java Math Class static float abs(float f) method with example

Java Math Class static float abs(float f) method: Here, we are going to learn about the static float abs(float f) method of Math Class with its syntax and example.
Submitted by Preeti Jain, on August 19, 2019

Math Class static float abs(float f)

  • This method is available in java.lang package.
  • This method is used to return the absolute value of the given parameter in the method.
  • This is a static method so this method is accessible with classname too.
  • The return type of this method depends on given argument datatype and here we are passing float data type in the method that means the return type of this method is float.
  • In this method, we pass only one parameter as an argument in the method of Math class.
  • This method does not throw any exception.
  • This is an overridable method so various versions of this method are available but here we are looking float datatype argument so the following syntax is given below,

Syntax:

    public static float abs(float f){
    }

Parameter(s):

float f – a float value whose absolute value to be found.

Return value:

The return type of this method is float, returns absolute value as a float.

Note:

  • If we pass a positive float, it returns the same value.
  • If we pass a negative float, it returns the value without sign i.e. it returns the positive float.
  • If we pass positive zero (0) or negative zero (-0), it returns zero without sign (0).
  • If we pass an infinity parameter, it returns the same infinity value without the sign.
  • If we pass "NaN" (Not A Number), it returns the same i.e. a "NaN".

Java program to demonstrate example of abs(float f) method

// Java program to demonstrate the example of 
// abs(float f) method of Math class

class AbsFloatTypeMethod {
    public static void main(String[] args) {
        // We are declaring few variables
        float a = 123.121f;
        float b = -123.121f;
        float c = 0.0f;
        float d = -0.0f;
        float e = 7.70f / 0.0f;
        float f = -7.70f / 0.0f;

        // By using abs(float f) method, 
        // we will find the absolute value of given parameter 
        System.out.println("The absolute value of a is : " + Math.abs(a));
        System.out.println("The absolute value of b is : " + Math.abs(b));
        System.out.println("The absolute value of c is : " + Math.abs(c));
        System.out.println("The absolute value of d is : " + Math.abs(d));
        System.out.println("The absolute value of e is : " + Math.abs(e));
        System.out.println("The absolute value of f is : " + Math.abs(f));
    }
}

Output

E:\Programs>javac AbsFloatTypeMetho.java

E:\Programs>java AbsFloatTypeMethod
The absolute value of a is : 123.121
The absolute value of b is : 123.121
The absolute value of c is : 0.0
The absolute value of d is : 0.0
The absolute value of e is : Infinity
The absolute value of f is : Infinity


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.