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 7807401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:50:44+00:00 2026-06-02T02:50:44+00:00

Refer to this article: math-round-vs-hack We have Math functions optimized. We use (num+0.5)|0 instead

  • 0

Refer to this article:
math-round-vs-hack

We have Math functions optimized.

We use (num+0.5)|0 instead of Math.round().

But there’s a problem which make confused that when num > 2147483647, it comes out wrong results.

function round(n) {
    return (n + 0.5) | 0;
};

round(2147483648) will return -2147483648

And according to wikipedia:

2147483647 in computing

The number 2,147,483,647 is also the maximum value for a 32-bit signed integer in >computing. It is therefore the maximum value for variables declared as int in many >programming languages running on popular CPUs, and the maximum possible score (or amount >of money) for many video games. The appearance of the number often reflects an error, >overflow condition, or missing value.[8] Similarly, "(214) 748-3647" is the sequence of >digits represented as a United States phone number and is the most common phone number >listed on web pages.[9]
The data type time_t, used on operating systems such as Unix, is a 32-bit signed integer >counting the number of seconds since the start of the Unix epoch (midnight UTC of 1 >January 1970).[10] The latest time that can be represented this way is 03:14:07 UTC on >Tuesday, 19 January 2038 (corresponding to 2,147,483,647 seconds since the start of the >epoch), so that systems using a 32-bit time_t type are susceptible to the Year 2038 >problem.[11]

How can I handle this situation to ensure good performance?

  • 1 1 Answer
  • 0 Views
  • 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-06-02T02:50:45+00:00Added an answer on June 2, 2026 at 2:50 am

    Short answer: Use Math.round()

    I would file this entire exercise under “why you should never do micro optimizations”. Take a look at this fiddle: http://jsfiddle.net/toxicsyntax/a5rWm/2/

    Here is some results from different browsers:

    • Firefox 11: round: 185ms bit_round: 123ms (for 100000000 iterations)
    • Chrome 18: round: 99ms bit_round: 96ms (for 100000000 iterations)
    • IE 9: round: 227ms bit_round: 123ms (for 1000000 iterations)
    • IPhone 4, Safari: round 399ms, bit_round 32ms (for 1000000 iterations)

    Sure, in the best case the bitwise round function is way faster than using Math.round(), but does it really matter? Worst case Math.round() still handles several millions rounds pr. seconds without problems.

    How many do you roundings do you have to do? I don’t expect you would ever have to round numbers in javascript, except for displaying numbers, and even if you are displaying thousands of rounded numbers pr. second, Math.round() is still going to be fast enough for you.

    Update

    And also, regarding micro optimazations in general, be sure to check out the article from Coding Horror: http://www.codinghorror.com/blog/2009/01/the-sad-tragedy-of-micro-optimization-theater.html

    Micro optimazation is very, very rarely a good idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 files: xxxxxTest.java [ refer this ] public class xxxxxTest extends TestCase
Please refer to this post. I have become able to configure my web.config file
I am developing a hibernate console application and refer to this article http://www.mkyong.com/hibernate/how-to-display-hibernate-sql-parameter-values-solution/ to
I refer to this article: WPF TreeView HierarchicalDataTemplate - binding to object with multiple
I have been reading this article to understand the gcroot template. I understand the
I know there are already articles that covered this, but I'm having an issue
Please refer to this background question. After constructing this COUNT, how would I then
I want to refer to this post, because it might relate: Make Form Fields
What does 'this' keyword refer to when used in global object? Let's say for
Note: In this question I'm using the term autocomplete (or iterative search) to refer

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.