- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
前几天,我将我的 Windows 构建环境从 MSVC2013 升级到 MSVC2017,你瞧,我的程序中一个多年来一直运行良好(并且在 g++/clang 下仍然运行良好)的函数突然开始给出不正确的结果使用 MSVC2017 编译。
我能够重写函数以再次给出正确的结果,但这段经历让我感到好奇——我的函数是调用了未定义的行为(直到现在恰好给出了正确的结果),还是代码定义明确并且MSVC2017 有问题吗?
下面是一个简单的程序,显示了我重写前后的函数玩具版本。特别是,函数 maybe_invokes_undefined_behavior()(如下所示)在使用值为 -32762 的参数调用时是否会调用未定义的行为?
#include <stdio.h>
enum {ciFirstToken = -32768};
// This function sometimes gives unexpected results under MSVC2017
void maybe_invokes_undefined_behavior(short token)
{
if (token >= 0) return;
token -= ciFirstToken; // does this invoke undefined behavior if (token==-32762) and (ciFirstToken==-32768)?
if (token == 6)
{
printf("Token is 6, as expected (unexpected behavior not reproduced)\n");
}
else
{
printf("Token should now be 6, but it's actually %i\n", (int) token); // under MSVC2017 this prints -65530 !?
}
}
// This function is rewritten to use int-math instead of short-math and always gives the expected result
void allgood(short token16)
{
if (token16 >= 0) return;
int token = token16;
token -= ciFirstToken;
if (token == 6)
{
printf("Token is 6, as expected (odd behavior not reproduced)\n");
}
else
{
printf("Token should now be 6, but it's actually %i\n", (int) token);
}
}
int main(int, char **)
{
maybe_invokes_undefined_behavior(-32762);
allgood(-32762);
return 0;
}
最佳答案
does this invoke undefined behavior if (token==-32762) and (ciFirstToken==-32768)?
token -= ciFirstToken;
否(简短回答)
现在让我们逐个分解。
1) 根据 expr.ass对于复合赋值,-=
:
The behavior of an expression of the form
E1
op=E2
is equivalent toE1 = E1 op E2
except thatE1
is evaluated only once.
表达式:
token -= ciFirstToken;
相当于:
token = token - ciFirstToken;
// ^ binary (not unary)
2) additive operator (-
) 执行 usual arithmetic conversion对于算术类型的操作数。
Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
(1.5) Otherwise, the integral promotions shall be performed on both operands.
3) 然后将两个操作数提升为 int
。
根据 conv.prom/1 :
A prvalue of an integer type other than
bool
,char16_
t,char32_t
, or wchar_t whose integer conversion rank is less than the rank ofint
can be converted to a prvalue of typeint
ifint
can represent all the values of the source type;
4) 整数提升后,不需要进一步转换。
If both operands have the same type, no further conversion is needed.
5) 最后,表达式的未定义行为按照expr.pre 定义:
If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined
结论
现在替换值:
token = -32762 - (-32768);
在所有整数提升之后,两个操作数都在INT_MIN[1] 和INT_MAX[2] 的有效范围内.
并且在评估之后,数学结果 (6) 然后被隐式转换为 short
,这在 short
的有效范围内。
因此,表达式是良构的。
非常感谢@MSalters、@n.m 和@Arne Vogel 帮助回答这个问题。
Visual Studio 2015 MSVC14 Integer Limits和 MS Integer Limits定义:
[1] INT_MIN -2147483648
[2] INT_MAX +2147483647
SHRT_MIN –32768
SHRT_MAX +32767
关于c++ - 该程序中的 16 位数学是否调用了未定义的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51033539/
Based on Deep Learning (2017, MIT) book. 本文基于Deep Learning (2017, MIT),推导过程补全了所涉及的知识及书中推导过程中跳跃和省
因此,我需要一种方法来弄清楚如何获得5个数字,并且当您将它们中的任意两个相加时,将得出一个总和,您只能通过将这两个特定的数字相加而得到。 这是我正在谈论的示例,但有3个数字: 1个 3 5 1 + 3
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
如何将 a 和 b 之间的数字线性映射到 c 和 d 之间。 也就是说,我希望 2 到 6 之间的数字映射到 10 到 20 之间的数字...但我需要广义的情况。 我的脑子快炸了。 最佳答案 如果您的
嘿,我有一个方程式,我需要弄清楚它是基于图表的数学,其中图表上有两个点,需要获取其余值: 我正在构建一个 javascript 页面,它获取图表上的两个点,但需要吐出图表上的任何位置。 它用于根据了解
有谁知道如何用 Doxygen 得到实复场或射影平面的符号,i.o.w 符号,如 IR、IC、IP 等? 例如,我尝试了\f$\field{R}\f$,但无法识别。 非常感谢您的帮助,G. 最佳答案
我正在使用 Segment to Segment 最接近方法,该方法将输出两个长度段之间的最近距离。每个段对应一个球体对象的起点和终点。速度只是从一个点到另一个点。 即使没有真正的碰撞,最近的方法也可
我有一个 arduino 连接到 Stradella 系统钢琴 Accordion 。我在左手和弦的 12 个音符中的每一个上都有光学传感器。当我弹奏和弦时,它会触发三个传感器。如果我想让合成器演奏和
我正在开发一个具有一些简单功能的新包。现在我可以使用已经存在的“math-vectors”库中的函数;特别是“插值”和“反转”。如何在我的新包中使用这些?编写 y:=reverse(...) 显然是不
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Integer division in JavaScript 希望这是一个简单的问题,基本上我需要这样做: 分隔线
我有一张表格,上面有学校类(class)。此表单上可以有任意数量的类,每个类有 2 个字段。书本费和学费。 我有一个名为总计的第三个字段,当他们在其他字段中输入成本时,我想更新该字段。 这就是我的设置
今天早些时候我问了一个类似的问题,结果发现我只是数学很烂,因为我也无法解决这个问题。 我通过宽度/高度计算屏幕比例。我需要一个函数来将结果数字转换为新的比例。 例如 function convertN
我有一个起始数字,因此必须仅在开始循环时将该数字乘以一个因子,然后将结果乘以另一个因子的 X 倍,然后必须将循环乘以 Y 次,最后我需要总金额...我认为最好查看数字来了解我需要什么 例如,如果我从数
现在我用 JAVA 遇到了一些问题,但不记得如何获取坐标系之间的长度。 例如。A 点 (3,7)B点(7,59) 我想知道如何计算a点和b点之间的距离。非常感谢您的回答。 :-) 最佳答案 A = (
我有两种类型的文本输入,积极的和可疑的。在将输入到这两种类型的输入中的所有数字相加后,我需要显示多组这些输入的总数。例如:2 个阳性 + 2 个可疑 = 总计:4 然后,我需要从总数中找出积极与可疑的
我正在尝试将输入金额乘以 3.5%,任何人都可以给我任何想法如何做到这一点吗? $("#invest_amount").keyup(function() { $('#fee').va
有谁知道返回a的最大数的Math方法 给定的位数。 例如,使用1位数字的最大数字是9,2是99,3是999,4是9999......等等。 使用字符串很容易实现,但这并不完全 我在找什么。 pri
我是 Knockout 的新手,但仍对它一头雾水,我想知道如何使用两个 KO 变量进行简单的数学运算(加法和乘法)。 此刻我有: self.popInc1 = ko.observable('0.3')
我在谷歌地图应用程序中有以下内容,并希望显示转换为英尺的海拔高度,但如何向上/向下舍入到最接近的数字? (消除小数点后的数字)我尝试了 number.toFixed(x) 方法,但似乎什么也没做。 f
我最近开始使用 JavaScript 编写小型 Canvas 游戏,并试图全神贯注于 Vector 2d 数学。我了解 Vectors 的基础知识(比如它们代表 2d 空间中具有方向的点,您可以对它们
我是一名优秀的程序员,十分优秀!