使用长双精度数“9223372036854775807 - 1”仍然删除最后一位数字并显示为“9223372036854775800”
我正在尝试读取两个数字,并显示它们之间的绝对差异。这些数字变得大得离谱,所以我不得不从 LONG 切换到 LONG DOUBLE,并以小数点 0 精度显示它们。我的问题是,对于主题中列出的数字,当我将其从字符串的 c_str 扫描为长双精度数时,要么在扫描中最后一位数字被删除,要么更有可能长双精度数的显示正在删除它。 9223372036854775807 - 1 应该是 9223372036854775806 但它显示的是 9223372036854775800 ,当我停下来检查其中包含 9223372036854775807 的长双精度时,它只是向我显示“9.2233720368547758e+018”
我会把这一切归咎于 2 位处理器,但似乎在 64 位上它仍然打印错误的答案。有谁有办法保留整个号码吗? 我的包含内容被删除了似乎会扰乱 html 解析器的字符。
#include iostream
#include string
#include math.h
using namespace std;
int main () {
string line;
std::getline(std::cin, line);
while ( std::cin )
{
long double n, m, o;
sscanf ((char *)line.c_str(),"%Lf %Lf",&n, &m);
if(n>=m)
{
o = n - m;
}
else
{
o = m-n;
}
char buffer[100000];
sprintf( buffer , "%0.0Lf\0", o);
cout << buffer << endl;
std::getline(std::cin, line);
}
return 0;
}
I'm trying to read in two numbers, and display the absolute difference between them. The numbers get ridiculously large, so I had to switch from LONG to LONG DOUBLE and just display them with 0 precision on the decimal. My issue is that with the number listed in the subject, when I scan it into a long double from a string's c_str either in the scan the last digit is being dropped, or more likely the display of the long double is dropping it.
9223372036854775807 - 1 should be 9223372036854775806 but instead it's displaying 9223372036854775800, and when I stop to inspect the long double with the 9223372036854775807 in it, it just shows me "9.2233720368547758e+018"
I would blame this all on a 2 bit processor, but it seems on a 64 bit it's still printing the wrong answer. Has anyone got any way to preserve the entire number?
my includes are stripped of characters that seemed to be messing with the html parser.
#include iostream
#include string
#include math.h
using namespace std;
int main () {
string line;
std::getline(std::cin, line);
while ( std::cin )
{
long double n, m, o;
sscanf ((char *)line.c_str(),"%Lf %Lf",&n, &m);
if(n>=m)
{
o = n - m;
}
else
{
o = m-n;
}
char buffer[100000];
sprintf( buffer , "%0.0Lf\0", o);
cout << buffer << endl;
std::getline(std::cin, line);
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的编译器支持,我将停止使用
long double
并使用long long
。您说您之前一直使用long
,所以我认为您没有存储小数部分;如果这是正确的,那么long long
将是您想要的,而不是long double
。我在 MSVC++ 2010 上使用long long
测试了您的代码,它给出了预期的输出。正如 Mysticial 所指出的,当今许多编译器上的 double 与 long double 相同。
I would stop using
long double
and uselong long
if your compiler supports it. You said you had previously been usinglong
so I don't think you were storing fractional parts; if that is correct, thenlong long
would be what you want, notlong double
. I tested your code withlong long
on MSVC++ 2010 and it gave the expected output.And as Mysticial noted,
double
is the same aslong double
on many compilers today.long double
并不需要来提供比double
更高的精度。然而,即使是这样,您也必须告诉编译器该文字是一个long double
。如果没有小数点,编译器将假定 9223372036854775807 是某种整数。所以你需要将其写为9223372036854775807.0L
。再说一遍,编译器不必为您提供比 double 更高的精度。因此,如果您没有获得任何额外的精度,请不要感到太惊讶。
long double
is not required to provide more precision thandouble
. However, even if it does, you have to tell the compiler that the literal is along double
. Without a decimal point, the compiler will assume9223372036854775807
is some kind of integer. So you need to write it as9223372036854775807.0L
.And again, the compiler doesn't have to give you more precision than a
double
. So don't be too surprised if you don't get any added precision.接受答案后。
浮点数的精度有限。
在
中是LDBL_DIG
。这是代码可以将文本读入long double
的有效十进制位数,并且始终会再次打印出相同的数字。指定至少为 10,通常为 15 以上。
9223372036854775807
,其 18 位有效十进制数字肯定超出了系统的LDBL_DIG
值。切换到整数数学的范围要有限得多。范围。
long
可能不起作用,因为LONG_MAX
可能小至2147483647
。最好使用long long
,它可以处理至少9223372036854775807
的数字 - 足够大。After accept answer.
Floating point numbers have finite precision.
In
<cfloat>
isLDBL_DIG
. This is the number of significant decimal digits that code may read text into along double
and will always print the same digits out again.The is specified to be at least 10 and is often 15+.
9223372036854775807
, with its 18 significant decimal digits certainly exceeds your system's value ofLDBL_DIG
.Switching to integer math has a far more limited range.
long
may not work asLONG_MAX
may be as small as2147483647
. Better to uselong long
which can cope with numbers up to at least9223372036854775807
- just big enough.