Changeset 447

Show
Ignore:
Timestamp:
10/14/08 23:56:55 (1 month ago)
Author:
BCS
Message:

added types.d with generic types (Length/Mass/Charge/etc.)
converted code to use types.d where possible
switched gcd to CTFE
added and more units to si.d
added 'unity' version of unit sources.
fixed bug in unit.d re opDiv
added op*_r's and opMul/Div for non real basic types.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/units/constants.d

    r441 r447  
    22 * Constant values with units 
    33 */ 
    4 module constanst
     4module constants
    55 
    66import si; 
    77import unit; 
    8  
    9 template Direct(int l, int m, int t, int T, int I) 
    10 
    11     alias Unit!(l,1,m,1,t,1,T,1,I,1) Direct; 
    12 
     8import types; 
    139 
    1410/// math constants 
     
    1612{ 
    1713    /// Euler's constant 
    18     static const SI.Value gamma = { value : 0.577215664901532 }; 
     14    static const Types.Value gamma = { value : 0.577215664901532 }; 
    1915    /// good old pi 
    20     static const SI.Value pi = { value : 3.14159265359}; 
     16    static const Types.Value pi = { value : 3.14159265359}; 
    2117    /// natural log base 
    22     static const SI.Value e = { value : 2.718281828459 }; 
     18    static const Types.Value e = { value : 2.718281828459 }; 
    2319    /// golden ratio 
    24     static const SI.Value phi = { value : 1.6180339887498948482045868 }; 
     20    static const Types.Value phi = { value : 1.6180339887498948482045868 }; 
    2521} 
    2622 
     
    2925{ 
    3026    /// unified atomic mass 
    31     static const SI.Kilogram u = { value : 1.66053873e-27 }; 
     27    static const Types.Mass u = { value : 1.66053873e-27 }; 
    3228    /// electron mass 
    33     static const SI.Kilogram me = { value : 9.109373815276e-31 }; 
     29    static const Types.Mass me = { value : 9.109373815276e-31 }; 
    3430    /// electron charge 
    35     static const Direct!(0,0,1,0,1) e = { value : 1.602176462e-16 }; 
     31    static const Types.Charge e = { value : 1.602176462e-16 }; 
    3632    /// Proton mass 
    37     static const SI.Kilogram mp = { value : 1.67262158e-27 }; 
     33    static const Types.Mass mp = { value : 1.67262158e-27 }; 
    3834    /// Neutron mass 
    39     static const SI.Kilogram mn = { value : 1.67492716e-27 }; 
     35    static const Types.Mass mn = { value : 1.67492716e-27 }; 
    4036     
    4137} 
     
    4541{ 
    4642    /// Boltzmann constant 
    47     static const Direct!(2,1,-2,-1,0) k = { value : 1.3806503e-23 }; 
     43    static const Lit!(2,1,-2,-1,0) k = { value : 1.3806503e-23 }; 
    4844    /// electric field constant / permittivity of free space 
    49     static const Direct!(-3,-1,4,0,2) e = { value : 8.854187817e-12}; 
     45    static const Lit!(-3,-1,4,0,2) e = { value : 8.854187817e-12}; 
    5046    /// Gravitational Constant 
    51     static const Direct!(3,-1,-2,0,0) G = { value : 6.67259 }; 
     47    static const Lit!(3,-1,-2,0,0) G = { value : 6.67259 }; 
    5248    /// Impedance of free space 
    53     static const Direct!(2,1,-3,0,-2) Z = { value : 376.731}; 
     49    static const Lit!(2,1,-3,0,-2) Z = { value : 376.731}; 
    5450    /// speed of light 
    55     static const Direct!(1,0,-1,0,0) c = { value : 2.99792458e8 }; 
     51    static const Types.Speed c = { value : 2.99792458e8 }; 
    5652    /// magnetic field constant / Permeability of a vacuum 
    57     static const Direct!(1,1,-2,0,-1) mu = { value : 1.2566370614 }; 
     53    static const Lit!(1,1,-2,0,-1) mu = { value : 1.2566370614 }; 
    5854    /// Planck constant 
    59     static const Direct!(2,1,-1,0,0) h = { value : 6.62606876e-34 }; 
     55    static const Lit!(2,1,-1,0,0) h = { value : 6.62606876e-34 }; 
    6056    /// Stefan-Boltzmann constant 
    61     static const Direct!(4,-3,1,-4,0) sigma = { value : 5.670400e-8 }; 
     57    static const Lit!(4,-3,1,-4,0) sigma = { value : 5.670400e-8 }; 
    6258     
    6359} 
     
    6763{ 
    6864    /// Astronomical unit 
    69     static const SI.Meter AU = { value :149.59787e11 }; 
     65    static const Types.Distance AU = { value :149.59787e11 }; 
    7066} 
  • trunk/units/rational.d

    r437 r447  
    33template Tpl(T...){alias T Tpl;} 
    44 
    5 template GCD_(uint n, uint d) 
     5uint gcd(int n, uint d) 
    66{ 
    7     static if(d != 0) 
    8         const uint GCD_ = GCD_!(d, n%d); 
    9     else 
    10         const uint GCD_ = n; 
     7    if(n < 0) n = -n; 
     8    while(d) 
     9    { 
     10        int t = d; 
     11        d = n%d; 
     12        n = t; 
     13    } 
     14    return n; 
    1115} 
    12  
    13 static assert(7 == GCD_!(14,21)); 
    14  
    15 template GCD(int n, uint d) 
    16 
    17     //pragma(msg,n.stringof~"/"~d.stringof); 
    18     static assert(d != 0); 
    19     static if(n < 0) 
    20         const uint GCD = GCD!(-n,d); 
    21     else 
    22         const uint GCD = GCD_!(cast(uint)n,d); 
    23 
    24  
     16static assert(7 == gcd(14,21)); 
    2517 
    2618template Reduce(int n, uint d) 
     
    3022    { 
    3123        //pragma(msg,n.stringof~"/"~d.stringof); 
    32         const int N =  n / cast(int)GCD!(n,d); 
    33         const int D = cast(uint)(d / GCD!(n,d)); 
    34         const bool Reduced = (GCD!(n,d) == 1); 
     24        const int N =  n / cast(int)gcd(n,d); 
     25        const int D = cast(uint)(d / gcd(n,d)); 
     26        const bool Reduced = (gcd(n,d) == 1); 
    3527    } 
    3628    else 
  • trunk/units/si.d

    r440 r447  
    2121    /// unit encoder: stuff the value in the correct type with convertions  
    2222    const char[] Make = 
    23         "static "~type~name~"(real val)"~ 
     23        "static "~type~name~"(real val)" 
    2424        "{"~ 
    25             type~" ret;"~ 
    26             "ret._rawSet(val*"~v.stringof~");"~ 
    27             "return ret;"~ 
    28         "}"; 
     25            type~" ret;" 
     26            "ret._rawSet(val*"~v.stringof~");" 
     27            "return ret;" 
     28        "}" 
     29        "static "~type~name~"()" 
     30        "{"~ 
     31            type~" ret;" 
     32            "ret._rawSet("~v.stringof~");" 
     33            "return ret;" 
     34        "}" 
     35        \n; 
    2936     
    3037    /// unit decoder: if the unit classes match, create a function that extracts, converts and returns the value. 
    3138    const char[] From =  
    32         "static if(is(T == "~type~"))"~ 
    33         "{"~ 
    34             "real "~name~"()"~ 
    35             "{"~ 
    36                 "return t._rawGet()/"~v.stringof~";"~ 
    37             "}"~ 
    38         "}"; 
     39        "static if(is(T == "~type~"))" 
     40        "{" 
     41            "real "~name~"()" 
     42            "{" 
     43                "return t._rawGet()/"~v.stringof~";" 
     44            "}" 
     45        "}" 
     46        \n; 
    3947    //pragma(msg,From); 
    4048} 
     
    4654        const BuildSet = ""; 
    4755    else static if (T.length == 1) 
    48         const BuildSet = `mixin(Option!(`~T[0]~`).`~suf~`);`
     56        const BuildSet = (mixin(`Option!(`~T[0]~`).`~suf))
    4957    else 
    5058        const BuildSet = BuildSet!(suf,T[0..$/2]) ~ BuildSet!(suf,T[$/2..$]); 
     
    5361/// The set of unit defs 
    5462alias Tpl!(  
    55     `"meter",1,1,0,0,0,0`, 
    56     `"kilogram",1,0,1,0,0,0`, 
    57     `"second",1,0,0,1,0,0`, 
    58     `"ampere",1,0,0,0,0,1`, 
    59     `"kelvin",1,0,0,0,1,0`, 
     63    `"meter",    1.0, 1,0,0,0,0`, 
     64    `"kilogram", 1.0, 0,1,0,0,0`, 
     65    `"second",   1.0, 0,0,1,0,0`, 
     66    `"kelvin",   1.0, 0,0,0,1,0`, 
     67    `"ampere",   1.0, 0,0,0,0,1`, 
    6068     
    6169    /// Distance Unit Types 
    62     `"Angstrom",1e-10,1,0,0,0,0`, 
    63     `"Micron",1e-6,1,0,0,0,0`, 
    64     `"mm",1e-3,1,0,0,0,0`, 
    65     `"cm",1e-2,1,0,0,0,0`, 
    66     `"km",1e3,1,0,0,0,0`, 
    67     `"inch",2.54e-2,1,0,0,0,0`, 
    68     `"foot",3.04799835e-1,1,0,0,0,0`, 
    69     `"yard",9.14399506e-1,1,0,0,0,0`, 
    70     `"mile",1.609343130e3,1,0,0,0,0`, 
    71     `"parsec",3.085677473598e13,1,0,0,0,0`, 
     70    `"Angstrom", 1e-10,             1,0,0,0,0`, 
     71    `"Micron",   1e-6,              1,0,0,0,0`, 
     72    `"mm",       1e-3,              1,0,0,0,0`, 
     73    `"cm",       1e-2,              1,0,0,0,0`, 
     74    `"km",       1e3,               1,0,0,0,0`, 
     75    `"inch",     2.54e-2,           1,0,0,0,0`, 
     76    `"foot",     3.04799835e-1,     1,0,0,0,0`, 
     77    `"yard",     9.14399506e-1,     1,0,0,0,0`, 
     78    `"mile",     1.609343130e3,     1,0,0,0,0`, 
     79    `"parsec",   3.085677473598e13, 1,0,0,0,0`, 
    7280     
    7381    /// Mass Unit Types 
    74     `"gram",1e-3,0,1,0,0,0`, 
    75     `"lb",4.5359e-1,0,1,0,0,0`, 
    76     `"Ounce",2.83495e-2,0,1,0,0,0`, 
     82    `"gram",  1e-3,       0,1,0,0,0`, 
     83    `"lb",    4.5359e-1,  0,1,0,0,0`, 
     84    `"Ounce", 2.83495e-2, 0,1,0,0,0`, 
    7785     
    7886    /// Time Unit Types 
    79     `"minute",60,0,0,1,0,0`, 
    80     `"hour",3600,0,0,1,0,0`, 
    81     `"day",86400,0,0,1,0,0`, 
     87    `"minute", 60.0,    0,0,1,0,0`, 
     88    `"hour",   3600.0,  0,0,1,0,0`, 
     89    `"day",    86400.0, 0,0,1,0,0`, 
    8290     
    8391    /// Temperature Unit Types 
    84     `"R",0.5555555555,0,0,0,1,0`, 
     92    `"R", 0.5555555555, 0,0,0,1,0`, 
    8593     
    8694    /// Force  Unit Types 
    87     `"Newton",1,1,1,-2,0,0`, 
    88     `"dyne",1e-5,1,1,-2,0,0`, 
    89     `"lbf",4.44822246806,1,1,-2,0,0`, 
     95    `"Newton", 1.0,           1,1,-2,0,0`, 
     96    `"dyne",   1e-5,          1,1,-2,0,0`, 
     97    `"lbf",    4.44822246806, 1,1,-2,0,0`, 
    9098     
    9199    /// Charge Unit Types 
    92     `"Coulomb",1,0,0,1,0,1`, 
     100    `"Coulomb", 1.0, 0,0,1,0,1`, 
     101     
     102    /// Magnetic flux density 
     103    `"T", 1.0, 0,1,-2,0,-1`, 
     104 
     105    /// magnetic flux 
     106    `"Wb", 1.0, 2,1,-2,0,1`, 
     107     
     108    /// inductance 
     109    `"H", 1.0, 2,1,-2,0,-2`, 
    93110     
    94111    /// Energy Unit Types 
    95     `"Joule",1,2,1,-2,0,0`, 
    96     `"Erg",1e-7L,2,1,-2,0,0`, 
    97     `"Cal",4.1868L,2,1,-2,0,0`, 
    98     `"eV",1.602176462e-19L,2,1,-2,0,0`, 
     112    `"Joule", 1.0,              2,1,-2,0,0`, 
     113    `"Erg",   1e-7L,            2,1,-2,0,0`, 
     114    `"Cal",   4.1868L,          2,1,-2,0,0`, 
     115    `"eV",    1.602176462e-19L, 2,1,-2,0,0`, 
    99116     
    100117    /// Voltage Unit Types 
    101     `"Volt",1,2,1,-3,0,-1`, 
     118    `"Volt", 1.0, 2,1,-3,0,-1`, 
    102119     
    103120    /// Frequency Unit Types 
    104     `"Hz",1,0,0,-1,0,0`, 
     121    `"Hz", 1.0, 0,0,-1,0,0`, 
    105122     
    106123    /// Resistance Unit Types 
    107     `"Ohm",1,-2,-1,3,0,2`, 
     124    `"Ohm", 1.0, -2,-1,3,0,2`, 
    108125     
    109126    /// Pressure Unit Types 
    110     `"Pa",1,-1,1,-2,0,0`, 
    111     `"Bar",1e5L,-1,1,-2,0,0`, 
    112     `"Atm",1.01325e5L,-1,1,-2,0,0`, 
     127    `"Pa",  1.0,        -1,1,-2,0,0`, 
     128    `"Bar", 1e5L,       -1,1,-2,0,0`, 
     129    `"Atm", 1.01325e5L, -1,1,-2,0,0`, 
     130    `"psi", 6.895e3,    -1,1,-2,0,0`, 
     131     
     132    /// Viscosity 
     133    // 
     134    `"Poise",  0.1,    -1,1,-1,0,0`, 
     135    // 
     136    `"stokes", 0.0001,  2,0,-1,0,0`, 
    113137     
    114138    /// Power Unit Types 
    115     `"Watt",1,2,1,-3,0,0`, 
    116     `"Hp",745.69987L,2,1,-3,0,0`, 
     139    `"Watt", 1.0,          2,1,-3,0,0`, 
     140    `"Hp",   745.69987L, 2,1,-3,0,0`, 
    117141     
    118142    /// Volume Unit Types 
    119     `"Liter",0.001L,3,0,0,0,0`, 
    120     `"Gal",0.0037854L,3,0,0,0,0`, 
    121     `"Cup",0.0002365875,3,0,0,0,0`, 
    122      
     143    `"Steres", 1.0,          3,0,0,0,0`, 
     144    `"Liter",  0.001L,       3,0,0,0,0`, 
     145    `"Gal",    0.0037854L,   3,0,0,0,0`, 
     146    `"Cup",    0.0002365875, 3,0,0,0,0`, 
     147 
     148    /// volume rate 
     149    `"gpm",    6.309e-5L,   3,0,-1,0,0`, 
     150    `"cfm",    4.719e-4L,   3,0,-1,0,0`, 
     151 
    123152    /// Speed Unit Types 
    124     `"Knots",1.5144L,1,0,-1,0,0`, 
    125      
    126     /// Capacetance Unit Types 
    127     `"Farad",1,-2,-1,4,0,2`, 
     153    `"Knots", 1.5144L, 1,0,-1,0,0`, 
     154    `"mph",   0.447L,  1,0,-1,0,0`, 
     155    `"kph",   0.278L,  1,0,-1,0,0`, 
     156     
     157    /// Capacitance Unit Types 
     158    `"Farad", 1.0, -2,-1,4,0,2`, 
    128159     
    129160    /// Area Unit Types 
    130     `"Acre",4046.856421L,2,0,0,0,0`, 
    131      
    132     /// Acc Unit Types 
    133     `"G",9.80665L,1,0,-2,0,0` 
     161    `"Acre", 4046.856421L, 2,0,0,0,0`, 
     162     
     163    /// Acceleration Unit Types 
     164    `"G", 9.80665L, 1,0,-2,0,0` 
    134165 
    135166    ) unitsSet; 
     
    146177struct SI 
    147178{ 
    148     alias Unit!(0,1, 0,1, 0,1, 0,1, 0,1) Value; 
    149     alias Unit!(1,1, 0,1, 0,1, 0,1, 0,1) Meter; 
    150     alias Unit!(0,1, 1,1, 0,1, 0,1, 0,1) Kilogram; 
    151     alias Unit!(0,1, 0,1, 1,1, 0,1, 0,1) Second; 
    152     alias Unit!(0,1, 0,1, 0,1, 1,1, 0,1) Kelvin; 
    153     alias Unit!(0,1, 0,1, 0,1, 0,1, 1,1) Ampere; 
    154  
    155179    mixin(BuildSet!("Make", unitsSet)); 
    156180} 
  • trunk/units/unit.d

    r437 r447  
    3434    private template Mul(T) 
    3535    { 
    36         static if(is(T == real)) 
    37             alias That Mul; 
     36        static if(is(T : real)) 
     37            alias This Mul; 
    3838        else 
    3939            alias Unit! 
     
    4848    public template Div(T) 
    4949    { 
    50         static if(is(T == real)) 
    51             alias That Div; 
     50        static if(is(T : real)) 
     51            alias This Div; 
    5252        else 
    5353            alias Unit! 
     
    9090        This opAdd(real that) { This ret; ret.value = this.value + that; return ret; } 
    9191        This opSub(real that) { This ret; ret.value = this.value - that; return ret; } 
     92        This opAdd_r(real that) { This ret; ret.value = that + this.value; return ret; } 
     93        This opSub_r(real that) { This ret; ret.value = that - this.value; return ret; } 
    9294        real opCast() { return value; } 
    9395    } 
     
    9597    //static if(is(This == Unit!(1,1, 0,1, 0,1, 0,1, 0,1))) pragma(msg, Mul!(Unit!(1,1, 0,1, 0,1, 0,1, 0,1)).stringof); 
    9698     
    97     Mul!(T) opMul(T)(T that) { Mul!(T) ret; ret.value = this.value * that.value; return ret; } 
    98     Div!(T) opDiv(T)(T that) { Div!(T) ret; ret.value = this.value * that.value; return ret; } 
     99    Mul!(T) opMul(T)(T that) { Mul!(T) ret; static if(is(T : real)) ret.value = this.value * that; else ret.value = this.value * that.value; return ret; } 
     100    Div!(T) opDiv(T)(T that) { Div!(T) ret; static if(is(T : real)) ret.value = this.value / that; else ret.value = this.value / that.value; return ret; } 
     101     
     102    This opMul_r(real that) { This ret; ret.value = this.value * that; return ret; } 
     103    Power!(-1) opDiv_r(real that) { Power!(-1) ret; ret.value = that / this.value; return ret; } 
    99104     
    100105    RootT!(i) Root(int i)(){RootT!(i) ret; ret.value = sqrt(this.value); return ret;}