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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:02:18+00:00 2026-06-17T03:02:18+00:00

Say I have a float X. I want to find the largest number that

  • 0

Say I have a float X. I want to find the largest number that is less than X and can be losslessly stored in a float.

IIRC the IEEE standard says you can do this by converting the float’s bits to an int representation, subtracting one, and convert back to float.

(edit: this is true for positive numbers that are not NaN or inf. For negative numbers, you must add. See Rawling’s answer for more info.)

To change between representations, I only know of C#’s (cast) operator, which truncates. That’s not what I want.

Is there a way to do this in C#?

  • 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-17T03:02:19+00:00Added an answer on June 17, 2026 at 3:02 am

    Here is how you can simply turn a float into an int, change it, and then turn it back into a float:

    float myFloat = 10.3f;
    // Get the bytes making up the float
    byte[] bytes = BitConverter.GetBytes(myFloat);
    // Make an int out of them
    int myInt = BitConverter.ToInt32(bytes, 0);
    // Change it
    myInt--;
    // Get the bytes making up the int
    bytes = BitConverter.GetBytes(myInt);
    // Make a float out of them
    myFloat = BitConverter.ToSingle(bytes, 0);
    // gives 10.2999992 or so
    

    BitConverter even has this built in for the 64-bit equivalent:

    double myDouble = 10.3;
    long myLong = BitConverter.DoubleToInt64Bits(myDouble);
    myLong--;
    myDouble = BitConverter.Int64BitsToDouble(myLong); // gives 10.2999999...
    

    However, as Peter Ruderman points out, a simple decrement of the underlying int doesn’t reliably give you the next-smallest float.

    In particular, for negative numbers, you need to increment the integer in order to make the float more negative. For float zero, the next smallest int actually corresponds to NaN, so you need a special case there.

    Here are a couple of functions I’ve knocked together that should cope with these cases in general; it also looks like it sensibly travels between large numbers and positive/negative infinity, too! I’ve used unsafe conversions to reduce code length, but you can stick to the byte conversions above if you wish:

    static unsafe float Increment(float f)
    {
        int val = *(int*)&f;
        if (f > 0)
            val++;
        else if (f < 0)
            val--;
        else if (f == 0)
            return float.Epsilon;
        return *(float*)&val;
    }
    static unsafe float Decrement(float f)
    {
        int val = *(int*)&f;
        if (f > 0)
            val--;
        else if (f < 0)
            val++;
        else if (f == 0)
            return -float.Epsilon; // thanks to Sebastian Negraszus
        return *(float*)&val;
    }
    

    As Jeppe points out, you probably also want to

    • Start each function with if (float.IsNaN(f)) return f;, so you don’t accidentally increment or decrement a NaN and give something which is a number.
    • Also consider checking the input against float.PositiveInfinity or .NegativeInfinity, since, mathematically speaking, those should probably stay constant under increment/decrement.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a float that I want to interpret as an unsigned int
Say I have a large number (integer or float) like 12345 and I want
I have a number (let's say, 34), and I want to find its next
I have a float value say 28.980000. I want something like that 28.98, only
Lets say that i have the following array : float QuadVertices[4 * 2]; float
Let's say I have two animations that work individually, but I want them to
Say I have a Float. I want the first 32 bits of the fractional
Say I have these 3 different functions that I may want to point to:
Say I have a float in the range of [0, 1] and I want
I have a list of say 100k floats and I want to convert it

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.