How to resolve the algorithm Extreme floating point values step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Extreme floating point values step by step in the Scala programming language

Table of Contents

Problem Statement

The IEEE floating point specification defines certain 'extreme' floating point values such as minus zero, -0.0, a value distinct from plus zero; not a number, NaN; and plus and minus infinity. The task is to use expressions involving other 'normal' floating point values in your language to calculate these, (and maybe other), extreme floating point values in your language and assign them to variables. Print the values of these variables if possible; and show some arithmetic with these values and variables. If your language can directly enter these extreme floating point values then show it.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Extreme floating point values step by step in the Scala programming language

Source code in the scala programming language

object ExtremeFloatingPoint extends App {
  val negInf = -1.0 / 0.0 //also Double.NegativeInfinity
  val inf = 1.0 / 0.0 //  //also Double.PositiveInfinity
  val nan = 0.0 / 0.0 //  //also Double.NaN
  val negZero = -2.0 / inf

  println("Value:         Result:      Infinity? Whole?")
  println(f"Negative inf: ${negInf}%9s ${negInf.isInfinity}%9s ${negInf.isWhole}%9s")
  println(f"Positive inf: ${inf}%9s ${inf.isInfinity}%9s ${inf.isWhole}%9s")
  println(f"NaN:          ${nan}%9s ${nan.isInfinity}%9s ${nan.isWhole}%9s")
  println(f"Negative 0:   ${negZero}%9s ${negZero.isInfinity}%9s ${negZero.isWhole}%9s")
  println(f"inf + -inf:   ${inf + negInf}%9s ${(inf + negInf).isInfinity}%9s ${(inf + negInf).isWhole}%9s")
  println(f"0 * NaN:      ${0 * nan}%9s ${(inf + negInf).isInfinity}%9s ${(inf + negInf).isWhole}%9s")
  println(f"NaN == NaN:   ${nan == nan}%9s")
}


  

You may also check:How to resolve the algorithm Pseudo-random numbers/Splitmix64 step by step in the C programming language
You may also check:How to resolve the algorithm Environment variables step by step in the Raku programming language
You may also check:How to resolve the algorithm Determine if a string is squeezable step by step in the Julia programming language
You may also check:How to resolve the algorithm Collections step by step in the Lisaac programming language
You may also check:How to resolve the algorithm Partition function P step by step in the J programming language