|
|
导航: |
论坛 -> DELPHI技术
斑竹:liumazi,sephil |
|
作者: |
|
2022/5/23 13:55:45 |
标题: |
|
加入我的收藏 |
楼主: |
#define AAAA 0xFF010000 void __fastcall TForm8::Button3Click(TObject* Sender) { double n[] = { 1, 1.5, 1.7 }; int index = AAAA + 1 * 1000; int index1 = AAAA + 1.0 * 1000; int index3 = AAAA + int(n[0] * 1000); int index2 = AAAA + n[0] * 1000; } IDE:RAD11.1,windows10 上述代码在传统编译器classic下面,结果正常,index、index1、index2、index3均=0xFF0103E8 而在Clang下编译,则运行时出错: index=0xFF0103E8; index1=0x3A1424;//变动 index2=0x80000000; index3=0xFF0103E8; 同时报错: First chance exception at $7281E09B. Exception class $C0000090 with message 'floating point invalid operation at 0x7281e09b'. Process Project5.exe (9720)
是上面代码问题还是RAD问题?
----------------------------------------------
========== |
作者: |
|
2022/5/23 17:39:42 |
2楼: |
int index2 = AAAA + n[0] * 1000;这行有问题吧,浮点数直接给整数? int index1 = AAAA + 1.0 * 1000;这个结果没法说所谓的准确性,只能说精度。IEEE格式的浮点数就是精度问题,不像整数那么运算。 就像BASIC语言里面 2*2=4, 但2^2=3.9999999或者4.0000001一样,都很正常的,谈不上错误吧。如果要高精度运算,就要写方法,直接用运算符肯定不行。
----------------------------------------------
-
|
作者: |
|
2022/5/23 18:28:32 |
3楼: |
直接运行以下C程序 #include<stdio.h> #include<math.h> #define AAAA 0xFF010000 int main(){ double n[] = { 1, 1.5, 1.7 }; int index = AAAA + 1 * 1000; int index1 = AAAA + 1.0 * 1000; int index3 = AAAA + (int)floor(n[0] * 1000); int index2 = AAAA + n[0] * 1000; printf("%x\n%x\n%x\n%x\n",index,index1,index2,index3); return 0; }
CLANG运行结果: ff0103e8 33333333 80000000 ff0103e8
GCC运行结果: ff0103e8 7fffffff 80000000 ff0103e8 可见,在C的标准里,index1的结果就是未定义, index2的结果就是0x80000000 标准如此,只能说错的是C++ Builder的clasic编译器
----------------------------------------------
-
|
作者: |
|
2022/5/23 20:37:47 |
4楼: |
浮点数赋值给整数编译器没转换,这是个教训。谢谢楼上各位!
----------------------------------------------
==========
|
作者: |
|
2022/5/23 20:41:36 |
5楼: |
to 3楼: 你测试运行下面这行后没报错吗? int index2 = AAAA + n[0] * 1000; C++Builder 允许给出结果0x80000000后就报下面错了:
'floating point invalid operation at 0x7281e09b'
----------------------------------------------
==========
|
作者: |
|
2022/5/23 22:16:00 |
6楼: |
…… 被禁用帐号,帖子内容自动屏蔽! ……
----------------------------------------------
该账号是个傻逼
|
作者: |
|
2022/5/24 1:47:20 |
7楼: |
@smartdata 我用的是C,不是C++, C并没有异常,通常靠检测errno来判断有没有错误。
----------------------------------------------
-
|
|