1. GTKConvert Units.exw with Carpenters Fractions

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 
-- 
-- CARPENTER'S FRACTIONS 
-- 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 
-- 
 
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 
   {"m",1,"cm",100,"mm",1e3,"um",1e6,"km",1e-3,"in",1/.0254,"ft",1/.3048, 
    "yd",1/.9144,"mi",1/1609.344,"fl",4.973e-3,"lg",2.071237307e-4, 
    "au",6.684587154e-12,"ly",1.056980777e-16,"ps",3.24254e-17,"nmi",1/1852}, 
-- Time 
   {"s",1,"min",1/60,"hr",1/3600,"dy",1/86400,"wk",1/604800,"yr",1/31557600}, 
-- Velocity 
   {"m/s",1,"cm/s",100,"mm/s",1e3,"km/s",1e-3,"in/s",1/.0254,"ft/s",1/.3048, 
    "mi/s",1/1609.344,"km/hr",3.6,"mi/hr",1/.44704,"kn",3.6/1.852}, 
-- Angles 
   {"rad",1,"deg",57.29577951,"arcs",2.062648063e5,"arcmin",3437.746771, 
    "gra",63.66197724}, 
-- Angular velocity 
   {"rad/s",1,"deg/s",57.29577951,"deg/min",3437.746771,"rev/s",.1591549431, 
    "rev/min",9.549296586,"rev/hr",572.9577952}, 
-- Area 
   {"m2",1,"cm2",1e4,"mm2",1e6,"km2",1e-6,"in2",1550.0031,"ft2",10.76390841, 
    "yd2",1.195990046,"mi2",3.861021585e-7,"acr",2.471053354e-04,"ha",.0001}, 
-- Volume 
   {"m3",1,"cm3",1e6,"ft3",35.31466672,"in3",61023.74409,"yd3",1.307950618, 
    "gal",264.172, "lt",1e3,"pt",2113.38, "qt",1056.6884,"mm3",1e9, 
      "tblsp",67628, "tsp",202884, "floz",33814, "cup",4166.67, "ml",1000000}, 
-- Weight 
   {"kg",1,"g",1000,"lbs",2.205,"tn",1.1025e-3,"oz",35.28,"mtn",1e-3}, 
-- Pressure 
   {"Pa",1,"N/m2",1,"bar",1e-5,"mbar",1e-2,"N/cm2",1e-4,"dn/cm2",10, 
    "g/cm2",1/98.0665,"kg/cm2",1/98066.5,"atm",1/101325,"lbs/in2",1/6894.8, 
    "lbs/ft2",1/47.88055555,"kg/m2",1/9.80665}, 
-- 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 message » categorize

2. Re: GTKConvert Units.exw with Carpenters Fractions

--# GtkConvertUnits.exw - <b>by Jim Roberts</b> - convert from one unit to another  
--  Gtk interface by Irv Mullins -- Thanks, Irv! 
--  several changes by Kenneth Rhodes including carpenters fractions display for inches  
include GtkEngine.e   
include GtkConvertUnits.e   
 
constant    
    win=create(GtkWindow, "title=Convert by Jim Roberts,border_width=10,position=1,$destroy=Quit"),   
    panel=create(GtkBox, "orientation=HORIZONTAL,spacing=10"),     
    cb1=create(GtkComboBoxText, "tooltip text=Select Category of Unit Measures"),     
    V1=create(GtkEntry, "placeholder text=Value to convert,tooltip text=Enter Value to Convert,$changed=Convert"  ),    
    cb2=create(GtkComboBoxText, "tooltip text = Select Input Unit, $changed=Convert"),   
    cb3=create(GtkComboBoxText, "tooltip text = Select Output Unit,$changed=Convert"), 
    V2=create(GtkEntry,  "width chars=25, placeholder text=Results"  )   
    add(win,panel)  add(panel,{cb1,V1,cb2,cb3,V2})    
    for i = 1 to length(index) do  
        set(cb1,"append text",index[i])  
    end for  
    connect(cb1,"changed","Update")  
show_all(win)   
main()   
  
function SelectUnits(integer i) 
-- returns a sequence of unit labels   
object units = {}  
   for a = 1  to length(Unit[i]) by 2 do   
      units = append(units, Unit[i][a])    
   end for   
return  units 
end function   
  
---------------------------  
global function Update()  
---------------------------  
integer u = get(cb1,"active")  
object units = SelectUnits(u)  
   set(cb2,"remove all")  set(cb3,"remove all")  
   for j = 1 to length(units) do   
      set(cb2,"append text",units[j]) set(cb3,"append text",units[j])  
   end for  
   set(cb2,"active",1) set(cb3,"active",1)  
return 1   
end function   
  
global function Convert()  
----------------------------  
atom val = to_number(get(V1,"text"))    
object from_units = get(cb2,"active text")  
object to_units = get(cb3,"active text") 
object c ={} 
  if val = 0 then return 1 end if  
  if atom(from_units) or atom(to_units) then return 1 end if  
  object results = ConvertUnit(val,from_units, to_units)  
  results[1] = from_units results[2] = to_units results = append(results,val) 
  if match( "in", to_units) then 
      c= carp( results[3], in_in) c = prepend(c, from_units )  
      c = prepend(c, val)  
      c =sprintf("%g %s = %g %g/%g inches", {c[1],c[2],c[3],c[4],c[5]})  
      set(V2,"text", c)  
      set(V2,"tooltip markup",text:format("[4] [1] = [3] [2]",results)) 
  else 
      set(V2,"tooltip markup",text:format("[4] [1] = [3] [2]",results))  
      results[3] = sprintf("%.4g",results[3])   
      set(V2, "text", text:format("[4] [1] = [3] [2]",results))  
   end if 
return 1  
end function   
 
 
new topic     » goto parent     » topic index » view message » categorize

3. Re: GTKConvert Units.exw with Carpenters Fractions

Very cool!

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu