CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: size limit (float)

1. Elite Member Power Poster
Join Date
Apr 2001
Location
Manchester, England
Posts
4,474

## size limit (float)

What is the maximum value (excluding infinity) which can be assigned to a float?

I'm building an open source program which assigns the value 3.40282347e+38f to a float. However, MSVC gives me compiler error C2177: constant too big

2. ## Re: size limit (float)

See https://docs.microsoft.com/en-us/cpp...ta-type-ranges for info on Microsoft data types/ranges/sizes. For a float the maximum size is 3.4E+38

There is also numeric_limits in c++. See http://www.cplusplus.com/reference/l...umeric_limits/

3. ## Re: size limit (float)

Originally Posted by John E
What is the maximum value (excluding infinity) which can be assigned to a float?

I'm building an open source program which assigns the value 3.40282347e+38f to a float. However, MSVC gives me compiler error C2177: constant too big
MSDN: Data Type Constants

4. Member
Join Date
Feb 2017
Posts
293

## Re: size limit (float)

Originally Posted by John E
What is the maximum value (excluding infinity) which can be assigned to a float?

I'm building an open source program which assigns the value 3.40282347e+38f to a float. However, MSVC gives me compiler error C2177: constant too big
In C++ the float fundamental type is implementation-defined. The C++ standard guarantees the correct max value of a float if the std::numeric_limits template is used, like

Code:
```#include <climits>

const float MAX_FLOAT = std::numeric_limits<float>::max(); // max float handled by this C++ implementation```
The value you stated, 3.40282347e+38f, is suggestive of the IEEE 754 standard but since the max float number in decimal representation is only approximate and only allows for 6-7 digits of precision the stated value may be (or become during compilation) too big due to rounding, which would explain the error. This on the other hand will be safe,

Code:
`const float MAX_FLOAT = 3.40282e+38f; // max float (IEEE 754 32-bit) stated with 6 digits of precision`
Still I recommend you always use the definition in the first code snippet; It's exact and works with any standards-compliant C++ compiler regardless of which internal representation of float it uses (and not just IEEE 754).
Last edited by wolle; August 26th, 2017 at 12:45 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.