GTKConvert Units.exw with Carpenters Fractions

new topic     » topic index » view thread      » older message » newer message

I "updated" and added Derek Parnell's Carpenters Fraction utility lib to GtkConvertUnits.e file and tweaked the main program so that now when any unit is converted to inches, carpenters fractions are displayed in the entry box and decimal fractions are displayed in the tool-tip. I may have to spread the include file and the main program over two posts.

--  carp.e 
-- derived from decimal or metric units 
-- default entry unit is decimal inches converted to fractional inches 
-- selectable mm_inch, cm_inch, m_inch, cm_ft, m_ft 
-- selectable base/resolution, default can be set to preference 
-- by Derek Parnell, from EuForum 5/6/2012 
-- Update by Kenneth Rhodes: added in_in conversion constant so decimal inches to carpenters fractions 
include std/math.e 
include std/console.e 
export constant mm_in = 1/25.4, cm_in = 1/2.54, m_in =  1/.0254, m_ft  = 1000 / 304.8, cm_ft =  10 / 304.8, in_in = 1 
export function carp( atom x, atom conv = 1, integer base = 32) 
    integer numerator, denominator, units, sign 
    x *= conv  -- Apply any Unit-of-measure conversion 
    if x >= 0 then sign = 1 else sign = -1 x = -x end if -- Remember original sign and ensure working is on absolute value. 
    x = floor(x * base + 0.5) -- Express original value in terms of how many 'base' fragments it is (rounded) 
    numerator = remainder(x, base)  -- Get numerator in range of 0 to base-1 
    units = (x - numerator)/base  denominator = base -- Strip off the non-fraction part 
    -- First, normalize the fraction in terms of GCD, greater common denominatior 
    if numerator > 0 then x = gcd(numerator, denominator) numerator /= x denominator /= x end if 
return {units, numerator, denominator, sign} 
end function 
--  GtkConvertUnits.e, Jim Robert's convert.e updated for EuGTK interface 
-- Measurement Conversion function (second update) 
-- (c2001) Jim Roberts 
--  changes made by Kenneth Rhodes & Irv Mullins(2018): 
--  several new unit conversions added to Energy and Area categories 
--  globals changed to export and category index of the 
--  units  of measure sequence "Unit" 
-- index of categories 
export constant index = {"Length", "Time", "Velocity", "Angles",  "Angular Velocity",  
					  "Area", "Volume",  "Weight", "Pressure", "Energy"} 
export constant Unit = { 
-- Length 
-- Time 
-- Velocity 
-- Angles 
-- Angular velocity 
-- Area 
-- Volume 
    "gal",264.172, "lt",1e3,"pt",2113.38, "qt",1056.6884,"mm3",1e9, 
      "tblsp",67628, "tsp",202884, "floz",33814, "cup", 4166.67 ,"ml",2113}, 
-- Weight 
-- Pressure 
-- Energy 
   {"j",1,"kj", 1/1000, "Nm",1,  "mj", 1e+6, "gcal",4.184, "kcal",0.000239006," erg",1e7, 
   "btu",9.48e-04, "US-therm",  9.4804e-9, "ftlbs",0.73756,"whr",2.778e-4,"kwhr",2.778e-7} 
integer l = length(Unit) 
export function ConvertUnit(atom val, sequence from, sequence To ) 
    integer f, t, i1, i2 
    f = 0 t = 0 
    for a = 1 to l do                 -- indexes from and To units 
	if not f then f = find(from, Unit[a])  i1=a  end if 
	if not t then t = find(  To, Unit[a])  i2=a  end if 
    end for 
    if not f or not t then return {not f, not t, 0} end if   -- Error for unsupported units 
    if i1 != i2  then return {-1, -1, 0}  end if  -- Error for mis-matched units 
    val /= Unit[i1][f+1]  val *= Unit[i1][t+1]   -- converts val to base unit then converts val to To unit 
    return {0, 0, val}  -- return no error and converted value 
end function 
new topic     » topic index » view thread      » older message » newer message


Quick Links

User menu

Not signed in.

Misc Menu