我绞尽脑汁想弄清楚为什么这段代码没有得到正确的结果。我正在寻找浮点正和负溢出/下溢级别的十六进制表示。该代码基于此站点和Wikipedia 条目:
7f7f ffff ≈ 3.4028234 × 10 38 (最大单精度)——来自维基百科条目,对应于正溢出
这是代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
int main(void) {
float two = 2;
float twentyThree = 23;
float one27 = 127;
float one49 = 149;
float posOverflow, negOverflow, posUnderflow, negUnderflow;
posOverflow = two - (pow(two, -twentyThree) * pow(two, one27));
negOverflow = -(two - (pow(two, one27) * pow(two, one27)));
negUnderflow = -pow(two, -one49);
posUnderflow = pow(two, -one49);
cout << "Positive overflow occurs when value greater than: " << hex << *(int*)&posOverflow << endl;
cout << "Neg overflow occurs when value less than: " << hex << *(int*)&negOverflow << endl;
cout << "Positive underflow occurs when value greater than: " << hex << *(int*)&posUnderflow << endl;
cout << "Neg overflow occurs when value greater than: " << hex << *(int*)&negUnderflow << endl;
}
输出是:
当值大于时发生正溢出:
f3800000
当值小于时发生负溢出7f800000
:当值大于时发生正下溢:当值大于1
时发生负溢出:80000001
To get the hexadecimal representation of the floating point, I am using a method described here:
Why isn't the code working? I know it'll work if positive overflow = 7f7f ffff
.