Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java ulp Function

by suresh

The Java ulp Function is one of the Java Math functions, which is to return the size of an ulp of the argument. An ulp of a float value is the positive distance between the given value and the next value that is larger in magnitude. In this article, we will show how to use Java Math.ulp function with example.

Java ulp Function Syntax

The basic syntax of the Math.ulp Function in Java Programming language is as shown below.

Math.ulp(data_type number);

Number: It can be a number or a valid numerical expression.

  • If the number argument is positive or negative double or float value, Math.ulp function will return the output.
  • If the number argument is not a number, Math.ulp function will return NaN.
  • When the number argument is positive or negative Zero, Math.ulp function will return Float.MIN_EXPONENT.
  • If the number argument is positive or negative Infinity, Math.ulp function will return Positive Infinity.
  • If it is positive or negative Float.MAX_EXPONENT, Math.ulp function will return the result as 2104.

Java Programming provides two different functions to find the ulp of the specified value. The following function will accept positive or negative float value as an argument and returns the float ulp.

static float ulp(float number); //Return Type is Float

// In order to use in program: 
Math.ulp(float number);

The following function will accept positive or negative double value as an argument and returns the ulp of type double.

static double ulp(double number); //Return Type is Double

// In order to use in program: 
Math.ulp(double number);

Java ulp Function Example

In this program, We use the Math.ulp function to find the ulp on both positive and negative values and display the output

//Java Math.ulp Function example
package MathFunctions;

public class UlpMethod {
	public static void main(String[] args) {
		double a = 2.352, b = -6.953;
		float c = 65.14f, d = -16.590f;	

		System.out.println("\nMath.ulp Function result of Zero: " + Math.ulp(0));
		
		System.out.println("\nMath.ulp Function of result Positive Double Value: " + Math.ulp(a));
		System.out.println("Math.ulp Function of result Negative Double Value: " + Math.ulp(b));
		
		System.out.println("\nMath.ulp Function of result Positive Float Value: " + Math.ulp(c));
		System.out.println("Math.ulp Function of result Negative Float Value: " + Math.ulp(d));
		
		double e = Math.ulp(100.90 + 165.10 - 180.50 + 6.50);
		System.out.println("\nMath.ulp Function results = " + e);
	}
}

OUTPUT

Java ulp Function 1

ANALYSIS

First, We used the Zero as the argument of Math.ulp Function.

System.out.println("\nMath.ulp Function result of Zero: " + Math.ulp(0));

Next, We used the Java Math.ulp Function on variables a and b (they belong to double type). The following statements will call the ulp method of double type ( static double ulp(double number) ) to display the result.

System.out.println("\nMath.ulp Function of result Positive Double Value: " + Math.ulp(a));
System.out.println("Math.ulp Function of result Negative Double Value: " + Math.ulp(b));

Then, We used the scalb Math function on variable c and d (they belong to float type). The following statements will call the ulp method of float type ( static float ulp(float number) ) to display the result.

System.out.println("\nMath.ulp Function of result Positive Float Value: " + Math.ulp(c));
System.out.println("Math.ulp Function of result Negative Float Value: " + Math.ulp(d));

Lastly, We declared a variable of type Double and performed the Math.ulp function directly on expression.

double e = Math.ulp(100.90 + 165.10 - 180.50 + 6.50);
System.out.println("\nMath.ulp Function results = " + e);

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy