| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2497 人关注过本帖
标题:以下两份代码为何有这差别?
只看楼主 加入收藏
ditg
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:16
帖 子:852
专家分:1937
注 册:2014-4-10
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册


龙芯3A2000手册提供的依据和编码格式;有理由认为intel不是IEEE 754标准(网上的很多文章估计过时了)

梦想拥有一台龙芯3A-4000
2020-06-13 12:51
hffjhhh
Rank: 1
等 级:新手上路
帖 子:127
专家分:7
注 册:2019-4-10
收藏
得分:0 
回复 11楼 ditg
我没有学过汇编,因此不知道如何用汇编指令来验证是否是intel处理器的问题。
本机上sizeof(long double)的大小为16个字节。
如果intel处理器不是IEEE 754标准,那么是否意味着就是处理器的问题导致无法正确计算16个字节的long double类型的数据呢?
如果不是处理器的问题,那么是否意味着就是编译器的问题导致无法计算16个字节的long double类型的数据呢?也无法用%llf格式来打印16个字节的long double类型的数据呢?
2020-06-13 16:00
fulltimelink
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:171
专家分:752
注 册:2020-4-1
收藏
得分:5 
%Le
2020-06-13 17:30
fulltimelink
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:7
帖 子:171
专家分:752
注 册:2020-4-1
收藏
得分:0 
搬运工
The problem is that the compiler (GCC) and the runtime library (Microsoft's) are implemented by different groups that happen to have different ideas about how the type long double should be represented. (gcc uses 128 bits for long double; Microsoft uses 64 bits, with the same representation as double.)

Either choice of representation is perfectly legitimate, but they're incompatible with each other. It's not a bug either in GCC or in Microsoft's library, but in the way MinGW integrates them.

Your options are to use an implementation other than MinGW, to write or copy code that handles long double correctly , or to avoid calling any library functions that take arguments or return results of type long double (computations on long double shouldn't be a problem as long as they don't call any library functions). For example, you can convert to double and print with %g, with some loss of range and precision.

Another (probably better) workaround is to compile with -D__USE_MINGW_ANSI_STDIO, which causes MinGW to use its own implementation of printf and friends rather than relying on the Microsoft implementation.

2020-06-13 17:38
ditg
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:16
帖 子:852
专家分:1937
注 册:2014-4-10
收藏
得分:0 
long double我还没完全整出来(宿舍没有intel电脑,就算有也没有linux系统的),前面说的是double类型(64位)。

我认为影响编译结果的只有cpu指令和编译器。两个假设:假设相同编译器在不同平台间遵守相同的设计规则;那么输出结果也应该相同。假设前面给的网站编码结果正确(反正我计算得到这个编码以及龙芯手册给的的确是真实结构),那么下面代码应该输出3.3。目前相同代码在龙芯上输出3.3(龙芯说它依照的是IEEE 754),intel输出0.0。反正intel的输出结果总有问题吧?!(欢迎拍砖)

程序代码:
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("%lf\n", 0x400a666666666666);
}


结果:
图片附件: 游客没有浏览图片的权限,请 登录注册


(刚看到,上面的引用不太相关,因为实验没有使用windows系统)

[此贴子已经被作者于2020-6-13 18:02编辑过]


梦想拥有一台龙芯3A-4000
2020-06-13 17:54
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
以下是引用hffjhhh在2020-6-12 13:19:50的发言:
即使改成了Lf也一样还是打印0.000000.


如果你用的是 MinGW,那么应该如14楼 fulltimelink 说的那样做,或在 #include <stdio.h> 之前加上 #define __USE_MINGW_ANSI_STDIO 1
如果不是 MinGW 的话,该升级一下编译器了,因为C标准在上个世纪就支持long double了。
2020-06-15 09:05
快速回复:以下两份代码为何有这差别?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018704 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved