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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:08:37+00:00 2026-06-01T03:08:37+00:00

In the following program you can see that each value slightly less than .5

  • 0

In the following program you can see that each value slightly less than .5 is rounded down, except for 0.5.

for (int i = 10; i >= 0; i--) {
    long l = Double.doubleToLongBits(i + 0.5);
    double x;
    do {
        x = Double.longBitsToDouble(l);
        System.out.println(x + " rounded is " + Math.round(x));
        l--;
    } while (Math.round(x) > i);
}

prints

10.5 rounded is 11
10.499999999999998 rounded is 10
9.5 rounded is 10
9.499999999999998 rounded is 9
8.5 rounded is 9
8.499999999999998 rounded is 8
7.5 rounded is 8
7.499999999999999 rounded is 7
6.5 rounded is 7
6.499999999999999 rounded is 6
5.5 rounded is 6
5.499999999999999 rounded is 5
4.5 rounded is 5
4.499999999999999 rounded is 4
3.5 rounded is 4
3.4999999999999996 rounded is 3
2.5 rounded is 3
2.4999999999999996 rounded is 2
1.5 rounded is 2
1.4999999999999998 rounded is 1
0.5 rounded is 1
0.49999999999999994 rounded is 1
0.4999999999999999 rounded is 0

I am using Java 6 update 31.

  • 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-01T03:08:39+00:00Added an answer on June 1, 2026 at 3:08 am

    Summary

    In Java 6 (and presumably earlier), round(x) is implemented as floor(x+0.5).1 This is a specification bug, for precisely this one pathological case.2 Java 7 no longer mandates this broken implementation.3

    The problem

    0.5+0.49999999999999994 is exactly 1 in double precision:

    static void print(double d) {
        System.out.printf("%016x\n", Double.doubleToLongBits(d));
    }
    
    public static void main(String args[]) {
        double a = 0.5;
        double b = 0.49999999999999994;
    
        print(a);      // 3fe0000000000000
        print(b);      // 3fdfffffffffffff
        print(a+b);    // 3ff0000000000000
        print(1.0);    // 3ff0000000000000
    }
    

    This is because 0.49999999999999994 has a smaller exponent than 0.5, so when they’re added, its mantissa is shifted, and the ULP gets bigger.

    The solution

    Since Java 7, OpenJDK (for example) implements it thus:4

    public static long round(double a) {
        if (a != 0x1.fffffffffffffp-2) // greatest double value less than 0.5
            return (long)floor(a + 0.5d);
        else
            return 0;
    }
    


    1. http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html#round%28double%29


    2. https://bugs.java.com/bugdatabase/view_bug?bug_id=6430675 (credits to @SimonNickerson for finding this)


    3. http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round%28double%29


    4. http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/lang/Math.java#Math.round%28double%29

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

Sidebar

Related Questions

In the following, how can I make it such that the program uses the
How can I prevent the following code crashing my program? ::MessageBox(NULL, Lbefore, NULL, MB_OK);
The following program is very simple: it outputs a single dot each half a
Consider following program: static void Main (string[] args) { int i; uint ui; i
I have the following program: ~/test> cat test.cc int main() { int i =
I have the following code that is attempting to start each of the commands
I'm trying to write a program that converts a long into a string and
So we have a program that the user can use by copying text from
Can one place an arbitrary program (firefox, openoffice, etc...) in a QX11EmbedContainer? The fllowing
In following program . I have one doubt. I have declared one global variable

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.