BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.nextDown() Method

By Chaitanya Singh | Filed Under: java

Java Math.nextDown() method returns floating point number adjacent to the passed argument, in the direction of negative infinity.

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = 12345;
    float f = 8.88f;
    System.out.println(Math.nextDown(d));
    System.out.println(Math.nextDown(f));
  }
}

Output:

12344.999999999998
8.879999

Syntax of Math.nextDown() method

Math.nextDown(12.25f); //returns 12.249999

nextDown() Description

public static double nextDown(double d): Returns floating point value adjacent to d, towards negative infinity. This is equivalent to nextAfter(d, Double.NEGATIVE_INFINITY). However it faster compared to nextAfter() method.

public static float nextDown(float f): Returns floating point value adjacent to passed float argument f, towards negative infinity. This is equivalent to nextAfter(d, Float.NEGATIVE_INFINITY). However it faster compared to nextAfter() method.

nextDown() Parameters

  • d: Double value whose adjacent value to be determined.
  • f: Float value whose adjacent value to be determined.

nextDown() Return Value

  • Adjacent floating value towards negative infinity.
  • If the argument is NaN (Not a number) then it returns NaN.
  • If the argument is negative infinity, then it returns negative infinity.
  • If the passed argument is zero, it returns -Double.MIN_VALUE for double type argument or -Float.MIN_VALUE for float type argument.

Example 1

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = 500;
    float f = 6.51f;
    System.out.println(Math.nextDown(d)); //towards negative infinity
    System.out.println(Math.nextDown(f)); //towards negative infinity
  }
}

Output:

Java Math.nextDown() Example Output_1

Example 2

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = 0.0/0; //NaN
    System.out.println(Math.nextDown(d));
  }
}

Output:

Java Math.nextDown() Example Output_2

Example 3

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = 0; // zero double value
    float f = 0.0f; //zero float value

    //This will return -Double.MIN_VALUE
    System.out.println(Math.nextDown(d));

    //This will return -Float.MIN_VALUE
    System.out.println(Math.nextDown(f));
  }
}

Output:

Java Math.nextDown() Example Output_3

Example 4

public class JavaExample
{
  public static void main(String[] args)
  {
    double d = Double.NEGATIVE_INFINITY;
    System.out.println(Math.nextDown(d));
  }
}

Output:

Java Math.nextDown() Example Output_4

Recommended Posts

  • Java Math.nextUp()
  • Java Math.nextAfter()
  • Java Math.copySign()
  • Java Math.ceil()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap