Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 929369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:09:36+00:00 2026-05-15T20:09:36+00:00

If a float overflow occurs on a value, I want to set it to

  • 0

If a float overflow occurs on a value, I want to set it to zero, like this…

m_speed += val;
if ( m_speed > numeric_limits<float>::max()) { // This might not even work, since some impls will wraparound after previous line
  m_speed = 0.f
}

but once val has been added to m_speed, the overflow has already occurred (and I’m assuming that the same problem would occur if i did if (( m_speed + val ) > ..).

How can I check to make sure an overflow is going to occur, without causing an overflow?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:09:37+00:00Added an answer on May 15, 2026 at 8:09 pm

    You could do:

    if (numeric_limits<float>::max() - val < m_speed)
    {
        m_speed = 0;
    }
    else
    {
        m_speed += val;
    }
    

    Another method might be:

    m_speed += val;
    if (m_speed == numeric_limits<float>::infinity())
        m_speed = 0;
    

    But do keep in mind when an overflow actually occurs, the result is undefined behavior. So while this probably works on most machines, it isn’t guaranteed. You’re better of catching it before it happens.


    Because this isn’t trivial to read at first, I’d wrap it into a function:

    template <typename T>
    bool will_overflow(const T& pX, const T& pValue, 
                        const T& pMax = std::numeric_limits<T>::max())
    {
        return pMax - pValue < pX;
    }
    
    template <typename T>
    bool will_underflow(const T& pX, const T& pValue, 
                        const T& pMin = std::numeric_limits<T>::min())
    {
        return pMin + pValue > pX;
    }
    
    m_speed = will_overflow(m_speed, val) ? 0 : m_speed + val;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image with float:left , and I’d like it to overflow its
I've this code : .outer { width: 1000px; float: left; overflow: scroll; } .inner
I have this HTML & CSS and the 'overflow: hidden' tag is not working
Hi stack overflow community. I have 2 dynamic divs that I want to float
I want to float a element so that it stays above elements on its
I have the following html (not proper code but for understanding) <div id=content-wrap overflow-hidden>
css: .listingContainer { margin:auto; overflow:hidden; padding:0 0 16px 16px; width:660px; } .listingItem { float:left;
<div name=qst id=qst style='width:60%;float:left;display:inline;overflow:auto;'> </div> <div id=data name=data class='checkbox' style='overflow:hidden;width:35%;height:550px;margin-left:0px;'></div> These two div appears
I have a div with overflow: auto . Suppose I want to put an
I have this problem that always occurs when I'm using display: inline-block to display

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.