| C++ Builder 串口控件 | | C++ Builder 编程技巧 | | C++ Builder 操作指南 | | C++ Builder 参考手册 | | 基础知识 | | cfloat 浮点数 | | cmath 数学函数 | | cstdlib 标准库函数 | | • atof, _ttof, _wtof | | • _atold, _ttold, _wtold | | • atoi, _ttoi, _wtoi | | • atol, _ttol, _wtol | | • _atoi64, _ttoi64, _wtoi64 | | • strtod, _tcstod, wcstod | | • strtof, _tcstof, wcstof | | • strtold, _strtold, _tcstold, wcstold, _wcstold | | • strtol, _tcstol, wcstol | | • strtoll, _tcstoll, wcstoll | | • strtoul, _tcstoul, wcstoul | | • strtoull, _tcstoull, wcstoull | | • itoa, _itot, _itow | | • ltoa, _ltoa, _ltot, _ltow | | • ultoa, _ultot, _ultow | | • _i64toa, _i64tot, _i64tow | | • _ui64toa, _ui64tot, _ui64tow | | • ecvt, _ecvt | | • fcvt, _fcvt | | • gcvt, _gcvt | | • abs, labs, llabs | | • div, ldiv, lldiv | | • div_t, ldiv_t, lldiv_t | | • _rotl, _crotl, _lrotl | | • _rotr, _crotr, _lrotr | | • rand, _lrand | | • srand | | • random | | • randomize | | • mbstowcs | | • mblen | | • mbtowc | | • mbtowc_cp | | • wcstombs | | • wctomb | | • wctomb_cp | | • swab, _swab | | • _fullpath, _tfullpath, _wfullpath | | • _makepath, _tmakepath, _wmakepath | | • _splitpath, _tsplitpath, _wsplitpath | | • getenv, _tgetenv, _wgetenv | | • putenv, _putenv, _tputenv, _wputenv | | • _searchenv, _tsearchenv, _wsearchenv | | • _searchstr, _tsearchstr, _wsearchstr | | • system, _tsystem, _wsystem | | • abort | | • atexit | | • atexit_t | | • exit, _exit | | • abort_handler_s | | • ignore_handler_s | | • constraint_handler_t | | • set_constraint_handler_s | | • _argc | | • _argv, _targv, _wargv; | | • _environ, _tenviron, _wenviron | | • EXIT_SUCCESS, EXIT_FAILURE | | • _MAX_PATH, _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, _MAX_EXT | | • MB_CUR_MAX | | • RAND_MAX, LRAND_MAX | | System 字符串 | | System 日期和时间 | | System.Math.hpp 数学函数 | | 其他数据类型 | | VCL 基础类 | | VCL 应用程序 | | Pictures 图片 | | Graphics 绘图 | | Additional 控件 | | System 控件 | | A ~ Z 字母顺序排列的目录 | | 网友留言/技术支持 |
|
| abs, labs, llabs - 整数取绝对值 |
abs, labs, llabs:整数取绝对值
函数原型:
| 函数原型 |
int abs(int x);
long abs(long x);
long long abs(long long x);
float abs (float x);
double abs(double x);
long double abs (long double x); |
| long labs(long x); |
| long long llabs(long long x); |
头文件:
#include <cstdlib>
#include <cmath>
命名空间:
std
参数:
x:整数或浮点数
• 浮点数定义域范围为 [-Infinity, +Infinity]
• int 和 long 型 [-2147483648, 2147483647]
• long long 型 [-9223372036854775808, 9223372036854775807]
返回值:
x 的绝对值,值域范围为 [0, +Infinity]
• 整数 int 和 long 型唯一一个能够出错的值就是如果 x 等于 -2147483648 取绝对值超出了 int 和 long 的范围,得到错误的结果,
但是结果给 unsigned int 或 unsigned long 赋值,能够得到正确的结果。
• 整数 long long 型唯一一个能够出错的值就是如果 x 等于 -9223372036854775808 取绝对值超出了 long long 的范围,得到错误的结果,
但是结果给 unsigned long long 赋值,能够得到正确的结果。
例子:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString s;
s.cat_sprintf(L"abs(12345) = %d\r\n" , abs(12345) );
s.cat_sprintf(L"abs(-12345) = %d\r\n" , abs(-12345) );
s.cat_sprintf(L"abs(123456789l) = %ld\r\n" , abs(123456789l) );
s.cat_sprintf(L"abs(-123456789l) = %ld\r\n" , abs(-123456789l) );
s.cat_sprintf(L"abs(1234567890123456789ll) = %lld\r\n", abs(1234567890123456789ll) );
s.cat_sprintf(L"abs(-1234567890123456789ll) = %lld\r\n", abs(-1234567890123456789ll));
s.cat_sprintf(L"abs(-1.23f) = %g\r\n" , abs(-1.23f) );
s.cat_sprintf(L"abs(1.23f) = %g\r\n" , abs(1.23f) );
s.cat_sprintf(L"abs(-123.456) = %lg\r\n" , abs(-123.456) );
s.cat_sprintf(L"abs(123.456) = %lg\r\n" , abs(123.456) );
s.cat_sprintf(L"abs(-1.23456e-300) = %lg\r\n" , abs(-1.23456e-300) );
s.cat_sprintf(L"abs(1.23456e-300) = %lg\r\n" , abs(1.23456e-300) );
s.cat_sprintf(L"abs(-1.23456e300) = %lg\r\n" , abs(-1.23456e300) );
s.cat_sprintf(L"abs(1.23456e300) = %lg\r\n" , abs(1.23456e300) );
s.cat_sprintf(L"abs(-∞) = %llg\r\n", abs(-Infinity) );
s.cat_sprintf(L"abs(+∞) = %llg\r\n", abs(Infinity) );
Memo1->Lines->Text = s;
} |

兼容性:
| 函数 \ C++ Builder 编译器 |
bcc32 |
clang32 |
clang64 |
| abs |
√ |
√ |
√ |
| labs |
√ |
√ |
√ |
| llabs |
√ |
√ |
√ |
相关链接:
• fabs • copysign • ceil • floor • trunc • round • RoundTo • _matherr • 浮点数异常处理
|
|
|
|