0

I am trying to find the multiplier for a fraction that will let me get a whole number.

So trying to solve $c = a \times b$

Where $a$ is a number like $1.6$ or $0.7$ or $5.24$

Where $b$ is the lowest number that $a$ can be multiplied by to make $c$ a whole number.

The use case is in a game I am programming the currency is only in whole numbers of a single denomination (single gold coins) if the player wants to sell a quantity of items that are worth a fractional value like $1.6$, how many must they sell so they can receive a whole number without throwing out the fractions.

  • Aside: while $1.6$ is a rational number, it isn't a fraction. (some fractions representing this number are $16/10$ and $8/5$) –  Jul 03 '17 at 20:26
  • Then fraction is the wrong word, I am working in code so I don't work with proper fractions like 1 and 8/5 but with floating point numbers so my frame of reference for the question is a little bit skewed. Feel free to offer a suggestion if you think it will make the question clearer. – Shawn Sagady Jul 03 '17 at 20:30
  • 1
    Also, you might want to visit the game programming site for advice on how to handle your actual problem. As someone with lots of game playing experience (and thus, lots of experience with all the ways developers mess this up), I strongly suggest that you'll save yourself a lot of trouble if you remove the restriction that currency is only whole numbers. –  Jul 03 '17 at 20:31
  • 1
    Working in code is no excuse for not working with proper fractions -- your language's floating point type is not your only option for storing numbers! A number of languages, for example, have rational types, and even in those that don't you can still make structures to exactly hold the value, whether building your own rational type or instead storing alternative data that is represented exactly by integers: e.g. rather than storing the value of a commodity as $1.6$, you instead store it in a struct that says you can only sell in batches of $5$, for which you get $8$ gold. –  Jul 03 '17 at 20:38
  • You are totally correct in all your suggestions but I am also constrained by other factors that require me to work with this set of variables. Were it my own project I would certainly consider alternatives. – Shawn Sagady Jul 03 '17 at 20:42

3 Answers3

2

Multiply $a = \dfrac 85$ by $b = 5$ to get $$c= \frac 85\cdot 5 = 8$$

When a rational number is expressed as a fraction $\dfrac nd$ that is fully reduced meaning $\gcd(n, d)=1$, with (n, numerator; d, denominator, each an integer,) then we have $$a = \frac nd\quad \text{ so we put }\; b = d,$$ and so $$c = \frac nd\cdot d = n$$

That is, any rational number expressed as a fully reduced fraction where the numerator and denominator are co-prime, then the lowest integer value for $b$ is given by $bd$, where $d$ is the denominator of the fully reduced fraction = $a$

amWhy
  • 209,954
0

I assume you mean to ask the following question:

Given a rational number $a$, what is the smallest integer value of $b$ for which $ab$ is an integer

The solution is simple: if you write $a$ as a fraction in lowest terms, then $b$ is the denominator.

  • This is also a great answer and very straight forward, but amWhy really breaks it down mathematically and why it works. Thanks so much for all the input! – Shawn Sagady Jul 04 '17 at 04:01
0

I'd suggest this algorithm: multiply by $10$ repeatedly until you have an integer. You can now write your decimal as a fraction: $\frac{k}{10^n}$. Then, use any factorization algorithm to find the greatest common divisor of $k$ and $10^n$, and divide both the numerator and denominator by that. This puts the fraction in lowest terms, and now the denominator is the number you wanted.

For the record: This is a horribly inefficient algorithm. If you need it to be fast (for example, if you're trying to do billions of these calculations) you'll want to do something else. But it'll work if you're only trying to do it a hundred times a second, or something like that.

  • Ive seen this method suggested before (very brute force) unfortunately it can result in pretty long hangs (multiple miliseconds) which can mean a lot when you are trying to get work done at 140fps without hitches. – Shawn Sagady Jul 04 '17 at 04:03
  • @ShawnSagady You're trying to calculate new values more than a hundred times a second? Is the situation actually changing that fast, or could you cache the values? Otherwise, you'll need to know something about the floating point values in question (for example, were they produced by dividing by $10$, etc.). – Reese Johnston Jul 04 '17 at 04:58
  • No just a lot of other overhead going on with rendering or something so taking up multiple milliseconds can get nasty. But I've found a solution that is working really well with an approximation and a custom gcd function. I'll post up my pseudo code solution in comments when I get a chance in case others are curious. – Shawn Sagady Jul 04 '17 at 17:40