Функция abs
Функция вычисляет абсолютную величину (модуль) значения, передаваемого в качестве аргумента через параметр n.
В C++, эта функция перегружена в заголовочном файле <cmath> для типов данных с плавающей точкой (см. функция abs в cmath ), в заголовочном файле <complex> — для комплексных чисел (см. функция abs для комплексных чисел), а в <valarray> для массивов целых чисел (см. функция abs в valarray ).
В языке программирования Си существует функция для вычисления модуля только целых чисел.
abs() Function in C
In the C programming language, the use of the function abs in C is to return the absolute value of an integer. By integer, it means that the abs() is an arithmetics function. The stdlib.h library contains predefined function abs() for computing the absolute value of integers.
By absolute value, it means the function returns the positive value of an integer. Let's say we have an int value -3 , and we want to know what the absolute value of -3 is. So we will use the absolute function and return the absolute value of -3 , which is 3 .
Let's have a look at the syntax of abs() function :
Syntax of abs() in C
In the C programming language, abs() has the following syntax
Parameters of abs() in C
The parameter or argument of abs() function is an integral value. In the above syntax, it is x .
Return Value of abs() in C
Whatever integer value we pass into the absolute function abs() , the absolute value of that integer is the return value of abs() in C. In the above, the return value of abs() is the absolute value of x .
Example
A single and short code example to get the absolute value of a number using the abs() function.
Program:
Output:
What is abs in C?
The abs function in the C programming language stands for "Absolute Value". In other words, it's the distance between a number and a number line beginning at 0 , without taking the direction into account. A number will always have a positive abs value or absolute value, meaning that a distance will never be negative.
Что такое abs в c
In C#, Abs() is a Math class method which is used to return the absolute value of a specified number. This method can be overload by passing the different type of parameters to it.
Math.Abs(Decimal)
This method is used to return the absolute value of a Decimal number.
Syntax:
Parameter:
val: It is the required number which is greater than or equal to Decimal.MinValue, but less than or equal to Decimal.MaxValue of type System.Decimal.
Return Type: It returns a decimal number say r, such that 0 ≤ r ≤ Decimal.MaxValue.
Example:
Output:
Math.Abs(Double)
This method is used to return the absolute value of a double-precision floating-point number.
Syntax:
Parameter:
val: It is the required number which is greater than or equal to Double.MinValue, but less than or equal to Double.MaxValue of type System.Double.
Return Type: It returns a double-precision floating-point number say r, such that 0 ≤ r ≤ Double.MaxValue.
Note:
- If val is equal to NegativeInfinity or PositiveInfinity, the return value will be PositiveInfinity.
- If the val is equal to NaN then return value will be NaN.
Example:
Output:
Math.Abs(Int16)
This method is used to return the absolute value of a 16-bit signed integer.
Syntax:
Parameter:
val: It is the required number which is greater than Int16.MinValue, but less than or equal to Int16.MaxValue of type System.Int16.
Return Type: It returns 16-bit signed integer say r, such that 0 ≤ r ≤ Int16.MaxValue.
Exception: This method will give OverflowException if the value of val is equals to Int16.MinValue.
Abs – абсолютное значение целого числа
Функция abs() возвращает модуль (неотрицательное, целое число) числа x.
При подключении файла stdlib.h функция abs() будет определяться макрокомандой, которую можно расширять до встроенного кода.
Если необходимо функцию abs() использовать вместо макрокоманды, то следует после #include <stdlib.h> добавить #undef abs().
При подключении файла math.h функция abs(), при передаче аргумента, возвращает абсолютное значение либо целого числа, либо числа с плавающей запятой.
Функция abs() принимает только один аргумент.
В C++ функция abs() перегружена в прототипах <cstdlib>, <complex> (комплексные числа), <valarray> (массив целых чисел), <cmath> (числа с плавающей точкой).
Синтаксис
Прототип
| Язык программирования | Заголовочный файл |
|---|---|
| С++ | stdlib.h math.h complex.h |
| C | cmath |
Возвращаемое значение
Функция abs() для целочисленных и вещественных чисел возвращает целое значение от 0 до 32767. Если передаваемый аргумент будет отрицательным и равным -32768, то он будет возвращен, как -32768.
Функция abs() для комплексных чисел возвращает значение типа double.
Переносимость
Функция abs() доступна в системе UNIX и определена в стандарте ANSI C.
Функция abs() для комплексных чисел требует язык программирования С++.