Re: Converting decimal number to carpenter's fractions

new topic     » goto parent     » topic index » view thread      » older message » newer message
K_D_R said...

Does anyone have, or can recommend a routine to convert decimals to carpenters inch fractions: 32nds, 16ths, 8ths, 4ths, halfs? I have a list of equivalents so I guess If there is no exact match, I would need to compare up the list until a higher value is reached and then subtract my search value from the larger value and subtract the previous lower value from the decimal value I am seeking to convert and then select the value which has the lowest difference.

sequence decimal_fractions { 
{.03125, 1, 32}, 
{.06250, 1, 16}, 
{.09375, 3, 32}, 
 ..... 
{.96875, 31, 32} 
 
 
 
x = 0.07356   
0.09375 - 0.07356 = 0.02019 
0.07356 - 0.06250 = 0.01106 x = {1,16}  or 1/16th inch 

Seems like there must be an easier way, though.

Any suggestions would be appreciated.

I've used something like this

-- minimum fraction default 1/64th 
-- needs more work if want to find the nearest fraction over or under. 
-- might be nice to return the amount over if any. 
-- 
public function dec_frac(atom x1, atom minfraction = 0.015625) 
	atom numerator = 1, denominator = 2, mf = 64 
	x1 += 0.00002 
	if minfraction < 1 then 
		mf = floor(math:abs(1/minfraction)) 
	end if 
 
    for r = numerator to mf - 1 do 
      if abs(x1 - (r/mf)) <= minfraction  then 
    	numerator = r  
    	goto "done" 
      end if 
    end for  
 
    return {}	--error 
	label "done" 
 
	--simplity 
	return {numerator/math:gcd(numerator, mf) 
			, mf/math:gcd(numerator, mf)} 
end function 
 
 
 test_equal("dec_frac 1/32",{1,32}, trig:dec_frac(1/32)) 
 test_equal("dec_frac 1/16",{1,16}, trig:dec_frac(1/16)) 
 test_equal("dec_frac 3/32",{3,32}, trig:dec_frac(.094)) 
 test_equal("dec_frac 1/4",{1,4}, trig:dec_frac(0.25)) 
 test_equal("dec_frac 5/8",{5,8}, trig:dec_frac(0.625)) 
 test_equal("dec_frac 7mm",{17,64}, trig:dec_frac(0.2756)) 
 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu