RE: [Clip] Re: Use of 'IfInRange'
To find the nearest fraction is just an extension of the normal technique for
rounding: adding .5 to the number and truncating.
In your case, extract the decimal portion (x), multiply by 16, add .5 and then
Int(x * 16 + 0.5)
The result will be the nearest 16th to your original number.
This is similar to the fence post problem in that there are 17 possible results
for the 16 intervals: 0 thru 16. I would create a 17 element static array
the strings that you want to display for each value (such as "1/16", "1/8",
"3/16", "1/4", etc), the 17th one being blank. Use the result+1 to index
into the array. You would only need to test for when the result equals 16 in
which case you need to add one to the original integer.
This should be faster than a lot of tests for 16 conditions. [Remember to turn
screen update off <g>]
At 03/10/2011 05:59, John wrote:
>Consider how you'd handle converting decimals to fractions, when you are
>restricted to fractions of 1/16 or larger. Only /16, /8 /4 /3 and /2 are
>permitted. When a decimal falls between the natural break between these
>fractions, what is the best way to determine which fraction should be used?
>This is actually the problem I was trying to resolve in a better way than my
>current kludge, which uses classes, but isn't accurate for numbers between
>15/16 and the next whole number. If the decimal is closer to 15/16 than the
>next whole number, I'd choose the fraction, but when it's closer to the
>whole number, I'd want to round up to the whole number. After I played
>around with it for a few hours, I decided I need a different approach, and
>thought perhaps IfInRange would work better.