在Linux和Windows上同样的计算 - >不同的结果 [英] Same calculation on Linux and Windows --> different results

查看:513
本文介绍了在Linux和Windows上同样的计算 - >不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string toFormatFromDecimal(long long t(long long t) ,格式格式){
int digitCount = ceil(log(t)/ log((int)format));
string hex =;
for(int i = 0; i< digitCount; i ++){
long double cur =(long double)t /(long double)(format);
long long ganzzahl =(long long)cur;
long double kommazahl = cur - ganzzahl;
十六进制+ =数字[(long long)(kommazahl * format)];
t = ganzzahl;
}
return string(hex.rbegin(),hex.rend());





$ b我使用Linux中的GCC和Windows上的Visual Studio c ++编译器
看来我在这里的整数部分得到了不同的值:

  long long ganzzahl =(long long)cur; 

任何想法如何发生?在Linux和Windows上有不同的预测吗?



感谢
Florian



- 解决方案 -

  string toFormatFromDecimal(long long t,Format format){
int digitCount = ceil(log(t)/ log ((int)format));
string hex =;
for(int i = 0; i< digitCount; i ++){
hex + = digits [(int)(t%format)];
t = t /格式;
}
return string(hex.rbegin(),hex.rend());



$ b $ div class =h2_lin>解决方案

是的,GCC和Visual Studio C ++有不同的 long double 类型。在x86的GCC生成代码中, long double 是80位双扩展IEEE 754格式(*),而Visual Studio C ++将 long double 类似于64位双精度IEEE 754格式(*)。
$ b $ (long double)t 在两个平台上都不一定是相同的数字,而且分区也不一样。虽然你已经标记了你的问题整数除法,但它是不同浮点类型之间的浮点除法。几乎:它表现得非常非常好非常类似于具有15个指数位和63个有效位的79位 IEEE 754 类型,但它有一个稍宽的指数范围,因为它在有效数中使用了一个明确的位。

(**)差不多:因为编译器生成的指令在将x87配置为53位有效数之后的历史x87指令,非正规结果可能是双舍入的( reference )。


I have coded following algorithm to convert a decimal value into Binary/Hexadecimal etc..

string toFormatFromDecimal(long long t, Format format) {
    int digitCount = ceil(log(t) / log((int) format));
    string hex = "";
    for (int i = 0; i < digitCount; i++) {
        long double cur = (long double)t / (long double)(format);
        long long ganzzahl = (long long) cur;
        long double kommazahl = cur - ganzzahl;
        hex += digits[(long long) (kommazahl * format)];
        t = ganzzahl;
    }
    return string(hex.rbegin(), hex.rend());
}

I use GCC in linux and Visual Studio c++ compiler on Windows It seems that i got different values at the "integer" Division here:

long long ganzzahl = (long long) cur;

Any Idea how this could happen? are there different precissions on Linux and Windows?

Thanks Florian

--Solution--

string toFormatFromDecimal(long long t, Format format) {
    int digitCount = ceil(log(t) / log((int) format));
    string hex = "";
    for (int i = 0; i < digitCount; i++) {
        hex += digits[(int) (t%format)];
        t = t/format;
    }
    return string(hex.rbegin(), hex.rend());
}

解决方案

Yes, GCC and Visual Studio C++ have different long double types. On GCC generating code for x86, long double is a 80-bit double-extended IEEE 754 format(*), whereas Visual Studio C++ treats long double like a 64-bit double-precision IEEE 754 format(*).

So (long double)t does not have to be the same number on both platforms, and the division is not the same either. Although you have tagged your question "integer-division", it is a floating-point division between different floating-point types.

(*) almost: it behaves very very much like a 79-bit IEEE 754 type with 15 exponent bits and 63 significand bits would, but it has a slightly wider exponent range since it uses an explicit bit for the leading 1 in the significand.

(**) almost: because the compiler generates instructions that use the historical x87 instructions after having configured the x87 for 53-bit significands, denormal results may be double-rounded (reference).

这篇关于在Linux和Windows上同样的计算 - &gt;不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆