How to resolve the algorithm Approximate equality step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Approximate equality step by step in the Scala programming language

Table of Contents

Problem Statement

Sometimes, when testing whether the solution to a task (for example, here on Rosetta Code) is correct, the difference in floating point calculations between different language implementations becomes significant. For example, a difference between 32 bit and 64 bit floating point calculations may appear by about the 8th significant digit in base 10 arithmetic.

Create a function which returns true if two floating point numbers are approximately equal.

The function should allow for differences in the magnitude of numbers, so that, for example, 100000000000000.01   may be approximately equal to   100000000000000.011, even though   100.01   is not approximately equal to   100.011. If the language has such a feature in its standard library, this may be used instead of a custom function. Show the function results with comparisons on the following pairs of values:

Answers should be true for the first example and false in the second, so that just rounding the numbers to a fixed number of decimals should not be enough. Otherwise answers may vary and still be correct. See the Python code for one type of solution.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Approximate equality step by step in the Scala programming language

Source code in the scala programming language

object Approximate extends App {
  val (ok, notOk, ε) = ("👌", "❌", 1e-18d)

  private def approxEquals(value: Double, other: Double, epsilon: Double) =
    scala.math.abs(value - other) < epsilon

  private def test(a: BigDecimal, b: BigDecimal, expected: Boolean): Unit = {
    val result = approxEquals(a.toDouble, b.toDouble, ε)
    println(f"$a%40.24f ≅ $b%40.24f => $result%5s ${if (expected == result) ok else notOk}")
  }

  test(BigDecimal("100000000000000.010"), BigDecimal("100000000000000.011"), true)
  test(BigDecimal("100.01"), BigDecimal("100.011"), false)
  test(BigDecimal(10000000000000.001 / 10000.0), BigDecimal("1000000000.0000001000"), false)
  test(BigDecimal("0.001"), BigDecimal("0.0010000001"), false)
  test(BigDecimal("0.000000000000000000000101"), BigDecimal(0), true)
  test(BigDecimal(math.sqrt(2) * math.sqrt(2d)), BigDecimal(2.0), false)
  test(BigDecimal(-Math.sqrt(2) * Math.sqrt(2)), BigDecimal(-2.0), false)
  test(BigDecimal("3.14159265358979323846"), BigDecimal("3.14159265358979324"), true)
}


  

You may also check:How to resolve the algorithm String interpolation (included) step by step in the REXX programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the zkl programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the ColdFusion programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Playing cards step by step in the JavaScript programming language