How are upper and lower bounds for floating point numbers defined?

I have a question about the quote below (N3797, 3.9.1 / 8):

The representation of floating point type values ​​is implementation-defined.

As far as I understand, this gives a complete implementation implementation when defining the boundaries of floating point numbers. They are specified in template<class T> class numeric_format . For example,

 #include <iostream> #include <limits> int main() { std::cout << "double_max = " << std::numeric_limits<double>().max() << std::endl; std::cout << "double_min = " << std::numeric_limits<double>().min() << std::endl; std::cout << "float_max = " << std::numeric_limits<float>().max() << std::endl; std::cout << "float_min = " << std::numeric_limits<float>().min() << std::endl; } 

Demo

My question is: can the upper and lower bounds of floating point numbers be arbitrarily high or low, or are there limits? Does pure C also provide an implementation of a specific set of values ​​for floating point numbers?

I suspect that this depends on the architecture with which we work.

+8
c ++ c floating-point
source share
3 answers

The limits for built-in types are mostly limited by hardware constraints. x64s typically use IEEE 754 for their floating point data, as defined in the FPA standard (hardware coprocessor).

In any case, how things are internally represented and handled may differ, as you noted.

The representation of floating point type values ​​is implementation-defined

the compiler usually has knowledge (especially with reference to the source code that generates the code) of the base target system, and therefore it can select the correct set of instructions when requested using fp arithmetic.

You can always define your own data type, which itself manages the physical resources. The term you refer to may be arithmetic of arbitrary accuracy (usually much slower if your data matches what the equipment offers, you should go for it).

In special cases (for example, some built-in modules where there is no FPU or floating point operations cannot be performed), emulation can be used. It is cheaper (fewer transistors), although slower.

+4
source share

the upper and lower bounds depend on the implementation of floating point numbers. Thus, this is not only a user-defined limit. rather, its mathematical limitation, which depends on the memory layout developed by the compiler for storing floating point numbers.

If you want a user-defined limit , you can use your own integer class, which overloads operators for numerical operations and applies your user limit to any operation.

+3
source share

In simple C, you can #include <float.h> , which provides constants such as:

 DBL_MAX DBL_MIN FLT_MAX FLT_MIN 

as well as the number of bits and digits of the mantissa, the range of exponentiality, etc.

I am always confused by which of the C ++ reference sites is the one that you are allowed to publish here, and which you should not, because you get comments about it, "but here you can find it on Google: http: / /www.cplusplus.com/reference/cfloat/ Since this is rather a standard file, I expect that there isn’t such a big difference anyway. Of course, this refers to the "C ++ - ified version", but the macros are defined the same for C .

(Note that C ++ limits are usually implemented from the same constants that are in float.h in a sense, directly or indirectly)

+2
source share

All Articles