定位因整数除法导致的数值错误
是否有 g++ 警告或其他工具可以识别整数除法(截断为零)?我有数千行代码进行计算,这些计算不可避免地会出现数值错误,通常是由于需要定位“float = int/int”而导致的。我需要一个合理的方法来找到这些。
Is there a g++ warning or other tool that can identify integer division (truncation toward zero)? I have thousands of lines of code with calculations that inevitably will have numerical errors typically due to "float = int/int" that need to be located. I need a reasonable method for finding these.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试
-Wconversion
。来自 gcc 的手册页:
对于以下示例程序 (
test.cpp
),我收到错误test.cpp: In function 'int main()':
。test.cpp:7:警告:从“int”转换为“float”可能会改变其值
Try
-Wconversion
.From gcc's man page:
For the following sample program (
test.cpp
), I get the errortest.cpp: In function ‘int main()’:
.test.cpp:7: warning: conversion to ‘float’ from ‘int’ may alter its value
我很难指出这些数字错误。您要求进行整数计算,并获得了整数计算的正确数字。如果这些数字不可接受,则要求进行浮点计算:
I have a hard time calling these numerical errors. You asked for integer calculations, and got the correct numbers for integer calculations. If those numbers aren't acceptable, then ask for floating point calculations:
关于
-Wconversion
<的评论gcc 的 /a>:更改浮点变量的类型
float
到double
使警告消失:使用
$ g++-4.7 -Wconversion 'file.cpp'
编译不会返回任何警告(如$ clang++ -Weverything 'file.cpp'
)。说明:
使用
float
类型时的警告不会因为完全有效的整数运算而返回,而是因为float
无法存储int
的所有可能值> (较大的不能用float
捕获,但可以用double
捕获)。因此,在 float 情况下将 RHS 分配给 f 时,值可能会发生变化,但在 double 情况下则不会。明确一下:返回警告不是因为int/int
,而是因为赋值float = int
。为此,请参阅以下问题:java中当大小相同时float和integer数据类型有什么区别, 将整数存储为浮点数 和 用于 int 的舍入 ->浮动-> int 往返转换
但是,当使用
float
时,-Wconversion
仍可用于识别可能受影响的行,但并不全面,实际上并不适用于那。对于-Wconversion
的目的,请参阅 docs/gcc/Warning-Options.html 和此处 gcc.gnu.org/wiki/NewWconversion可能感兴趣的还有以下讨论'在 C++ 中隐式转换整数计算以进行浮点数'
Remark on
-Wconversion
of gcc:Changing the type of the floating point variable from
float
todouble
makes the warning vanish:Compiling with
$ g++-4.7 -Wconversion 'file.cpp'
returns no warnings (as$ clang++ -Weverything 'file.cpp'
).Explanation:
The warning when using the type
float
is not returned because of the totally valid integer arithmetics, but becausefloat
cannot store all possible values ofint
(larger ones cannot be captured byfloat
but bydouble
). So there might be a change of value when assigning RHS tof
in the case of float but not in the case of double. To make it clear: The warning is not returned because ofint/int
but because of the assignmentfloat = int
.For this see following questions: what the difference between the float and integer data type when the size is same in java, Storing ints as floats and Rounding to use for int -> float -> int round trip conversion
However, when using
float
-Wconversion
could still be useful to identify possible lines which are affected but is not comprehensive and is actually not intended for that. For the purpose of-Wconversion
see docs/gcc/Warning-Options.html and here gcc.gnu.org/wiki/NewWconversionPossibly of interest is also following discussion 'Implicit casting Integer calculation to float in C++'
此页面包含有关 g++ 警告的信息。如果您已经尝试过
-Wall
那么唯一剩下的可能就是此链接中的警告。再看一下-Wconversion
可能会成功。注意:完全编辑了响应。
This page contains info about g++ warnings. If you've already tried
-Wall
then the only thing left could be the warnings in this link. On second look-Wconversion
might do the trick.Note: Completely edited the response.
发现此类错误的最佳方法是进行真正良好的单元测试。所有替代方案都不够好。
The best way to find such error is to have really good unit tests. All alternatives are not good enough.
看看这个 clang-tidy 检测。
它捕获这样的情况:
Have a look at this clang-tidy detection.
It catches cases like this: