math - Algorithm to find a common multiplier to convert decimal numbers to whole numbers -


i have array of numbers potentially have 8 decimal places , need find smallest common number can multiply them whole numbers. need original numbers can multiplied out same scale , processed sealed system deal whole numbers, can retrieve results , divide them common multiplier relative results.

currently few checks on numbers , multiply 100 or 1,000,000, processing done *sealed system can quite expensive when dealing large numbers multiplying million sake of isn’t great option. approximation lets sealed algorithm gets 10 times more expensive every time multiply factor of 10.

what efficient algorithm, give best possible result, accomplish need , there mathematical name and/or formula i’m need?

*the sealed system isn’t sealed. own/maintain source code 100,000 odd lines of proprietary magic , has been thoroughly bug , performance tested, altering deal floats not option many reasons. system creates grid of x y cells, rects x y dropped grid, “proprietary magic” occurs , results spat out – extremely simplified version of reality, it’s enough approximation.

so far there quiet few answers , wondered how should go choosing ‘correct’ one. begin figured fair way create each solution , performance test it, later realised pure speed wasn’t relevant factor – more accurate solution relevant. wrote performance tests anyway, i’m choosing correct answer based on speed accuracy using ‘gut feel’ formula.

my performance tests process 1000 different sets of 100 randomly generated numbers. each algorithm tested using same set of random numbers. algorithms written in .net 3.5 (although far 2.0 compatible) tried pretty hard make tests fair possible.

  • greg – multiply large number , divide gcd – 63 milliseconds
  • andy – string parsing – 199 milliseconds
  • eric – decimal.getbits – 160 milliseconds
  • eric – binary search – 32 milliseconds
  • ima – sorry couldn’t figure out how implement solution in .net (i didn’t want spend long on it)
  • bill – figure answer pretty close greg’s didn’t implement it. i’m sure it’d smidge faster potentially less accurate.

so greg’s multiply large number , divide gcd” solution second fastest algorithm , gave accurate results i’m calling correct.

i wanted decimal.getbits solution fastest, slow, i’m unsure if due conversion of double decimal or bit masking , shifting. there should similar usable solution straight double using bitconverter.getbytes , knowledge contained here: 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 eyes kept glazing on every time read article , ran out of time try implement solution.

i’m open other solutions if can think of better.

i'd multiply sufficiently large (100,000,000 8 decimal places), divide gcd of resulting numbers. you'll end pile of smallest integers can feed other algorithm. after getting result, reverse process recover original range.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -