C++ 这种类型的浮点数的最大尺寸是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6060409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Whats the maximum size for this type of float?
提问by Johnathan
In C++, using a float variable under a 32 bit OS, whats the maximum real number you could get from a float, given an additional decimal precision? Example: 1000.2
在 C++ 中,在 32 位操作系统下使用浮点变量,给定额外的十进制精度,您可以从浮点数中获得的最大实数是多少?示例:1000.2
回答by sje397
If you
如果你
#include <cfloat>
it should be
它应该是
FLT_MAX
回答by Fred Larson
#include <limits>
std::numeric_limits<float>().max()
Or maybe you're interested in the number of digits that can be represented:
或者您可能对可以表示的位数感兴趣:
std::numeric_limits<float>().digits10
See http://stdcxx.apache.org/doc/stdlibref/numeric-limits.html
回答by Mark Ransom
I'm going to assume you're using IEEE-754 floating point numbers, even though you did not specify - this is the most commonly used by modern processors. If you're using the binary32 form which is commonly selected by C++ compilers for the float
type, you can see from this table in Wikipediathat it holds just over 7 digits.
我将假设您使用的是 IEEE-754 浮点数,即使您没有指定 - 这是现代处理器最常用的。如果您使用的是 C++ 编译器通常为该float
类型选择的 binary32 形式,您可以从Wikipedia 的此表中看到它仅包含 7 位数字。
Reserving one digit to the right of the decimal point, the maximum value that can be held in 7 decimal digits is 999999.9
.
保留小数点右侧一位,7位十进制数最多可容纳的数值为999999.9
.
回答by ?imon Tóth
Checkout out cfloat
. This file is part of the C++ standard and contains constants that describe floating number limits.
结帐cfloat
。此文件是 C++ 标准的一部分,包含描述浮点数限制的常量。
If you want a specific number, you need to ask about a specific system (not just 32bit OS).
如果你想要一个特定的数字,你需要询问一个特定的系统(不仅仅是 32 位操作系统)。