A função abs () em C ++ retorna o valor absoluto do argumento.
A função abs é idêntica a fabs () em C ++.
A função é definida no arquivo de cabeçalho.
(Matemática) | x | = abs (x) (Programação C ++)
protótipo abs () (a partir do padrão C ++ 11)
abs duplo (duplo x); float abs (float x); abs duplo longo (x duplo longo); abs duplo (T x); // Para tipo integral
A função abs () recebe um único argumento e retorna um valor do tipo double
, float
ou long double
de tipo.
abs () Parâmetros
A função abs () recebe um único argumento, x cujo valor absoluto é retornado.
abs () Valor de retorno
A função abs () retorna o valor absoluto de x, ou seja, | x |.
Exemplo 1: Como a função abs () funciona em C ++?
#include #include using namespace std; int main() ( double x = -87.91, result; result = abs(x); cout << "abs(" << x << ") = |" << x << "| = " << result << endl; return 0; )
Quando você executa o programa, a saída será:
abs (-87,91) = | -87,91 | = 87,91
Exemplo 2: função abs () para tipos integrais
#include #include using namespace std; int main() ( long int x = -101; double result; result = abs(x); cout << "abs(" << x << ") = |" << x << "| = " << result << endl; return 0; )
Quando você executa o programa, a saída será:
abs (-101) = | -101 | = 101