DBL_MAX &双精度数的最大值
这一行:
NSLog(@"DBL_MAX: %f", DBL_MAX);
打印这个非常大的值: 179769313486231570814527423731704356798070567525844996598917476 8031572607800285387605895586327668781715404589535143824642343213 268894641827684675467035375169860499105765512820762454900903893 2894407586850845513394230458323690322294816580855933212334827479
但是,当我测试这样的双精度值时:
double test = 9999999999999999.0;
NSLog(@"test: %f", test);
我得到了这个意外的结果:
test: 10000000000000000.000000
这似乎是产生预期结果的最大位数:
double test = 999999999999999.0;
NSLog(@"test: %f", test);
test: 999999999999999.000000
如何使用更高的正分数?
谢谢。
This line:
NSLog(@"DBL_MAX: %f", DBL_MAX);
prints this very large value:
17976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407586850845513394230458323690322294816580855933212334827479
However, when I test a double value like this:
double test = 9999999999999999.0;
NSLog(@"test: %f", test);
I get this unexpected result:
test: 10000000000000000.000000
This appears to be the maximum number of digits that produce the expected result:
double test = 999999999999999.0;
NSLog(@"test: %f", test);
test: 999999999999999.000000
How can I work with higher positive fractions?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,我无法直接回答这个问题,因为我不明白你的意思“我如何处理更高的正分数?”。
不过,我可以阐明什么是浮点数、什么不是浮点数。
浮点数由以下部分组成:
这些通常使用巧妙的编码组合成 32、64、80 或 128 位。此外,还使用一些特殊编码来表示+-无穷大、非数字(NaN) 和+-零。
由于尾数的位数有限,因此您的值只能有这个数量的有效位。一个非常小的浮点数可以表示 10^-308 和大的 10^308 中的值。然而,任何数字只能有大约 16 位十进制数字。
换句话说,如果 DBL_MAX 与变量中存储的信息量不对应,则打印输出。例如,无法表示相同的数字,但末尾使用 ...7480 而不是 ...7479。
那么回到问题,为了告诉如何代表你的价值观,你必须描述你想要代表什么样的价值观。它们真的是分数吗(即一个整数除以另一个整数),在这种情况下,您可能想使用两个整数来表示。如果您想表示非常大的值,您可能需要使用 http://gmplib.org 之类的包
Unfortunately, I can't answer the question directly, as I don't understand what you mean by "How can I work with higher positive fractions?".
However, I can shed some light on what a floating-point number is ans what it isn't.
A floating-point number consists of:
These are combined using a clever encoding typically into 32, 64, 80, or 128 bits. In addition, some special encodings are used to represent +-infinity, Not a Number (NaN), and +-Zero.
As the mantissa has a limited number of bits, your value can only have this number of significant bits. A really small floating-point number can represent values in the 10^-308 and and large one 10^308. However, any number can only have about 16 decimal digits.
On other words, the print-out if DBL_MAX does not corresponds the amount of information stored in the variable. For example, there is no way to represent the same number but with a ...7480 instead of ...7479 at the end.
So back to the question, in order to tell how to represent your values, you must describe what kind of values you want to represent. Are they really fractions (i.e. one integer divided by another integer), in that case you might want to represent this using two integers. If you want to represent really large values, you might want to use packages like http://gmplib.org
C# 中的浮点并不总是产生准确的结果。有些数字无法用双精度型、浮点数或小数表示。您可以通过使用“十进制”而不是“双精度”来提高准确性,但它仍然不能确保所有数字都能准确表示。
Floating point in C# doesn't produce accurate results all the time. There are numbers that cannot be represented in double, floats or decimals. You can improve your accuracy by using "decimal" instead of "double", but it still doesn't ensure that all numbers will be represented exactly.