<p>I have an array of numbers that potentially have up to 8 decimal places and I need to find the smallest common number I can multiply them by so that they are all whole numbers. I need this so all the original numbers can all be multiplied out to the same scale and be processed by a sealed system that will only deal with whole numbers, then I can retrieve the results and divide them by the common multiplier to get my relative results.</p>

<p>Currently we do a few checks on the numbers and multiply by 100 or 1,000,000, but the processing done by the *sealed system can get quite expensive when dealing with large numbers so multiplying everything by a million just for the sake of it isn’t really a great option. As an approximation lets say that the sealed algorithm gets 10 times more expensive every time you multiply by a factor of 10.</p>

<p>What is the most efficient algorithm, that will also give the best possible result, to accomplish what I need and is there a mathematical name and/or formula for what I’m need?</p>

<p>*The sealed system isn’t really sealed. I own/maintain the source code for it but its 100,000 odd lines of proprietary magic and it has been thoroughly bug and performance tested, altering it to deal with floats is not an option for many reasons. It is a system that creates a grid of X by Y cells, then rects that are X by Y are dropped into the grid, “proprietary magic” occurs and results are spat out – obviously this is an extremely simplified version of reality, but it’s a good enough approximation.</p>

<p>So far there are quiet a few good answers and I wondered how I should go about choosing the ‘correct’ one. To begin with I figured the only fair way was to create each solution and performance test it, but I later realised that pure speed wasn’t the only relevant factor – an more accurate solution is also very relevant. I wrote the performance tests anyway, but currently the I’m choosing the correct answer based on speed as well accuracy using a ‘gut feel’ formula.</p>

<p>My performance tests process 1000 different sets of 100 randomly generated numbers. 
Each algorithm is tested using the same set of random numbers.
Algorithms are written in .Net 3.5 (although thus far would be 2.0 compatible)
I tried pretty hard to make the tests as fair as possible.</p>

<ul>
<li>Greg – Multiply by large number
and then divide by GCD – 63
milliseconds</li>
<li>Andy – String Parsing
– 199 milliseconds </li>
<li>Eric – Decimal.GetBits – 160 milliseconds</li>
<li>Eric – Binary search – 32
milliseconds </li>
<li>Ima – sorry I couldn’t
figure out a how to implement your
solution easily in .Net (I didn’t
want to spend too long on it) </li>
<li>Bill – I figure your answer was pretty
close to Greg’s so didn’t implement
it. I’m sure it’d be a smidge faster
but potentially less accurate.</li>
</ul>

<p>So Greg’s Multiply by large number and then divide by GCD” solution was the second fastest algorithm and it gave the most accurate results so for now I’m calling it correct.</p>

<p>I really wanted the Decimal.GetBits solution to be the fastest, but it was very slow, I’m unsure if this is due to the conversion of a Double to a Decimal or the Bit masking and shifting. There should be a 
similar usable solution for a straight Double using the BitConverter.GetBytes and some knowledge contained here: <a href="http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/bclteam/archive/2007/05/29/bcl-refresher-floating-point-types-the-good-the-bad-and-the-ugly-inbar-gazit-matthew-greig.aspx</a> but my eyes just kept glazing over every time I read that article and I eventually ran out of time to try to implement a solution.</p>

<p>I’m always open to other solutions if anyone can think of something better.</p>

[{'answer_id': 58497, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 4, 'selected': True, 'text': '<p>I\'d multiply by something sufficiently large (100,000,000 for 8 decimal places), then divide by the <a href="http://en.wikipedia.org/wiki/Greatest_common_divisor" rel="nofollow noreferrer">GCD</a> of the resulting numbers. You\'ll end up with a pile of smallest integers that you can feed to the other algorithm. After getting the result, reverse the process to recover your original range.</p>\n'}, {'answer_id': 58512, 'author': 'Evil Andy', 'author_id': 4431, 'author_profile': 'https://Stackoverflow.com/users/4431', 'pm_score': 0, 'selected': False, 'text': '<p>What language are you programming in? Something like</p>\n\n<pre><code>myNumber.ToString().Substring(myNumber.ToString().IndexOf(".")+1).Length\n</code></pre>\n\n<p>would give you the number of decimal places for a double in C#. You could run each number through that and find the largest number of decimal places(x), then multiply each number by 10 to the power of x.</p>\n\n<p>Edit: Out of curiosity, what is this sealed system which you can pass only integers to?</p>\n'}, {'answer_id': 58532, 'author': 'Scott', 'author_id': 6042, 'author_profile': 'https://Stackoverflow.com/users/6042', 'pm_score': 0, 'selected': False, 'text': "<p>Greg: Nice solution but won't calculating a GCD that's common in an array of 100+ numbers get a bit expensive? And how would you go about that? Its easy to do GCD for two numbers but for 100 it becomes more complex (I think).</p>\n\n<p>Evil Andy: I'm programing in .Net and the solution you pose is pretty much a match for what we do now. I didn't want to include it in my original question cause I was hoping for some outside the box (or my box anyway) thinking and I didn't want to taint peoples answers with a potential solution. While I don't have any solid performance statistics (because I haven't had any other method to compare it against) I know the string parsing would be relatively expensive and I figured a purely mathematical solution could potentially be more efficient.\nTo be fair the current string parsing solution is in production and there have been no complaints about its performance yet (its even in production in a separate system in a VB6 format and no complaints there either). It's just that it doesn't feel right, I guess it offends my programing sensibilities - but it may well be the best solution.</p>\n\n<p>That said I'm still open to any other solutions, purely mathematical or otherwise.</p>\n"}, {'answer_id': 58657, 'author': 'ima', 'author_id': 5733, 'author_profile': 'https://Stackoverflow.com/users/5733', 'pm_score': 0, 'selected': False, 'text': '<p>In a loop get mantissa and exponent of each number as integers. You can use frexp for exponent, but I think bit mask will be required for mantissa. Find minimal exponent. Find most significant digits in mantissa (loop through bits looking for last "1") - or simply use predefined number of significant digits.\nYour multiple is then something like 2^(numberOfDigits-minMantissa). "Something like" because I don\'t remember biases/offsets/ranges, but I think idea is clear enough.</p>\n'}, {'answer_id': 58713, 'author': 'Tyler', 'author_id': 3561, 'author_profile': 'https://Stackoverflow.com/users/3561', 'pm_score': 1, 'selected': False, 'text': '<p>If you want to find some integer N so that N*x is also an exact integer for a set of floats x in a given set are all integers, then you have a basically unsolvable problem.  Suppose x = the smallest positive float your type can represent, say it\'s 10^-30.  If you multiply all your numbers by 10^30, and then try to represent them in binary (otherwise, why are you even trying so hard to make them ints?), then you\'ll lose basically all the information of the other numbers due to overflow.</p>\n\n<p>So here are two suggestions:</p>\n\n<ol>\n<li>If you have control over all the related code, find another\napproach.  For example, if you have some function that takes only\nint\'s, but you have floats, and you want to stuff your floats into\nthe function, just re-write or overload this function to accept\nfloats as well.</li>\n<li>If you don\'t have control over the part of your system that requires\nint\'s, then choose a precision to which you care about, accept that\nyou will simply have to lose some information sometimes (but it will\nalways be "small" in some sense), and then just multiply all your\nfloat\'s by that constant, and round to the nearest integer.</li>\n</ol>\n\n<p>By the way, if you\'re dealing with fractions, rather than float\'s, then it\'s a different game.  If you have a bunch of fractions a/b, c/d, e/f; and you want a least common multiplier N such that N*(each fraction) = an integer, then N = a<em>b</em>c / gcd(a,b,c); and gcd(a,b,c) = gcd(a, gcd(b, c)).  You can use <a href="http://en.wikipedia.org/wiki/Euclidean_algorithm" rel="nofollow noreferrer">Euclid\'s algorithm</a> to find the gcd of any two numbers.</p>\n'}, {'answer_id': 60085, 'author': 'Eric', 'author_id': 4540, 'author_profile': 'https://Stackoverflow.com/users/4540', 'pm_score': 0, 'selected': False, 'text': '<p>So basically you want to determine the number of digits after the decimal point for each number.</p>\n\n<p>This would be rather easier if you had the binary representation of the number.  Are the numbers being converted from rationals or scientific notation earlier in your program?  If so, you could skip the earlier conversion and have a much easier time.  Otherwise you might want to pass each number to a function in an external DLL written in C, where you could work with the floating point representation directly.  Or you could cast the numbers to decimal and do some work with <a href="http://msdn.microsoft.com/en-us/library/system.decimal.getbits(VS.71).aspx" rel="nofollow noreferrer">Decimal.GetBits</a>.</p>\n\n<p>The fastest approach I can think of in-place and following your conditions would be to find the smallest necessary power-of-ten (or 2, or whatever) as suggested before.  But instead of doing it in a loop, save some computation by doing binary search on the possible powers.  Assuming a maximum of 8, something like:</p>\n\n<pre><code>int NumDecimals( double d )\n{\n   // make d positive for clarity; it won\'t change the result\n   if( d&lt;0 ) d=-d;\n\n   // now do binary search on the possible numbers of post-decimal digits to \n   // determine the actual number as quickly as possible:\n\n   if( NeedsMore( d, 10e4 ) )\n   {\n      // more than 4 decimals\n      if( NeedsMore( d, 10e6 ) )\n      {\n          // &gt; 6 decimal places\n          if( NeedsMore( d, 10e7 ) ) return 10e8;\n          return 10e7;\n      }\n      else\n      {\n         // &lt;= 6 decimal places\n         if( NeedsMore( d, 10e5 ) ) return 10e6;\n         return 10e5;\n      }\n   }\n   else\n   {\n      // &lt;= 4 decimal places\n      // etc...\n   }\n\n}\n\nbool NeedsMore( double d, double e )\n{\n   // check whether the representation of D has more decimal points than the \n   // power of 10 represented in e.\n   return (d*e - Math.Floor( d*e )) &gt; 0;\n}\n</code></pre>\n\n<p>PS: you wouldn\'t be passing security prices to an option pricing engine would you?  It has exactly the flavor...</p>\n'}, {'answer_id': 60126, 'author': 'Douglas Leeder', 'author_id': 3978, 'author_profile': 'https://Stackoverflow.com/users/3978', 'pm_score': 1, 'selected': False, 'text': "<ol>\n<li>Multiple all the numbers by 10\nuntil you have integers.</li>\n<li>Divide\nby 2,3,5,7 while you still have all\nintegers.</li>\n</ol>\n\n<p>I think that covers all cases.</p>\n\n<pre><code>2.1 * 10/7 -&gt; 3\n0.008 * 10^3/2^3 -&gt; 1\n</code></pre>\n\n<p>That's assuming your multiplier can be a rational fraction.</p>\n"}]
['https://Stackoverflow.com/questions/58493', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6042/']