Skip to content

Latest commit

 

History

History
120 lines (93 loc) · 4.47 KB

abs-labs-llabs-abs64.md

File metadata and controls

120 lines (93 loc) · 4.47 KB
titledescriptionms.dateapi_nameapi_locationapi_typetopic_typef1_keywordshelpviewer_keywords
abs, labs, llabs, _abs64
API reference for abs, labs, llabs, and _abs64; which calculates the absolute value of a value.
04/05/2018
abs
_abs64
labs
llabs
msvcrt.dll
msvcr80.dll
msvcr90.dll
msvcr100.dll
msvcr100_clr0400.dll
msvcr110.dll
msvcr110_clr0400.dll
msvcr120.dll
msvcr120_clr0400.dll
ucrtbase.dll
api-ms-win-crt-utility-l1-1-0.dll
DLLExport
apiref
_abs64
STDLIB/_abs64
abs
CORECRT_MATH/abs
STDLIB/abs
CSTDLIB/abs
labs
CORECRT_MATH/labs
llabs
CORECRT_MATH/llabs
absolute values
abs function
abs64 function
_abs64 function
calculating absolute values

abs, labs, llabs, _abs64

Calculates the absolute value of the argument.

Syntax

intabs( intn ); longlabs( longn ); long longllabs( long longn ); __int64_abs64( __int64n );
longabs( long n ); // C++ onlylonglongabs( longlong n ); // C++ onlydoubleabs( double n ); // C++ onlylongdoubleabs( longdouble n ); // C++ onlyfloatabs( float n ); // C++ only

Parameters

n
Numeric value.

Return value

The abs, labs, llabs, and _abs64 functions return the absolute value of the parameter n. There's no error return.

Remarks

Because C++ allows overloading, you can call overloads of abs that take and return long, long long, float, double, and long double values. These overloads are defined in the <cmath> header. In a C program, abs always takes and returns an int.

Microsoft-specific: The range of negative integers representable in any integral type is larger than the range of positive integers representable in that type. So, it's possible to supply an argument to these functions that can't be converted. If the absolute value of the argument can't be represented by the return type, the abs functions return the argument value unchanged. Specifically, abs(INT_MIN) returns INT_MIN, labs(LONG_MIN) returns LONG_MIN, llabs(LLONG_MIN) returns LLONG_MIN, and _abs64(_I64_MIN) returns _I64_MIN. Effectively, the abs functions can't be used to guarantee a positive value.

Requirements

RoutineRequired C headerRequired C++ header
abs, labs, llabs<math.h> or <stdlib.h><cmath>, <cstdlib>, <stdlib.h> or <math.h>
_abs64<stdlib.h><cstdlib> or <stdlib.h>

To use the overloaded versions of abs in C++, you must include the <cmath> header.

Example

This program computes and displays the absolute values of several numbers.

// crt_abs.c// Build: cl /W3 /TC crt_abs.c// This program demonstrates the use of the abs function// by computing and displaying the absolute values of// several numbers.#include<stdio.h>#include<math.h>#include<stdlib.h>#include<limits.h>intmain( void ) { intix=-4; longlx=-41567L; long longllx=-9876543210LL; __int64wx=-1; // absolute 32 bit integer valueprintf_s("The absolute value of %d is %d\n", ix, abs(ix)); // absolute long integer valueprintf_s("The absolute value of %ld is %ld\n", lx, labs(lx)); // absolute long long integer valueprintf_s("The absolute value of %lld is %lld\n", llx, llabs(llx)); // absolute 64 bit integer valueprintf_s("The absolute value of 0x%.16I64x is 0x%.16I64x\n", wx, _abs64(wx)); // Integer error cases:printf_s("Microsoft implementation-specific results:\n"); printf_s(" abs(INT_MIN) returns %d\n", abs(INT_MIN)); printf_s(" labs(LONG_MIN) returns %ld\n", labs(LONG_MIN)); printf_s(" llabs(LLONG_MIN) returns %lld\n", llabs(LLONG_MIN)); printf_s(" _abs64(_I64_MIN) returns 0x%.16I64x\n", _abs64(_I64_MIN)); }
The absolute value of -4 is 4 The absolute value of -41567 is 41567 The absolute value of -9876543210 is 9876543210 The absolute value of 0xffffffffffffffff is 0x0000000000000001 Microsoft implementation-specific results: abs(INT_MIN) returns -2147483648 labs(LONG_MIN) returns -2147483648 llabs(LLONG_MIN) returns -9223372036854775808 _abs64(_I64_MIN) returns 0x8000000000000000 

See also

Data conversion
Math and floating-point support
_cabs
fabs, fabsf, fabsl
imaxabs

close