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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:59:41+00:00 2026-05-31T23:59:41+00:00

I need to increment a float value atomically. I get its int value by

  • 0

I need to increment a float value atomically. I get its int value by calling Float.floatToIntBits on it. If I just do an i++ and convert it back to float, it does not give me the expected value. So how would I go about it?

(I’m trying to create an AtomicFloat through AtomicInteger, hence this question).

EDIT: here’s what I did:

Float f = 1.25f;
int i = Float.floatToIntBits(f);
i++;
f = Float.intBitsToFloat(i);

I wanted 2.25, but got 1.2500001 instead.

  • 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-31T23:59:42+00:00Added an answer on May 31, 2026 at 11:59 pm

    The reason is that the bits you get from floatToIntBits represents

    1. sign
    2. exponent
    3. mantissa

    laid out like this:

    Repr:  Sign   Exponent          Mantissa
    Bit:    31   30......23  22.....................0
    

    Incrementing the integer storing these fields with 1 won’t increment the float value it represents by 1.

    I’m trying to create an AtomicFloat through AtomicInteger, hence this question

    I did precisely this in an answer to this question:

    • Java: is there no AtomicFloat or AtomicDouble?

    To add functionality to increment the float by one, you could copy the code of incrementAndGet from AtomicInteger (and change from int to float):

    public final float incrementAndGet() {
        for (;;) {
            float current = get();
            float next = current + 1;
            if (compareAndSet(current, next))
                return next;
        }
    }
    

    (Note that if you want to increment the float by the smallest possible value, you take the above code and change current + 1 to current +Math.ulp(current).)

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

Sidebar

Related Questions

Basically I need to reset Identity Increment for all tables to its original. Here
HI i need to increment 15 minutes in time intrval which i get in
First my call back to increment I know is not correct. I am not
I need to increment a date value by one day in JavaScript. For example,
I need to increment a number in a source file from an Ant build
I need to increment a String in java from aaaaaaaa to aaaaaab to aaaaaac
I have an application where I need to INSERT an auto_increment value from a
I have table with an auto-increment field, but I need to transfer the table
Is there a integer element for a cookie? I need the cookie to increment
I need to increment User's balance, so I do: Doctrine_Query::create()->from('User')->update('balance', 'balance + 0.15')->execute(); And

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.