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

  • Home
  • SEARCH
  • 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 8377875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:47:06+00:00 2026-06-09T15:47:06+00:00

double d = 0; // random decimal value with it’s integral part within the

  • 0
double d = 0; // random decimal value with it's integral part within the range of Int32 and always positive.
int floored = (int) Math.Floor(d); // Less than or equal to.
int ceiled  = (int) Math.Ceiling(d); // Greater than or equal to.
int lessThan = ? // Less than.
int moreThan = ? // Greater than.

The Floor and ceiling functions include the largest/smallest integer that is less than/greater than or equal to d respectively. I want to find out the largest/smallest integer that is less than/greater than but NOT equal to d respectively.

OF course this can be achieved through a few if's and but's but I am looking for a method that either does not include branching or is at least a very fast since this operation will be performed billions of times in an algorithm.

Is binary manipulation possible? If not, what would be the best alternative?

The obvious solution would be something like:

int lessThan = (d - floored) > double.Epsilon ? floored : (floored-1);
int moreThan = (ceiled - d) > double.Epsilon ? ceiled : (ceiled+1);

NOTE: The objective is to find out whether d is closer to lessThan or moreThan.

  • 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-09T15:47:08+00:00Added an answer on June 9, 2026 at 3:47 pm

    Since d is always positive, you can use that casting to an integer truncates (ie it’s the floor for positive input and the ceiling for negative input).

    floor(d + 1) is the same as ceil(d) + 1 if integer, ceil(d) otherwise and ceil(d - 1) is the same as floor(d) - 1 if integer, floor(d) otherwise

    int moreThan = (int)(d + 1); // floor(d + 1)
    int lessThan = int.MaxValue + (int)((d - int.MaxValue) - 1) // ceil(d - 1)
    

    The lessThan is somewhat convoluted, I wouldn’t be surprised if someone else has a better idea.

    But since you want this:

    The objective is to find out whether d is closer to lessThan or moreThan

    It should be even simpler:

    double x = d % 1;
    if (x == 0 || x == 0.5)
        // d is equally far from either one, either by a difference of 1 or of 0.5
    else if (x < 0.5)
        // d is closer to lessThan
    else
        // d is closer to moreThan
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say that I would like to get an random double value wich is:
I have the following snippet: #include <boost/random/lognormal_distribution.hpp> #include <boost/random/lagged_fibonacci.hpp> int main() { const double
I would like to initialize boost::random::discrete_distribution with a double[] like this: boost::random::discrete_distribution<>* distribution(double* _distr)
Is it possible to generate a random number between 2 doubles? Example: public double
double d[10]; int length = 10; memset(d, length * sizeof(double), 0); //or for (int
Double has range more than a 64-bit integer, but its precision is less dues
Ok, it is possible to give weights/probabilities in boost::random::discrete_distribution. e.g. double probabilities[] = {
Suppose I wish to have 2 functions, one that generates a random integer within
i want to generate an array of random numbers for example if the range
I'm trying to generate a random Gaussian double in Objective-C (the same as random.nextGaussian

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.