| 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 字母顺序排列的目录 | | 网友留言/技术支持 |
|
| _atold, _ttold, _wtold - 字符串转 IEEE 长双精度浮点数 (long double) |
_atold, _ttold, _wtold:字符串转 IEEE 长双精度浮点数 (long double)
函数原型:
| long double _atold(const char *s); |
| long double _wtold(const wchar_t *s); |
头文件:
#include <cstdlib>
命名空间:
std
参数:
s:浮点数字符串,格式:"[空白字符][符号][数字][.][数字][e|E[符号]数字]",支持 "+INF"、"-INF"、"+NAN" 和 "-NAN"
返回值:
长双精度浮点数,long double 类型
如果参数 s 包含不可识别的字符,会终止于第一个不可识别的字符,返回前面可识别部分转换为浮点数的值 (如果第一个字符不可识别返回值为 0),不产生异常,不调用 _matherr。
如果溢出了,返回值为 ± _LHUGE_VAL,全局变量 errno 为 ERANGE,不产生异常,不调用 _matherrl。
| 字符串 |
转换结果 |
说明 |
| "1.2345" |
1.2345 |
普通的小数 |
| "1.23e45" |
1.23e+45 |
科学计数法:1.23×1045 |
| "1.23x45" |
1.23 |
包含不可识别的字符 'x',转换结果为 'x' 之前的内容:1.23 |
| "-1.23e-45" |
-1.23e-45 |
科学计数法:-1.23×10-45 |
| "1.23e4567" |
1.23e+4567 |
对于 32 位程序,1.23×104567 没有超出 long double 的范围,得到正确的结果 |
| "1.23e9876" |
1.18973e+4932 |
1.23×109876 超出了 long double 的范围,按照 C++ Builder 自带的帮助和 MSDN 里面的说明,返回值应该为 _LHUGE_VAL 即 +INF,但是实际测试结果为 1.18973e+4932,是 80 位长双精度浮点数 long double 的最大值 (点此处查看浮点数的取值范围) |
| "123" |
123 |
整数值 |
| "abc" |
0 |
不可识别的字符串,返回值为 0 |
| "+INF" |
+INF |
正无穷大:+INF |
| "-INF" |
-INF |
负无穷大:-INF |
| "+NAN" |
+NAN |
正不是数:+NAN |
| "-NAN" |
-NAN |
负不是数:-NAN |
注:
经过测试,bcc32 和 clang32 在溢出是不等于 _LHUGE_VAL,而是等于 long double 的最大值;而 clang64 在溢出的时候返回值不等于 _LHUGE_VAL,是一个错误的值,例如下面的例子的 _ttold(_T("1.23e4567"));并且不识别 "+INF"、"-INF"、"+NAN" 和 "-NAN"。无论是 C++ Builder 自带的帮助,还是微软的 MSDN 里面,对溢出和无穷大、NAN 的返回值的描述都和本文的 “返回值” 描述一致,所以认为 C++ Builder 的 clang64 编译器对于这一组函数存在兼容性问题 (测试的 C++ Builder 版本为 10.1 Berlin 和 10.2 Tokyo)。
例子:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString s;
Memo1->Lines->Add(s.sprintf(L"_wtold(L\"1.2345\") = %llg", _wtold(L"1.2345") ));
Memo1->Lines->Add(s.sprintf(L"_wtold(L\"1.23e45\") = %llg", _wtold(L"1.23e45") ));
Memo1->Lines->Add(s.sprintf(L"_wtold(L\"1.23x45\") = %llg", _wtold(L"1.23x45") ));
Memo1->Lines->Add(s.sprintf(L"_ttold(_T(\"-1.23e-45\")) = %llg", _ttold(_T("-1.23e-45")) ));
Memo1->Lines->Add(s.sprintf(L"_ttold(_T(\"1.23e4567\")) = %llg", _ttold(_T("1.23e4567")) ));
Memo1->Lines->Add(s.sprintf(L"_ttold(_T(\"1.23e9876\")) = %llg", _ttold(_T("1.23e9876")) ));
Memo1->Lines->Add(s.sprintf(L"_atold(\"123\") = %llg", _atold("123") ));
Memo1->Lines->Add(s.sprintf(L"_atold(\"abc\") = %llg", _atold("abc") ));
Memo1->Lines->Add(s.sprintf(L"_wtold(L\"+INF\") = %llg", _wtold(L"+INF") ));
Memo1->Lines->Add(s.sprintf(L"_wtold(L\"-INF\") = %llg", _wtold(L"-INF") ));
Memo1->Lines->Add(s.sprintf(L"_wtold(L\"+NAN\") = %llg", _wtold(L"+NAN") ));
Memo1->Lines->Add(s.sprintf(L"_wtold(L\"-NAN\") = %llg", _wtold(L"-NAN") ));
Memo1->Lines->Add(s.sprintf(L"_LHUGE_VAL = %llg" , _LHUGE_VAL ));
} |

兼容性:
| 函数 \ C++ Builder 编译器 |
bcc32 |
clang32 |
clang64 |
| _atold |
√ |
√ |
√ 【注】 |
| _ttold |
√ |
√ |
√ 【注】 |
| _wtold |
√ |
√ |
√ 【注】 |
相关链接:
• atof • _atold • strtod • strtof • strtold • ecvt • fcvt • gcvt
• StrToFloat • StrToFloatDef • FloatToStr • FloatToStrF • FormatFloat
• _matherr • 浮点数异常处理
|
|
|
|