What is abs() in D?
The abs() function in D returns the absolute value of a number.
The figure below shows the mathematical representation of the abs() function.
Note:
import std.mathis required to use this function.
Syntax
abs(num)
numis the number whose absolute value is required
Parameter
The abs() function takes a number as a parameter.
Return value
The abs() function returns the absolute value of a number passed as a parameter.
Code
The following code shows how to use the abs() function in D:
import core.stdc.stdio;import std.stdio;//Header required for the functionimport std.math;int main(){//Positive numberwriteln ("The value of abs(10) : ",abs(10));//Zerowriteln ("The value of abs(0) : ",abs(0));//Negative numberwriteln ("The value of abs(-10) : ",abs(-10));return 0;}
Explanation
-
Line 4: We add the
std.mathheader required for theabs()function. -
Line 9: We calculate the absolute value of the positive number
10usingabs(). -
Line 12: We calculate the absolute value
0usingabs(). -
Line 15: We calculate the absolute value of the negative number
-10usingabs().