size limit (float)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: size limit (float)

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,450

    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
    "A problem well stated is a problem half solved. - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,711

    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/
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2017 (15.3.4)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    18,956

    Re: size limit (float)

    Quote Originally Posted by John E View Post
    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
    Victor Nijegorodov

  4. #4
    Join Date
    Feb 2017
    Posts
    147

    Re: size limit (float)

    Quote Originally Posted by John E View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)