从FloatingLiteral/AFPloat获取原始解析的数字
Get the original parsed number from FloatingLiteral/APFloat
我开始使用clang作为我目前正在开发的有界模型检查器的前端,我需要一些从AST获得的浮点数的帮助。
例如,对于这个程序:
int main()
{
float a = 1.0f;
long double b = 5567.765434376l;
}
生成的AST为:
FunctionDecl 0x1dea160 </home/mramalho/main.c:3:1, line:7:1> line:3:5 main 'int ()'
`-CompoundStmt 0x1dea368 <line:4:1, line:7:1>
|-DeclStmt 0x1dea298 <line:5:3, col:17>
| `-VarDecl 0x1dea218 <col:3, col:13> col:9 a 'float' cinit
| `-FloatingLiteral 0x1dea278 <col:13> 'float' 1.000000e+00
`-DeclStmt 0x1dea350 <line:6:3, col:34>
`-VarDecl 0x1dea2c0 <col:3, col:19> col:15 b 'long double' cinit
`-FloatingLiteral 0x1dea320 <col:19> 'long double' 5.567765e+03
我从.getValue()中得到的APFLoat数字没有多大帮助,因为它被存储为已经转换的数字。
特别是,我想知道是否可以获得原始数字(在上面的例子中,1.0f和5567.765434376l)。如果可能的话,在适用的情况下使用(+/-)Inf和/或NaN的字符串会很好,但不是必需的。
我尝试了几种方法,但最终总是得到原始数字的修剪版本。
我最好的选择是方法convertFromString和结构DecimalInfo(都在APFloat上),但我找不到方法来访问它们,因为在我获得FloatingLiteral时,数字已经创建好了。
谢谢。
不知道是否可以回答自己的问题,但以下是我解决问题的方法:
llvm::SmallVector<char, 32> string;
val.toString(string, 32, 0);
其中val是APFloat。来自文件:
/// Converts this value into a decimal string.
///
/// param FormatPrecision The maximum number of digits of
/// precision to output. If there are fewer digits available,
/// zero padding will not be used unless the value is
/// integral and small enough to be expressed in
/// FormatPrecision digits. 0 means to use the natural
/// precision of the number.
/// param FormatMaxPadding The maximum number of zeros to
/// consider inserting before falling back to scientific
/// notation. 0 means to always use scientific notation.
///
/// Number Precision MaxPadding Result
/// ------ --------- ---------- ------
/// 1.01E+4 5 2 10100
/// 1.01E+4 4 2 1.01E+4
/// 1.01E+4 5 1 1.01E+4
/// 1.01E-2 5 2 0.0101
/// 1.01E-2 4 2 0.0101
/// 1.01E-2 4 1 1.01E-2
然后它是一个简单的迭代来获得值。
它还为无穷大的值提供"Inf"字符串。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 将浮动的heightmap数组导出为16位原始值
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 从C 到C#调用INT []显示随机大数字,而不是原始数组成员
- MaxHeaify调用产生的数字不在原始数组中
- 从FloatingLiteral/AFPloat获取原始解析的数字
- 当对任何数字进行移位操作时,如何返回原始数字