Changeset 447
- Timestamp:
- 10/14/08 23:56:55 (1 month ago)
- Files:
-
- trunk/units/constants.d (modified) (5 diffs)
- trunk/units/rational.d (modified) (2 diffs)
- trunk/units/si.d (modified) (4 diffs)
- trunk/units/types.d (added)
- trunk/units/unit.d (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/units/constants.d
r441 r447 2 2 * Constant values with units 3 3 */ 4 module constan st;4 module constants; 5 5 6 6 import si; 7 7 import 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 } 8 import types; 13 9 14 10 /// math constants … … 16 12 { 17 13 /// Euler's constant 18 static const SI.Value gamma = { value : 0.577215664901532 };14 static const Types.Value gamma = { value : 0.577215664901532 }; 19 15 /// good old pi 20 static const SI.Value pi = { value : 3.14159265359};16 static const Types.Value pi = { value : 3.14159265359}; 21 17 /// natural log base 22 static const SI.Value e = { value : 2.718281828459 };18 static const Types.Value e = { value : 2.718281828459 }; 23 19 /// golden ratio 24 static const SI.Value phi = { value : 1.6180339887498948482045868 };20 static const Types.Value phi = { value : 1.6180339887498948482045868 }; 25 21 } 26 22 … … 29 25 { 30 26 /// unified atomic mass 31 static const SI.Kilogramu = { value : 1.66053873e-27 };27 static const Types.Mass u = { value : 1.66053873e-27 }; 32 28 /// electron mass 33 static const SI.Kilogramme = { value : 9.109373815276e-31 };29 static const Types.Mass me = { value : 9.109373815276e-31 }; 34 30 /// 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 }; 36 32 /// Proton mass 37 static const SI.Kilogrammp = { value : 1.67262158e-27 };33 static const Types.Mass mp = { value : 1.67262158e-27 }; 38 34 /// Neutron mass 39 static const SI.Kilogrammn = { value : 1.67492716e-27 };35 static const Types.Mass mn = { value : 1.67492716e-27 }; 40 36 41 37 } … … 45 41 { 46 42 /// 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 }; 48 44 /// 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}; 50 46 /// 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 }; 52 48 /// 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}; 54 50 /// 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 }; 56 52 /// 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 }; 58 54 /// 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 }; 60 56 /// 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 }; 62 58 63 59 } … … 67 63 { 68 64 /// Astronomical unit 69 static const SI.MeterAU = { value :149.59787e11 };65 static const Types.Distance AU = { value :149.59787e11 }; 70 66 } trunk/units/rational.d
r437 r447 3 3 template Tpl(T...){alias T Tpl;} 4 4 5 template GCD_(uint n, uint d)5 uint gcd(int n, uint d) 6 6 { 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; 11 15 } 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 16 static assert(7 == gcd(14,21)); 25 17 26 18 template Reduce(int n, uint d) … … 30 22 { 31 23 //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); 35 27 } 36 28 else trunk/units/si.d
r440 r447 21 21 /// unit encoder: stuff the value in the correct type with convertions 22 22 const char[] Make = 23 "static "~type~name~"(real val)" ~23 "static "~type~name~"(real val)" 24 24 "{"~ 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; 29 36 30 37 /// unit decoder: if the unit classes match, create a function that extracts, converts and returns the value. 31 38 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; 39 47 //pragma(msg,From); 40 48 } … … 46 54 const BuildSet = ""; 47 55 else static if (T.length == 1) 48 const BuildSet = `mixin(Option!(`~T[0]~`).`~suf~`);`;56 const BuildSet = (mixin(`Option!(`~T[0]~`).`~suf)); 49 57 else 50 58 const BuildSet = BuildSet!(suf,T[0..$/2]) ~ BuildSet!(suf,T[$/2..$]); … … 53 61 /// The set of unit defs 54 62 alias 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`, 60 68 61 69 /// 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`, 72 80 73 81 /// 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`, 77 85 78 86 /// 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`, 82 90 83 91 /// Temperature Unit Types 84 `"R", 0.5555555555,0,0,0,1,0`,92 `"R", 0.5555555555, 0,0,0,1,0`, 85 93 86 94 /// 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`, 90 98 91 99 /// 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`, 93 110 94 111 /// 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`, 99 116 100 117 /// Voltage Unit Types 101 `"Volt", 1,2,1,-3,0,-1`,118 `"Volt", 1.0, 2,1,-3,0,-1`, 102 119 103 120 /// Frequency Unit Types 104 `"Hz", 1,0,0,-1,0,0`,121 `"Hz", 1.0, 0,0,-1,0,0`, 105 122 106 123 /// Resistance Unit Types 107 `"Ohm", 1,-2,-1,3,0,2`,124 `"Ohm", 1.0, -2,-1,3,0,2`, 108 125 109 126 /// 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`, 113 137 114 138 /// 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`, 117 141 118 142 /// 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 123 152 /// 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`, 128 159 129 160 /// Area Unit Types 130 `"Acre", 4046.856421L,2,0,0,0,0`,131 132 /// Acc Unit Types133 `"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` 134 165 135 166 ) unitsSet; … … 146 177 struct SI 147 178 { 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 155 179 mixin(BuildSet!("Make", unitsSet)); 156 180 } trunk/units/unit.d
r437 r447 34 34 private template Mul(T) 35 35 { 36 static if(is(T ==real))37 alias Th atMul;36 static if(is(T : real)) 37 alias This Mul; 38 38 else 39 39 alias Unit! … … 48 48 public template Div(T) 49 49 { 50 static if(is(T ==real))51 alias Th atDiv;50 static if(is(T : real)) 51 alias This Div; 52 52 else 53 53 alias Unit! … … 90 90 This opAdd(real that) { This ret; ret.value = this.value + that; return ret; } 91 91 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; } 92 94 real opCast() { return value; } 93 95 } … … 95 97 //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); 96 98 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; } 99 104 100 105 RootT!(i) Root(int i)(){RootT!(i) ret; ret.value = sqrt(this.value); return ret;}
