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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:44:38+00:00 2026-05-12T09:44:38+00:00

I have a difficult mathematical question that is breaking my brain, my whiteboard, and

  • 0

I have a difficult mathematical question that is breaking my brain, my whiteboard, and all my pens. I am working with a file that expresses 2 values, a multiplicand and a percentage. Both of those values must be integers. These two values are multiplied together to produce a range. Range is a float value.

My users edit the range, and I have to calculate a new percentage and multiplicand value. Confused yet? Here’s an example:

    Multiplicand: 25000 Apples
    Percentage: 400 (This works out to .4% or .004)
    Range: 100.0 Apples (Calculated by Multiplicand * Percentage)

To complicate things, the allowable values for Percentage are 0-100000. (Meaning 0-100%) Multiplicand is a value between 1 and 32bit int max (presumably unsigned).

I need to allow for users to input a range, like so:

Range: .04 Apples

And calculate the appropriate Percentage and Multiplicand. Using the first example:

    OriginalMultiplicand: 25000 Apples
    OriginalPercentage: 400 (This works out to .4% or .004)
    OriginalRange: 100.0 Apples (Calculated by Multiplicand * Percentage)
    NewRange: .01 Apples
    NewPercentage: 40
    NewMultiplicand: 25 Apples

The example calculation is easy, all that was required was adjusting down the multiplicand and percentage down by the scale factor of the new and old range. The problem arises when the user changes the value to something like 1400.00555. Suddenly I don’t have a clean way to adjust the two values.

I need an algorithmic approach to getting values for M & P that produce the closest possible value to the desired range. Any suggestions?

  • 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-12T09:44:38+00:00Added an answer on May 12, 2026 at 9:44 am

    To maximize the numbers of decimal points stored, you should use a P of 1, or 0.1%. If that overflows M, then increment P.

    So for your example of 1400.00555, P is 1 and M is 1400006

    Your algorithm would search for the lowest P such that M does not overflow. And you can do a binary search here.

    public int binarySearch(int P0, int P1) {
       P = (P1 - P0)/2;
       if(P == P0) {
         if(R/(P0/100f) does not overflows 32-bit int) {
           return P0;
         } else {
           return P1;
         }
       }
       if(R/(P/100f) does not overflows 32-bit int) {
         return binarySearch(P0, P);
       } else {
         return binarSearch(P, P1);
       }
    }
    
    P = binarySearch(1, 100000);
    M = round(R/(P/100f));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a feeling that this should really not be all that difficult, yet
I'm not sure it's an acceptabale question for Stackoverflow but I have difficult time
I have a problem that is kind of difficult to explain. What I would
Hi I have a difficult regex problem that I have tried and have a
I need to obtain all the posts that have the same tag. But I
i have this function in jquery that verify if ALL the fields have at
I have a website that presents Q&As to mathematical problems, mostly for pupils aged
I am working on in YUI framework with my client. I have a difficult
I have an oddly difficult task to perform. I thought it would be easy,
Ever since I gained experience at a software house I have found it difficult

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.