Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What is nextAfter() in Scala?

Abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The nextAfter() method is a static method in the math package that returns the floating-point number adjacent to the first argument towards the direction of the second argument. It returns the second argument in case both the arguments are equal.

Scala internally uses Java implementations of the corresponding methods. This method internally invokes nextAfter() method in Java.

Syntax

math.nextAfter(start, direction)

Parameters

  • start: The starting floating point is a Float or Double.
  • direction: It’s a value indicating which of the start’s neighbors or start should be returned. The direction can be of Double datatype.

Return value

The method returns a floating-point number adjacent to start in the direction of direction.

Example

import scala.math
object Main extends App {
val start: Double = 6.78
val direc: Double = 1
println(s"start - ${start}, direction - ${direc}")
val newNum = Math.nextAfter(start, direc)
println(s"Number next after ${start} towards ${direc} - ${newNum}")
}
nextAfter() in scala

Explanation

  • Line 1: We import the math package.
  • Line 4: The starting point start is defined.
  • Line 5: The direction direc is defined.
  • Line 6: Print the start and direc.
  • Line 7: The following value after start in the direction of direc is obtained by invoking the nextAfter() method. The value returned is stored in newNum.
  • Line 8: Print thenewNum.

RELATED TAGS

scala

CONTRIBUTOR

Abhilash
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring