Reputation: 3079
I need to convert floats into numbers that resemble measurements from a ruler. For example: 3.75 needs to be converted into 3 and 3/4. However, this is harder than it would seem at first, because I need to keep the denominator in a form that is easily translated into a ruler measurement by a human. Essentially, the denominator should only be powers of 2, up to 16. I don't want a fraction like 3/5 because 5'ths aren't marked on a ruler. I have figured out how to limit the denominator from going above 16, but I can't figure out how to keep the denominator a power of 2.
Answers in python or c++ is preferred.
Upvotes: 1
Views: 139
Reputation: 3079
I did what Jhecht said because it seemed easy to do with python dictionary.
Upvotes: 0
Reputation: 17576
fractions.gcd
, dunno about C++.Upvotes: 2