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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:14:55+00:00 2026-05-16T17:14:55+00:00

I have a double value d and would like a way to nudge it

  • 0

I have a double value d and would like a way to nudge it very slightly larger (or smaller) to get a new value that will be as close as possible to the original but still strictly greater than (or less than) the original.

It doesn’t have to be close down to the last bit—it’s more important that whatever change I make is guaranteed to produce a different value and not round back to the original.

(This question has been asked and answered for C, C++)

The reason I need this, is that I’m mapping from Double to (something), and I may have multiple items with the save double ‘value’, but they all need to go individually into the map.

My current code (which does the job) looks like this:


private void putUniqueScoreIntoMap(TreeMap map, Double score,
            A entry) {

        int exponent = 15;
        while (map.containsKey(score)) {
            Double newScore = score;
            while (newScore.equals(score) && exponent != 0) {
                newScore = score + (1.0d / (10 * exponent));
                exponent--;
            }
            if (exponent == 0) {
                throw new IllegalArgumentException("Failed to find unique new double value");
            }
            score = newScore;
        }
        map.put(score, entry);
    }

  • 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-05-16T17:14:56+00:00Added an answer on May 16, 2026 at 5:14 pm

    Use Double.doubleToRawLongBits and Double.longBitsToDouble:

    double d = // your existing value;
    long bits = Double.doubleToLongBits(d);
    bits++;
    d = Double.longBitsToDouble(bits);
    

    The way IEEE-754 works, that will give you exactly the next viable double, i.e. the smallest amount greater than the existing value.

    (Eventually it’ll hit NaN and probably stay there, but it should work for sensible values.)

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

Sidebar

Related Questions

I have a double value f and would like a way to nudge it
If I have a double (234.004223), etc., I would like to round this to
I have a Double which could have a value from around 0.000001 to 1,000,000,000.000
I have double (or float) variables that might be empty, as in holding no
I have trouble comparing 2 double in Excel VBA suppose that I have the
I have a string that contains both double-quotes and backslashes that I want to
I do have somewhat of a feeling that people will try to beat me
In my program I have one array with 25 double values 0.04 When I
I have the following C++ method : __declspec(dllexport) void __stdcall getDoubles(int *count, double **values);
Let's say I have an array of lots of values (C++ syntax, sorry): vector<double>

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.