!This file is part of VEL_demo, copyright David Keyes, 2007. ! !This source file is free software: you can redistribute it and/or modify !it under the terms of the GNU General Public License as published by !the Free Software Foundation, either version 3 of the License, or !(at your option) any later version. ! !This code is distributed in the hope that it will be useful, !but WITHOUT ANY WARRANTY; without even the implied warranty of !MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the !GNU General Public License for more details. ! !For terms of the GNU General Public license see . ! decimal inches to imperial ! ! input 13.75 > output 1 1 3 4 ( 1'-1 3/4") ! GLOBAL MODULE dec_imp( FLOAT d_inch; VAR INT feet; VAR INT inches; VAR INT den; VAR INT num); FLOAT sixt, sixa; BEGINMODULE ! initialize the variables to avoid stale bits ! feet:=0; inches:=0; den:=0; num:=0; d_inch:=abs(d_inch); !make positive feet:=trunc(d_inch/12); !Isolate feet inches:=trunc(frac(d_inch/12)*12); !Isolate inches sixt:=frac(frac(d_inch/12)*12)*16; !Isolate 1/16ths sixa:=round(sixt)/16; !normalize 1/16ths if(sixa <> 0) then if trunc(sixa*10000)=sixa*10000 then den:=16; ! If loops for denominator endif; if trunc(sixa*1000)=sixa*1000 then den:=8; endif; if trunc(sixa*100)=sixa*100 then den:=4; endif; if trunc(sixa*10)=sixa*10 then den:=2; endif; if trunc(sixa*1)=sixa*1 then den:=1; endif; if den=1 then inches:=(inches+1); !Its possible to be 1/32 less than 0! den:=0; endif; endif; if den=0 and inches=12 then inches:=0; feet:=feet+1; !roll over endif; num:=trunc(sixa*den); ENDMODULE