abs Algorithm

The abs Algorithm, which stands for "absolute value", is a mathematical function that returns the non-negative value of a given number. It is a widely-used algorithm in various fields such as mathematics, science, and engineering for determining the magnitude or size of a number without considering its sign. The function calculates the absolute value by eliminating the negative sign, if present, from a given number. In essence, the abs Algorithm measures the distance of a number from zero, disregarding its direction. For example, the absolute value of both -5 and 5 is 5, since both numbers are 5 units away from zero. This algorithm has several practical applications, including calculating distances, determining the difference between two values, and comparing magnitudes. In programming languages, the abs Algorithm is often provided as a built-in function, allowing users to easily compute the absolute value of an integer or floating-point number. By providing a consistent and reliable method for computing non-negative values, the abs Algorithm plays a crucial role in solving various mathematical and real-world problems.
/*
    author: PatOnTheBack
    license: GPL-3.0 or later

    Modified from:
        https://github.com/TheAlgorithms/Python/blob/master/maths/abs.py

    This script will find the absolute value of a number.

    More about absolute values:
        https://en.wikipedia.org/wiki/Absolute_value
*/

function absVal (num) {
  // Find absolute value of `num`.
  'use strict'
  if (num < 0) {
    return -num
  }
  // Executes if condition is not met.
  return num
}

// Run `abs` function to find absolute value of two numbers.
console.log('The absolute value of -34 is ' + absVal(-34))
console.log('The absolute value of 34 is ' + absVal(34))

LANGUAGE:

DARK MODE: