Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

GoLang – Find Absolute Value – Abs() Function – Examples & Explanation

Abs() is a function from Go language. This Abs() function returns the absolute (positive) value of any given number (positive or negative). If input value (x – parameter/argument) is negative, the Abs() will return a positive number. If the input value is already a positive value that will remain unchanged. If the function input value is a complex number (a + bi) then Abs() will convert its magnitude into a floating point value.

Abs()Syntax in Go Language


Note: float64 is a data type in Go language which has IEEE-754 64-bit floating-point numbers.

Some Special Cases:
Abs(±Inf) = +Inf (Here inf means infinity)
Abs(NaN) = NaN (NaN means not a number)

Abs() Parameter

x – where x is any number or numeric expression in go language i-e integer, float or complex.

 

Abs() Return Value

Abs() will return a positive or absolute value of a given input. The return value will depend on the input parameters of the function.
If an input parameter is an integer, then return value is an integer.
If an input parameter is a float or a complex then func Abs() returns type is a floating point number.

 

Abs() Examples in Go

 

An integer example of Abs() in Go Language

An integer number is a simple whole number that can be positive or negative or zero like 4, -67, 523, -8888 etc.
Example

Return value: 33

 

A floating point number example of Abs() in Go Language

A floating point is a number which has a decimal point like 33.54, -689.56, -69.36 etc.
Example

Return value: 18.62

 

A complex number example of Abs() in Go Language

A complex number is a number that can be expressed in the form of (a + bi), where a and b are real numbers and i is the imaginary unit. In complex numbers func Abs() will return a floating representation of the complex number. some of the examples of a complex number are (-28+6i), (9-300i). For complex numbers we have to import "math/cmplx" liberary and then use the Abs() function in the form of cmplx.Abs().
Example

Output:
94.19129471453293

LearnAndLearn.com © 2018 - All rights reserved