├── .gitignore ├── Fix.cs ├── FixConst.cs ├── FixMath.cs ├── FixMathConsts.cs ├── FixTrans2.cs ├── FixTrans3.cs ├── FixVec2.cs ├── FixVec3.cs ├── FixedPointy.csproj ├── FixedPointy.sln ├── LICENSE.txt ├── README.md └── Scratch ├── Program.cs └── Scratch.csproj /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | obj/ 3 | *.userprefs 4 | -------------------------------------------------------------------------------- /Fix.cs: -------------------------------------------------------------------------------- 1 | /* FixedPointy - A simple fixed-point math library for C#. 2 | * 3 | * Copyright (c) 2013 Jameson Ernst 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | */ 23 | 24 | using System; 25 | using System.Text; 26 | 27 | namespace FixedPointy { 28 | public struct Fix { 29 | internal const int FRACTIONAL_BITS = 16; 30 | 31 | internal const int INTEGER_BITS = sizeof(int) * 8 - FRACTIONAL_BITS; 32 | internal const int FRACTION_MASK = (int)(uint.MaxValue >> INTEGER_BITS); 33 | internal const int INTEGER_MASK = (int)(-1 & ~FRACTION_MASK); 34 | internal const int FRACTION_RANGE = FRACTION_MASK + 1; 35 | internal const int MIN_INTEGER = int.MinValue >> FRACTIONAL_BITS; 36 | internal const int MAX_INTEGER = int.MaxValue >> FRACTIONAL_BITS; 37 | 38 | public static readonly Fix Zero = new Fix(0); 39 | public static readonly Fix One = new Fix(FRACTION_RANGE); 40 | public static readonly Fix MinValue = new Fix(int.MinValue); 41 | public static readonly Fix MaxValue = new Fix(int.MaxValue); 42 | public static readonly Fix Epsilon = new Fix(1); 43 | 44 | static Fix () { 45 | if (FRACTIONAL_BITS < 8) 46 | throw new Exception("Fix must have at least 8 fractional bits."); 47 | if (INTEGER_BITS < 10) 48 | throw new Exception("Fix must have at least 10 integer bits."); 49 | if (FRACTIONAL_BITS % 2 == 1) 50 | throw new Exception("Fix must have an even number of fractional and integer bits."); 51 | } 52 | 53 | public static int FractionalBits { get { return FRACTIONAL_BITS; } } 54 | public static int IntegerBits { get { return INTEGER_BITS; } } 55 | public static int FractionMask { get { return FRACTION_MASK; } } 56 | public static int IntegerMask { get { return INTEGER_MASK; } } 57 | public static int FractionRange { get { return FRACTION_RANGE; } } 58 | public static int MinInteger { get { return MIN_INTEGER; } } 59 | public static int MaxInteger { get { return MAX_INTEGER; } } 60 | 61 | public static Fix Mix (int integer, int numerator, int denominator) { 62 | if (numerator < 0 || denominator < 0) 63 | throw new ArgumentException("Ratio must be positive."); 64 | 65 | int fraction = ((int)((long)FRACTION_RANGE * numerator / denominator) & FRACTION_MASK); 66 | fraction = integer < 0 ? -fraction : fraction; 67 | 68 | return new Fix((integer << FRACTIONAL_BITS) + fraction); 69 | } 70 | 71 | public static Fix Ratio (int numerator, int denominator) { 72 | return new Fix((int)((((long)numerator << (FRACTIONAL_BITS + 1)) / (long)denominator + 1) >> 1)); 73 | } 74 | 75 | public static explicit operator double (Fix value) { 76 | return (double)(value._raw >> FRACTIONAL_BITS) + (value._raw & FRACTION_MASK) / (double)FRACTION_RANGE; 77 | } 78 | 79 | public static explicit operator float (Fix value) { 80 | return (float)(double)value; 81 | } 82 | 83 | public static explicit operator int (Fix value) { 84 | if (value._raw > 0) 85 | return value._raw >> FRACTIONAL_BITS; 86 | else 87 | return (value._raw + FRACTION_MASK) >> FRACTIONAL_BITS; 88 | } 89 | 90 | public static implicit operator Fix (int value) { 91 | return new Fix(value << FRACTIONAL_BITS); 92 | } 93 | 94 | public static bool operator == (Fix lhs, Fix rhs) { 95 | return lhs._raw == rhs._raw; 96 | } 97 | 98 | public static bool operator != (Fix lhs, Fix rhs) { 99 | return lhs._raw != rhs._raw; 100 | } 101 | 102 | public static bool operator > (Fix lhs, Fix rhs) { 103 | return lhs._raw > rhs._raw; 104 | } 105 | 106 | public static bool operator >= (Fix lhs, Fix rhs) { 107 | return lhs._raw >= rhs._raw; 108 | } 109 | 110 | public static bool operator < (Fix lhs, Fix rhs) { 111 | return lhs._raw < rhs._raw; 112 | } 113 | 114 | public static bool operator <= (Fix lhs, Fix rhs) { 115 | return lhs._raw <= rhs._raw; 116 | } 117 | 118 | public static Fix operator + (Fix value) { 119 | return value; 120 | } 121 | 122 | public static Fix operator - (Fix value) { 123 | return new Fix(-value._raw); 124 | } 125 | 126 | public static Fix operator + (Fix lhs, Fix rhs) { 127 | return new Fix(lhs._raw + rhs._raw); 128 | } 129 | 130 | public static Fix operator - (Fix lhs, Fix rhs) { 131 | return new Fix(lhs._raw - rhs._raw); 132 | } 133 | 134 | public static Fix operator * (Fix lhs, Fix rhs) { 135 | return new Fix((int)(((long)lhs._raw * (long)rhs._raw + (FRACTION_RANGE >> 1)) >> FRACTIONAL_BITS)); 136 | } 137 | 138 | public static Fix operator / (Fix lhs, Fix rhs) { 139 | return new Fix((int)((((long)lhs._raw << (FRACTIONAL_BITS + 1)) / (long)rhs._raw + 1) >> 1)); 140 | } 141 | 142 | public static Fix operator % (Fix lhs, Fix rhs) { 143 | return new Fix(lhs.Raw % rhs.Raw); 144 | } 145 | 146 | public static Fix operator << (Fix lhs, int rhs) { 147 | return new Fix(lhs.Raw << rhs); 148 | } 149 | 150 | public static Fix operator >> (Fix lhs, int rhs) { 151 | return new Fix(lhs.Raw >> rhs); 152 | } 153 | 154 | int _raw; 155 | 156 | public Fix (int raw) { 157 | _raw = raw; 158 | } 159 | 160 | public int Raw { get { return _raw; } } 161 | 162 | public override bool Equals (object obj) { 163 | return (obj is Fix && ((Fix)obj) == this); 164 | } 165 | 166 | public override int GetHashCode () { 167 | return Raw.GetHashCode(); 168 | } 169 | 170 | public override string ToString () { 171 | var sb = new StringBuilder(); 172 | if (_raw < 0) 173 | sb.Append(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NegativeSign); 174 | int abs = (int)this; 175 | abs = abs < 0 ? -abs : abs; 176 | sb.Append(abs.ToString()); 177 | ulong fraction = (ulong)(_raw & FRACTION_MASK); 178 | if (fraction == 0) 179 | return sb.ToString(); 180 | 181 | fraction = _raw < 0 ? FRACTION_RANGE - fraction : fraction; 182 | fraction *= 1000000L; 183 | fraction += FRACTION_RANGE >> 1; 184 | fraction >>= FRACTIONAL_BITS; 185 | 186 | sb.Append(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); 187 | sb.Append(fraction.ToString("D6").TrimEnd('0')); 188 | return sb.ToString(); 189 | } 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /FixConst.cs: -------------------------------------------------------------------------------- 1 | /* FixedPointy - A simple fixed-point math library for C#. 2 | * 3 | * Copyright (c) 2013 Jameson Ernst 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | */ 23 | 24 | using System; 25 | using System.Text; 26 | 27 | namespace FixedPointy { 28 | public struct FixConst { 29 | public static explicit operator double (FixConst f) { 30 | return (double)(f._raw >> 32) + ((uint)(f._raw) / (uint.MaxValue + 1.0)); 31 | } 32 | 33 | public static implicit operator FixConst (double value) { 34 | if (value < int.MinValue || value >= int.MaxValue + 1L) 35 | throw new OverflowException(); 36 | 37 | var floor = Math.Floor(value); 38 | return new FixConst(((long)floor << 32) + (long)((value - floor) * (uint.MaxValue + 1.0) + 0.5)); 39 | } 40 | 41 | public static implicit operator Fix (FixConst value) { 42 | return new Fix((int)((value.Raw + (1 << (32 - Fix.FRACTIONAL_BITS - 1))) >> (32 - Fix.FRACTIONAL_BITS))); 43 | } 44 | 45 | public static explicit operator int (FixConst value) { 46 | if (value._raw > 0) 47 | return (int)(value._raw >> 32); 48 | else 49 | return (int)((value._raw + uint.MaxValue) >> 32); 50 | } 51 | 52 | public static implicit operator FixConst (int value) { 53 | return new FixConst((long)value << 32); 54 | } 55 | 56 | public static bool operator == (FixConst lhs, FixConst rhs) { 57 | return lhs._raw == rhs._raw; 58 | } 59 | 60 | public static bool operator != (FixConst lhs, FixConst rhs) { 61 | return lhs._raw != rhs._raw; 62 | } 63 | 64 | public static bool operator > (FixConst lhs, FixConst rhs) { 65 | return lhs._raw > rhs._raw; 66 | } 67 | 68 | public static bool operator >= (FixConst lhs, FixConst rhs) { 69 | return lhs._raw >= rhs._raw; 70 | } 71 | 72 | public static bool operator < (FixConst lhs, FixConst rhs) { 73 | return lhs._raw < rhs._raw; 74 | } 75 | 76 | public static bool operator <= (FixConst lhs, FixConst rhs) { 77 | return lhs._raw <= rhs._raw; 78 | } 79 | 80 | public static FixConst operator + (FixConst value) { 81 | return value; 82 | } 83 | 84 | public static FixConst operator - (FixConst value) { 85 | return new FixConst(-value._raw); 86 | } 87 | 88 | long _raw; 89 | 90 | public FixConst (long raw) { 91 | _raw = raw; 92 | } 93 | 94 | public long Raw { get { return _raw; } } 95 | 96 | public override bool Equals (object obj) { 97 | return (obj is FixConst && ((FixConst)obj) == this); 98 | } 99 | 100 | public override int GetHashCode () { 101 | return Raw.GetHashCode(); 102 | } 103 | 104 | public override string ToString () { 105 | var sb = new StringBuilder(); 106 | if (_raw < 0) 107 | sb.Append(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NegativeSign); 108 | long abs = (int)this; 109 | abs = abs < 0 ? -abs : abs; 110 | sb.Append(abs.ToString()); 111 | ulong fraction = (ulong)(_raw & uint.MaxValue); 112 | if (fraction == 0) 113 | return sb.ToString(); 114 | 115 | fraction = _raw < 0 ? (uint.MaxValue + 1L) - fraction : fraction; 116 | fraction *= 1000000000L; 117 | fraction += (uint.MaxValue + 1L) >> 1; 118 | fraction >>= 32; 119 | 120 | sb.Append(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); 121 | sb.Append(fraction.ToString("D9").TrimEnd('0')); 122 | return sb.ToString(); 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /FixMath.cs: -------------------------------------------------------------------------------- 1 | /* FixedPointy - A simple fixed-point math library for C#. 2 | * 3 | * Copyright (c) 2013 Jameson Ernst 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | */ 23 | 24 | using System; 25 | using System.Linq; 26 | 27 | namespace FixedPointy { 28 | public static partial class FixMath { 29 | public static readonly Fix PI; 30 | public static readonly Fix E; 31 | static Fix _log2_E; 32 | static Fix _log2_10; 33 | static Fix _ln2; 34 | static Fix _log10_2; 35 | static Fix[] _quarterSine; 36 | static Fix[] _cordicAngles; 37 | static Fix[] _cordicGains; 38 | 39 | static FixMath () { 40 | if (_quarterSineResPower >= Fix.FRACTIONAL_BITS) 41 | throw new Exception("_quarterSineResPower must be less than Fix.FractionalBits."); 42 | if (_quarterSineConsts.Length != 90 * (1 << _quarterSineResPower) + 1) 43 | throw new Exception("_quarterSineConst.Length must be 90 * 2^(_quarterSineResPower) + 1."); 44 | 45 | PI = _piConst; 46 | E = _eConst; 47 | _log2_E = _log2_EConst; 48 | _log2_10 = _log2_10Const; 49 | _ln2 = _ln2Const; 50 | _log10_2 = _log10_2Const; 51 | 52 | _quarterSine = Array.ConvertAll(_quarterSineConsts, c => (Fix)c); 53 | _cordicAngles = Array.ConvertAll(_cordicAngleConsts, c => (Fix)c); 54 | _cordicGains = Array.ConvertAll(_cordicGainConsts, c => (Fix)c); 55 | } 56 | 57 | public static Fix Abs (Fix value) { 58 | return value.Raw < 0 ? new Fix(-value.Raw) : value; 59 | } 60 | 61 | public static Fix Sign (Fix value) { 62 | if (value < 0) 63 | return -1; 64 | else if (value > 0) 65 | return 1; 66 | else 67 | return 0; 68 | } 69 | 70 | public static Fix Ceiling (Fix value) { 71 | return new Fix((value.Raw + Fix.FRACTION_MASK) & Fix.INTEGER_MASK); 72 | } 73 | 74 | public static Fix Floor (Fix value) { 75 | return new Fix(value.Raw & Fix.INTEGER_MASK); 76 | } 77 | 78 | public static Fix Truncate (Fix value) { 79 | if (value < 0) 80 | return new Fix((value.Raw + Fix.FRACTION_RANGE) & Fix.INTEGER_MASK); 81 | else 82 | return new Fix(value.Raw & Fix.INTEGER_MASK); 83 | } 84 | 85 | public static Fix Round (Fix value) { 86 | return new Fix((value.Raw + (Fix.FRACTION_RANGE >> 1)) & ~Fix.FRACTION_MASK); 87 | } 88 | 89 | public static Fix Min (Fix v1, Fix v2) { 90 | return v1 < v2 ? v1 : v2; 91 | } 92 | 93 | public static Fix Max (Fix v1, Fix v2) { 94 | return v1 > v2 ? v1 : v2; 95 | } 96 | 97 | public static Fix Sqrt (Fix value) { 98 | if (value.Raw < 0) 99 | throw new ArgumentOutOfRangeException("value", "Value must be non-negative."); 100 | if (value.Raw == 0) 101 | return 0; 102 | 103 | return new Fix((int)(SqrtULong((ulong)value.Raw << (Fix.FRACTIONAL_BITS + 2)) + 1) >> 1); 104 | } 105 | 106 | internal static uint SqrtULong (ulong N) { 107 | ulong x = 1L << ((31 + (Fix.FRACTIONAL_BITS + 2) + 1) / 2); 108 | while (true) { 109 | ulong y = (x + N / x) >> 1; 110 | if (y >= x) 111 | return (uint)x; 112 | x = y; 113 | } 114 | } 115 | 116 | public static Fix Sin (Fix degrees) { 117 | return CosRaw(degrees.Raw - (90 << Fix.FRACTIONAL_BITS)); 118 | } 119 | 120 | public static Fix Cos (Fix degrees) { 121 | return CosRaw(degrees.Raw); 122 | } 123 | 124 | static Fix CosRaw (int raw) { 125 | raw = raw < 0 ? -raw : raw; 126 | int t = raw & ((1 << (Fix.FRACTIONAL_BITS - _quarterSineResPower)) - 1); 127 | raw = (raw >> (Fix.FRACTIONAL_BITS - _quarterSineResPower)); 128 | 129 | if (t == 0) 130 | return CosRawLookup(raw); 131 | 132 | Fix v1 = CosRawLookup(raw); 133 | Fix v2 = CosRawLookup(raw + 1); 134 | 135 | return new Fix( 136 | (int)( 137 | ( 138 | (long)v1.Raw * ((1 << (Fix.FRACTIONAL_BITS - _quarterSineResPower)) - t) 139 | + (long)v2.Raw * t 140 | + (1 << (Fix.FRACTIONAL_BITS - _quarterSineResPower - 1)) 141 | ) 142 | >> (Fix.FRACTIONAL_BITS - _quarterSineResPower) 143 | ) 144 | ); 145 | } 146 | 147 | static Fix CosRawLookup (int raw) { 148 | raw %= 360 * (1 << _quarterSineResPower); 149 | 150 | if (raw < 90 * (1 << _quarterSineResPower)) { 151 | return _quarterSine[90 * (1 << _quarterSineResPower) - raw]; 152 | } else if (raw < 180 * (1 << _quarterSineResPower)) { 153 | raw -= 90 * (1 << _quarterSineResPower); 154 | return -_quarterSine[raw]; 155 | } else if (raw < 270 * (1 << _quarterSineResPower)) { 156 | raw -= 180 * (1 << _quarterSineResPower); 157 | return -_quarterSine[90 * (1 << _quarterSineResPower) - raw]; 158 | } else { 159 | raw -= 270 * (1 << _quarterSineResPower); 160 | return _quarterSine[raw]; 161 | } 162 | } 163 | 164 | public static Fix Tan (Fix degrees) { 165 | return Sin(degrees) / Cos(degrees); 166 | } 167 | 168 | public static Fix Asin (Fix value) { 169 | return Atan2(value, Sqrt((1 + value) * (1 - value))); 170 | } 171 | 172 | public static Fix Acos (Fix value) { 173 | return Atan2(Sqrt((1 + value) * (1 - value)), value); 174 | } 175 | 176 | public static Fix Atan (Fix value) { 177 | return Atan2(value, 1); 178 | } 179 | 180 | public static Fix Atan2 (Fix y, Fix x) { 181 | if (x == 0 && y == 0) 182 | throw new ArgumentOutOfRangeException("y and x cannot both be 0."); 183 | 184 | Fix angle = 0; 185 | Fix xNew, yNew; 186 | 187 | if (x < 0) { 188 | if (y < 0) { 189 | xNew = -y; 190 | yNew = x; 191 | angle = -90; 192 | } else if (y > 0) { 193 | xNew = y; 194 | yNew = -x; 195 | angle = 90; 196 | } else { 197 | xNew = x; 198 | yNew = y; 199 | angle = 180; 200 | } 201 | x = xNew; 202 | y = yNew; 203 | } 204 | 205 | for (int i = 0; i < Fix.FRACTIONAL_BITS + 2; i++) { 206 | if (y > 0) { 207 | xNew = x + (y >> i); 208 | yNew = y - (x >> i); 209 | angle += _cordicAngles[i]; 210 | } else if (y < 0) { 211 | xNew = x - (y >> i); 212 | yNew = y + (x >> i); 213 | angle -= _cordicAngles[i]; 214 | } else 215 | break; 216 | 217 | x = xNew; 218 | y = yNew; 219 | } 220 | 221 | return angle; 222 | } 223 | 224 | public static Fix Exp (Fix value) { 225 | return Pow(E, value); 226 | } 227 | 228 | public static Fix Pow (Fix b, Fix exp) { 229 | if (b == 1 || exp == 0) 230 | return 1; 231 | 232 | int intPow; 233 | Fix intFactor; 234 | if ((exp.Raw & Fix.FRACTION_MASK) == 0) { 235 | intPow = (int)((exp.Raw + (Fix.FRACTION_RANGE >> 1)) >> Fix.FRACTIONAL_BITS); 236 | Fix t; 237 | int p; 238 | if (intPow < 0) { 239 | t = 1 / b; 240 | p = -intPow; 241 | } else { 242 | t = b; 243 | p = intPow; 244 | } 245 | 246 | intFactor = 1; 247 | while (p > 0) { 248 | if ((p & 1) != 0) 249 | intFactor *= t; 250 | t *= t; 251 | p >>= 1; 252 | } 253 | 254 | return intFactor; 255 | } 256 | 257 | exp *= Log(b, 2); 258 | b = 2; 259 | intPow = (int)((exp.Raw + (Fix.FRACTION_RANGE >> 1)) >> Fix.FRACTIONAL_BITS); 260 | intFactor = intPow < 0 ? Fix.One >> -intPow : Fix.One << intPow; 261 | 262 | long x = ( 263 | ((exp.Raw - (intPow << Fix.FRACTIONAL_BITS)) * _ln2Const.Raw) 264 | + (Fix.FRACTION_RANGE >> 1) 265 | ) >> Fix.FRACTIONAL_BITS; 266 | if (x == 0) 267 | return intFactor; 268 | 269 | long fracFactor = x; 270 | long xa = x; 271 | for (int i = 2; i < _invFactConsts.Length; i++) { 272 | if (xa == 0) 273 | break; 274 | xa *= x; 275 | xa += (1L << (32 - 1)); 276 | xa >>= 32; 277 | long p = xa * _invFactConsts[i].Raw; 278 | p += (1L << (32 - 1)); 279 | p >>= 32; 280 | fracFactor += p; 281 | } 282 | 283 | return new Fix((int)((((long)intFactor.Raw * fracFactor + (1L << (32 - 1))) >> 32) + intFactor.Raw)); 284 | } 285 | 286 | public static Fix Log (Fix value) { 287 | return Log2(value) * _ln2; 288 | } 289 | 290 | public static Fix Log (Fix value, Fix b) { 291 | if (b == 2) 292 | return Log2(value); 293 | else if (b == E) 294 | return Log(value); 295 | else if (b == 10) 296 | return Log10(value); 297 | else 298 | return Log2(value) / Log2(b); 299 | } 300 | 301 | public static Fix Log10 (Fix value) { 302 | return Log2(value) * _log10_2; 303 | } 304 | 305 | static Fix Log2 (Fix value) { 306 | if (value <= 0) 307 | throw new ArgumentOutOfRangeException("value", "Value must be positive."); 308 | 309 | uint x = (uint)value.Raw; 310 | uint b = 1U << (Fix.FRACTIONAL_BITS - 1); 311 | uint y = 0; 312 | 313 | while (x < 1U << Fix.FRACTIONAL_BITS) { 314 | x <<= 1; 315 | y -= 1U << Fix.FRACTIONAL_BITS; 316 | } 317 | 318 | while (x >= 2U << Fix.FRACTIONAL_BITS) { 319 | x >>= 1; 320 | y += 1U << Fix.FRACTIONAL_BITS; 321 | } 322 | 323 | ulong z = x; 324 | 325 | for (int i = 0; i < Fix.FRACTIONAL_BITS; i++) { 326 | z = z * z >> Fix.FRACTIONAL_BITS; 327 | if (z >= 2U << Fix.FRACTIONAL_BITS) { 328 | z >>= 1; 329 | y += b; 330 | } 331 | b >>= 1; 332 | } 333 | 334 | return new Fix((int)y); 335 | } 336 | } 337 | } 338 | -------------------------------------------------------------------------------- /FixMathConsts.cs: -------------------------------------------------------------------------------- 1 | /* FixedPointy - A simple fixed-point math library for C#. 2 | * 3 | * Copyright (c) 2013 Jameson Ernst 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | */ 23 | 24 | using System; 25 | 26 | namespace FixedPointy { 27 | public static partial class FixMath { 28 | static FixConst _piConst = new FixConst(13493037705); 29 | static FixConst _eConst = new FixConst(11674931555); 30 | static FixConst _log2_EConst = new FixConst(6196328019); 31 | static FixConst _log2_10Const = new FixConst(14267572527); 32 | static FixConst _ln2Const = new FixConst(2977044472); 33 | static FixConst _log10_2Const = new FixConst(1292913986); 34 | 35 | const int _quarterSineResPower = 2; 36 | #region Sine Table 37 | static FixConst[] _quarterSineConsts = { 38 | new FixConst(0), new FixConst(18740271), new FixConst(37480185), new FixConst(56219385), 39 | new FixConst(74957515), new FixConst(93694218), new FixConst(112429137), new FixConst(131161916), 40 | new FixConst(149892197), new FixConst(168619625), new FixConst(187343842), new FixConst(206064493), 41 | new FixConst(224781220), new FixConst(243493669), new FixConst(262201481), new FixConst(280904301), 42 | new FixConst(299601773), new FixConst(318293542), new FixConst(336979250), new FixConst(355658543), 43 | new FixConst(374331065), new FixConst(392996460), new FixConst(411654373), new FixConst(430304448), 44 | new FixConst(448946331), new FixConst(467579667), new FixConst(486204101), new FixConst(504819278), 45 | new FixConst(523424844), new FixConst(542020445), new FixConst(560605727), new FixConst(579180335), 46 | new FixConst(597743917), new FixConst(616296119), new FixConst(634836587), new FixConst(653364969), 47 | new FixConst(671880911), new FixConst(690384062), new FixConst(708874069), new FixConst(727350581), 48 | new FixConst(745813244), new FixConst(764261708), new FixConst(782695622), new FixConst(801114635), 49 | new FixConst(819518395), new FixConst(837906553), new FixConst(856278758), new FixConst(874634661), 50 | new FixConst(892973913), new FixConst(911296163), new FixConst(929601063), new FixConst(947888266), 51 | new FixConst(966157422), new FixConst(984408183), new FixConst(1002640203), new FixConst(1020853134), 52 | new FixConst(1039046630), new FixConst(1057220343), new FixConst(1075373929), new FixConst(1093507041), 53 | new FixConst(1111619334), new FixConst(1129710464), new FixConst(1147780085), new FixConst(1165827855), 54 | new FixConst(1183853429), new FixConst(1201856464), new FixConst(1219836617), new FixConst(1237793546), 55 | new FixConst(1255726910), new FixConst(1273636366), new FixConst(1291521575), new FixConst(1309382194), 56 | new FixConst(1327217885), new FixConst(1345028307), new FixConst(1362813122), new FixConst(1380571991), 57 | new FixConst(1398304576), new FixConst(1416010539), new FixConst(1433689544), new FixConst(1451341253), 58 | new FixConst(1468965330), new FixConst(1486561441), new FixConst(1504129249), new FixConst(1521668421), 59 | new FixConst(1539178623), new FixConst(1556659521), new FixConst(1574110783), new FixConst(1591532075), 60 | new FixConst(1608923068), new FixConst(1626283428), new FixConst(1643612827), new FixConst(1660910933), 61 | new FixConst(1678177418), new FixConst(1695411953), new FixConst(1712614210), new FixConst(1729783862), 62 | new FixConst(1746920580), new FixConst(1764024040), new FixConst(1781093915), new FixConst(1798129881), 63 | new FixConst(1815131613), new FixConst(1832098787), new FixConst(1849031081), new FixConst(1865928172), 64 | new FixConst(1882789739), new FixConst(1899615460), new FixConst(1916405015), new FixConst(1933158084), 65 | new FixConst(1949874349), new FixConst(1966553491), new FixConst(1983195193), new FixConst(1999799137), 66 | new FixConst(2016365009), new FixConst(2032892491), new FixConst(2049381270), new FixConst(2065831032), 67 | new FixConst(2082241464), new FixConst(2098612252), new FixConst(2114943086), new FixConst(2131233655), 68 | new FixConst(2147483648), new FixConst(2163692756), new FixConst(2179860670), new FixConst(2195987083), 69 | new FixConst(2212071688), new FixConst(2228114178), new FixConst(2244114248), new FixConst(2260071593), 70 | new FixConst(2275985909), new FixConst(2291856895), new FixConst(2307684246), new FixConst(2323467662), 71 | new FixConst(2339206844), new FixConst(2354901489), new FixConst(2370551301), new FixConst(2386155981), 72 | new FixConst(2401715233), new FixConst(2417228758), new FixConst(2432696264), new FixConst(2448117454), 73 | new FixConst(2463492036), new FixConst(2478819716), new FixConst(2494100203), new FixConst(2509333207), 74 | new FixConst(2524518436), new FixConst(2539655602), new FixConst(2554744416), new FixConst(2569784592), 75 | new FixConst(2584775843), new FixConst(2599717883), new FixConst(2614610429), new FixConst(2629453196), 76 | new FixConst(2644245902), new FixConst(2658988265), new FixConst(2673680006), new FixConst(2688320843), 77 | new FixConst(2702910498), new FixConst(2717448694), new FixConst(2731935154), new FixConst(2746369601), 78 | new FixConst(2760751762), new FixConst(2775081362), new FixConst(2789358128), new FixConst(2803581789), 79 | new FixConst(2817752074), new FixConst(2831868713), new FixConst(2845931437), new FixConst(2859939978), 80 | new FixConst(2873894071), new FixConst(2887793449), new FixConst(2901637847), new FixConst(2915427003), 81 | new FixConst(2929160652), new FixConst(2942838535), new FixConst(2956460391), new FixConst(2970025959), 82 | new FixConst(2983534983), new FixConst(2996987204), new FixConst(3010382368), new FixConst(3023720217), 83 | new FixConst(3037000500), new FixConst(3050222962), new FixConst(3063387353), new FixConst(3076493421), 84 | new FixConst(3089540917), new FixConst(3102529593), new FixConst(3115459201), new FixConst(3128329495), 85 | new FixConst(3141140230), new FixConst(3153891163), new FixConst(3166582050), new FixConst(3179212649), 86 | new FixConst(3191782722), new FixConst(3204292027), new FixConst(3216740327), new FixConst(3229127385), 87 | new FixConst(3241452965), new FixConst(3253716833), new FixConst(3265918754), new FixConst(3278058497), 88 | new FixConst(3290135830), new FixConst(3302150525), new FixConst(3314102350), new FixConst(3325991081), 89 | new FixConst(3337816489), new FixConst(3349578350), new FixConst(3361276439), new FixConst(3372910535), 90 | new FixConst(3384480416), new FixConst(3395985861), new FixConst(3407426651), new FixConst(3418802568), 91 | new FixConst(3430113397), new FixConst(3441358921), new FixConst(3452538927), new FixConst(3463653201), 92 | new FixConst(3474701533), new FixConst(3485683711), new FixConst(3496599527), new FixConst(3507448772), 93 | new FixConst(3518231241), new FixConst(3528946727), new FixConst(3539595028), new FixConst(3550175940), 94 | new FixConst(3560689261), new FixConst(3571134792), new FixConst(3581512334), new FixConst(3591821689), 95 | new FixConst(3602062661), new FixConst(3612235055), new FixConst(3622338677), new FixConst(3632373336), 96 | new FixConst(3642338838), new FixConst(3652234996), new FixConst(3662061621), new FixConst(3671818526), 97 | new FixConst(3681505524), new FixConst(3691122431), new FixConst(3700669065), new FixConst(3710145244), 98 | new FixConst(3719550787), new FixConst(3728885515), new FixConst(3738149250), new FixConst(3747341816), 99 | new FixConst(3756463039), new FixConst(3765512743), new FixConst(3774490758), new FixConst(3783396912), 100 | new FixConst(3792231035), new FixConst(3800992960), new FixConst(3809682520), new FixConst(3818299548), 101 | new FixConst(3826843882), new FixConst(3835315358), new FixConst(3843713815), new FixConst(3852039094), 102 | new FixConst(3860291035), new FixConst(3868469481), new FixConst(3876574278), new FixConst(3884605270), 103 | new FixConst(3892562305), new FixConst(3900445232), new FixConst(3908253899), new FixConst(3915988159), 104 | new FixConst(3923647864), new FixConst(3931232868), new FixConst(3938743028), new FixConst(3946178199), 105 | new FixConst(3953538241), new FixConst(3960823014), new FixConst(3968032378), new FixConst(3975166196), 106 | new FixConst(3982224333), new FixConst(3989206654), new FixConst(3996113026), new FixConst(4002943318), 107 | new FixConst(4009697400), new FixConst(4016375143), new FixConst(4022976420), new FixConst(4029501105), 108 | new FixConst(4035949075), new FixConst(4042320205), new FixConst(4048614376), new FixConst(4054831467), 109 | new FixConst(4060971360), new FixConst(4067033938), new FixConst(4073019085), new FixConst(4078926688), 110 | new FixConst(4084756634), new FixConst(4090508812), new FixConst(4096183113), new FixConst(4101779428), 111 | new FixConst(4107297652), new FixConst(4112737678), new FixConst(4118099404), new FixConst(4123382727), 112 | new FixConst(4128587547), new FixConst(4133713764), new FixConst(4138761282), new FixConst(4143730003), 113 | new FixConst(4148619834), new FixConst(4153430681), new FixConst(4158162453), new FixConst(4162815059), 114 | new FixConst(4167388412), new FixConst(4171882423), new FixConst(4176297008), new FixConst(4180632082), 115 | new FixConst(4184887562), new FixConst(4189063369), new FixConst(4193159422), new FixConst(4197175643), 116 | new FixConst(4201111956), new FixConst(4204968286), new FixConst(4208744559), new FixConst(4212440704), 117 | new FixConst(4216056650), new FixConst(4219592328), new FixConst(4223047672), new FixConst(4226422614), 118 | new FixConst(4229717092), new FixConst(4232931042), new FixConst(4236064403), new FixConst(4239117116), 119 | new FixConst(4242089121), new FixConst(4244980364), new FixConst(4247790788), new FixConst(4250520341), 120 | new FixConst(4253168970), new FixConst(4255736624), new FixConst(4258223255), new FixConst(4260628816), 121 | new FixConst(4262953261), new FixConst(4265196545), new FixConst(4267358626), new FixConst(4269439463), 122 | new FixConst(4271439016), new FixConst(4273357246), new FixConst(4275194119), new FixConst(4276949597), 123 | new FixConst(4278623649), new FixConst(4280216242), new FixConst(4281727345), new FixConst(4283156931), 124 | new FixConst(4284504972), new FixConst(4285771441), new FixConst(4286956316), new FixConst(4288059574), 125 | new FixConst(4289081193), new FixConst(4290021154), new FixConst(4290879439), new FixConst(4291656032), 126 | new FixConst(4292350918), new FixConst(4292964084), new FixConst(4293495518), new FixConst(4293945210), 127 | new FixConst(4294313152), new FixConst(4294599336), new FixConst(4294803757), new FixConst(4294926411), 128 | new FixConst(4294967296), 129 | }; 130 | #endregion 131 | 132 | #region CORDIC Tables 133 | static FixConst[] _cordicAngleConsts = { 134 | new FixConst(193273528320), new FixConst(114096026022), new FixConst(60285206653), new FixConst(30601712202), 135 | new FixConst(15360239180), new FixConst(7687607525), new FixConst(3844741810), new FixConst(1922488225), 136 | new FixConst(961258780), new FixConst(480631223), new FixConst(240315841), new FixConst(120157949), 137 | new FixConst(60078978), new FixConst(30039490), new FixConst(15019745), new FixConst(7509872), 138 | new FixConst(3754936), new FixConst(1877468), new FixConst(938734), new FixConst(469367), 139 | new FixConst(234684), new FixConst(117342), new FixConst(58671), new FixConst(29335), 140 | }; 141 | 142 | static FixConst[] _cordicGainConsts = { 143 | new FixConst(3037000500), new FixConst(2716375826), new FixConst(2635271635), new FixConst(2614921743), 144 | new FixConst(2609829388), new FixConst(2608555990), new FixConst(2608237621), new FixConst(2608158028), 145 | new FixConst(2608138129), new FixConst(2608133154), new FixConst(2608131911), new FixConst(2608131600), 146 | new FixConst(2608131522), new FixConst(2608131503), new FixConst(2608131498), new FixConst(2608131497), 147 | new FixConst(2608131496), new FixConst(2608131496), new FixConst(2608131496), new FixConst(2608131496), 148 | new FixConst(2608131496), new FixConst(2608131496), new FixConst(2608131496), new FixConst(2608131496), 149 | }; 150 | #endregion 151 | 152 | #region Inverse Factorial Table 153 | static FixConst[] _invFactConsts = { 154 | new FixConst(4294967296), 155 | new FixConst(4294967296), 156 | new FixConst(2147483648), 157 | new FixConst(715827883), 158 | new FixConst(178956971), 159 | new FixConst(35791394), 160 | new FixConst(5965232), 161 | new FixConst(852176), 162 | new FixConst(106522), 163 | new FixConst(11836), 164 | new FixConst(1184), 165 | new FixConst(108), 166 | new FixConst(9), 167 | new FixConst(1), 168 | }; 169 | #endregion 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /FixTrans2.cs: -------------------------------------------------------------------------------- 1 | /* FixedPointy - A simple fixed-point math library for C#. 2 | * 3 | * Copyright (c) 2013 Jameson Ernst 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | */ 23 | 24 | using System; 25 | 26 | namespace FixedPointy { 27 | public struct FixTrans2 { 28 | public static readonly FixTrans2 Identity = new FixTrans2( 29 | 1, 0, 0, 30 | 0, 1, 0 31 | ); 32 | 33 | public static FixTrans2 operator * (FixTrans2 lhs, FixTrans2 rhs) { 34 | return new FixTrans2( 35 | lhs._m11 * rhs._m11 + lhs._m12 * rhs._m21, 36 | lhs._m11 * rhs._m12 + lhs._m12 * rhs._m22, 37 | lhs._m11 * rhs._m13 + lhs._m12 * rhs._m23 + lhs._m13, 38 | lhs._m21 * rhs._m11 + lhs._m22 * rhs._m21, 39 | lhs._m21 * rhs._m12 + lhs._m22 * rhs._m22, 40 | lhs._m21 * rhs._m13 + lhs._m22 * rhs._m23 + lhs._m23 41 | ); 42 | } 43 | 44 | public static FixVec2 operator * (FixTrans2 lhs, FixVec2 rhs) { 45 | return new FixVec2( 46 | lhs._m11 * rhs.X + lhs._m12 * rhs.Y + lhs._m13, 47 | lhs._m21 * rhs.X + lhs._m22 * rhs.Y + lhs._m23 48 | ); 49 | } 50 | 51 | public static FixTrans2 MakeRotation (Fix degrees) { 52 | Fix cos = FixMath.Cos(degrees); 53 | Fix sin = FixMath.Sin(degrees); 54 | return new FixTrans2( 55 | cos, -sin, 0, 56 | sin, cos, 0 57 | ); 58 | } 59 | 60 | public static FixTrans2 MakeScale (FixVec2 scale) { 61 | return new FixTrans2( 62 | scale.X, 0, 0, 63 | 0, scale.Y, 0 64 | ); 65 | } 66 | 67 | public static FixTrans2 MakeTranslation (FixVec2 delta) { 68 | return new FixTrans2( 69 | 1, 0, delta.X, 70 | 0, 1, delta.Y 71 | ); 72 | } 73 | 74 | Fix _m11, _m21, _m12, _m22, _m13, _m23; 75 | 76 | public FixTrans2 ( 77 | Fix m11, Fix m12, Fix m13, 78 | Fix m21, Fix m22, Fix m23 79 | ) { 80 | _m11 = m11; _m12 = m12; _m13 = m13; 81 | _m21 = m21; _m22 = m22; _m23 = m23; 82 | } 83 | 84 | public FixTrans2 (FixVec2 position, FixVec2 scale, Fix rotation) { 85 | Fix cos = FixMath.Cos(rotation); 86 | Fix sin = FixMath.Sin(rotation); 87 | 88 | _m11 = cos * scale.X; _m12 = -sin * scale.X; _m13 = position.X; 89 | _m21 = sin * scale.Y; _m22 = cos * scale.Y; _m23 = position.Y; 90 | } 91 | 92 | public Fix M11 { get { return _m11; } } 93 | public Fix M12 { get { return _m12; } } 94 | public Fix M13 { get { return _m13; } } 95 | public Fix M21 { get { return _m21; } } 96 | public Fix M22 { get { return _m22; } } 97 | public Fix M23 { get { return _m23; } } 98 | 99 | public FixTrans2 Rotate (Fix degrees) { 100 | return MakeRotation(degrees) * this; 101 | } 102 | 103 | public FixTrans2 Scale (FixVec2 scale) { 104 | return new FixTrans2( 105 | _m11 * scale.X, _m12 * scale.X, _m13 * scale.X, 106 | _m21 * scale.Y, _m22 * scale.Y, _m23 * scale.Y 107 | ); 108 | } 109 | 110 | public FixTrans2 Translate (FixVec2 delta) { 111 | return new FixTrans2( 112 | _m11, _m12, _m13 + delta.X, 113 | _m21, _m22, _m23 + delta.Y 114 | ); 115 | } 116 | 117 | public FixVec2 Apply (FixVec2 vec) { 118 | return this * vec; 119 | } 120 | 121 | public override string ToString () { 122 | return string.Format ("[[{0}, {1}, {2}], [{3}, {4}, {5}]]", _m11, _m12, _m13, _m21, _m22, _m23); 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /FixTrans3.cs: -------------------------------------------------------------------------------- 1 | /* FixedPointy - A simple fixed-point math library for C#. 2 | * 3 | * Copyright (c) 2013 Jameson Ernst 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | */ 23 | 24 | using System; 25 | 26 | namespace FixedPointy { 27 | public struct FixTrans3 { 28 | public static readonly FixTrans3 Identity = new FixTrans3( 29 | 1, 0, 0, 0, 30 | 0, 1, 0, 0, 31 | 0, 0, 1, 0 32 | ); 33 | 34 | public static FixTrans3 operator * (FixTrans3 lhs, FixTrans3 rhs) { 35 | return new FixTrans3( 36 | lhs._m11 * rhs._m11 + lhs._m12 * rhs._m21 + lhs._m13 * rhs._m31, 37 | lhs._m11 * rhs._m12 + lhs._m12 * rhs._m22 + lhs._m13 * rhs._m32, 38 | lhs._m11 * rhs._m13 + lhs._m12 * rhs._m23 + lhs._m13 * rhs._m33, 39 | lhs._m11 * rhs._m14 + lhs._m12 * rhs._m24 + lhs._m13 * rhs._m34 + lhs._m14, 40 | lhs._m21 * rhs._m11 + lhs._m22 * rhs._m21 + lhs._m23 * rhs._m31, 41 | lhs._m21 * rhs._m12 + lhs._m22 * rhs._m22 + lhs._m23 * rhs._m32, 42 | lhs._m21 * rhs._m13 + lhs._m22 * rhs._m23 + lhs._m23 * rhs._m33, 43 | lhs._m21 * rhs._m14 + lhs._m22 * rhs._m24 + lhs._m23 * rhs._m34 + lhs._m24, 44 | lhs._m31 * rhs._m11 + lhs._m32 * rhs._m21 + lhs._m33 * rhs._m31, 45 | lhs._m31 * rhs._m12 + lhs._m32 * rhs._m22 + lhs._m33 * rhs._m32, 46 | lhs._m31 * rhs._m13 + lhs._m32 * rhs._m23 + lhs._m33 * rhs._m33, 47 | lhs._m31 * rhs._m14 + lhs._m32 * rhs._m24 + lhs._m33 * rhs._m34 + lhs._m34 48 | ); 49 | } 50 | 51 | public static FixVec3 operator * (FixTrans3 lhs, FixVec3 rhs) { 52 | return new FixVec3( 53 | lhs._m11 * rhs.X + lhs._m12 * rhs.Y + lhs._m13 * rhs.Z + lhs._m14, 54 | lhs._m21 * rhs.X + lhs._m22 * rhs.Y + lhs._m23 * rhs.Z + lhs._m24, 55 | lhs._m31 * rhs.X + lhs._m32 * rhs.Y + lhs._m33 * rhs.Z + lhs._m34 56 | ); 57 | } 58 | 59 | public static FixTrans3 MakeRotationZ (Fix degrees) { 60 | Fix cos = FixMath.Cos(degrees); 61 | Fix sin = FixMath.Sin(degrees); 62 | return new FixTrans3( 63 | cos, -sin, 0, 0, 64 | sin, cos, 0, 0, 65 | 0, 0, 1, 0 66 | ); 67 | } 68 | 69 | public static FixTrans3 MakeRotationY (Fix degrees) { 70 | Fix cos = FixMath.Cos(degrees); 71 | Fix sin = FixMath.Sin(degrees); 72 | return new FixTrans3( 73 | cos, 0, sin, 0, 74 | 0, 1, 0, 0, 75 | -sin, 0, cos, 0 76 | ); 77 | } 78 | 79 | public static FixTrans3 MakeRotationX (Fix degrees) { 80 | Fix cos = FixMath.Cos(degrees); 81 | Fix sin = FixMath.Sin(degrees); 82 | return new FixTrans3( 83 | 1, 0, 0, 0, 84 | 0, cos, -sin, 0, 85 | 0, sin, cos, 0 86 | ); 87 | } 88 | 89 | public static FixTrans3 MakeRotation (FixVec3 degrees) { 90 | return MakeRotationX(degrees.X) 91 | .RotateY(degrees.Y) 92 | .RotateZ(degrees.Z); 93 | } 94 | 95 | public static FixTrans3 MakeScale (FixVec3 scale) { 96 | return new FixTrans3( 97 | scale.X, 0, 0, 0, 98 | 0, scale.Y, 0, 0, 99 | 0, 0, scale.Z, 0 100 | ); 101 | } 102 | 103 | public static FixTrans3 MakeTranslation (FixVec3 delta) { 104 | return new FixTrans3( 105 | 1, 0, 0, delta.X, 106 | 0, 1, 0, delta.Y, 107 | 0, 0, 1, delta.Z 108 | ); 109 | } 110 | 111 | Fix _m11, _m21, _m31, _m12, _m22, _m32, _m13, _m23, _m33, _m14, _m24, _m34; 112 | 113 | public FixTrans3 ( 114 | Fix m11, Fix m12, Fix m13, Fix m14, 115 | Fix m21, Fix m22, Fix m23, Fix m24, 116 | Fix m31, Fix m32, Fix m33, Fix m34 117 | ) { 118 | _m11 = m11; _m12 = m12; _m13 = m13; _m14 = m14; 119 | _m21 = m21; _m22 = m22; _m23 = m23; _m24 = m24; 120 | _m31 = m31; _m32 = m32; _m33 = m33; _m34 = m34; 121 | } 122 | 123 | public FixTrans3 (FixVec3 position, FixVec3 scale, FixVec3 rotation) { 124 | this = MakeRotationX(rotation.X) 125 | .RotateY(rotation.Y) 126 | .RotateZ(rotation.Z) 127 | .Scale(scale) 128 | .Translate(position); 129 | } 130 | 131 | public Fix M11 { get { return _m11; } } 132 | public Fix M12 { get { return _m12; } } 133 | public Fix M13 { get { return _m13; } } 134 | public Fix M14 { get { return _m14; } } 135 | public Fix M21 { get { return _m21; } } 136 | public Fix M22 { get { return _m22; } } 137 | public Fix M23 { get { return _m23; } } 138 | public Fix M24 { get { return _m24; } } 139 | public Fix M31 { get { return _m31; } } 140 | public Fix M32 { get { return _m32; } } 141 | public Fix M33 { get { return _m33; } } 142 | public Fix M34 { get { return _m34; } } 143 | 144 | public FixTrans3 RotateZ (Fix degrees) { 145 | return MakeRotationZ(degrees) * this; 146 | } 147 | 148 | public FixTrans3 RotateY (Fix degrees) { 149 | return MakeRotationY(degrees) * this; 150 | } 151 | 152 | public FixTrans3 RotateX (Fix degrees) { 153 | return MakeRotationX(degrees) * this; 154 | } 155 | 156 | public FixTrans3 Rotate (FixVec3 degrees) { 157 | return MakeRotation(degrees); 158 | } 159 | 160 | public FixTrans3 Scale (FixVec3 scale) { 161 | return new FixTrans3( 162 | _m11 * scale.X, _m12 * scale.X, _m13 * scale.X, _m14 * scale.X, 163 | _m21 * scale.Y, _m22 * scale.Y, _m23 * scale.Y, _m24 * scale.Y, 164 | _m31 * scale.Z, _m32 * scale.Z, _m33 * scale.Z, _m34 * scale.Z 165 | ); 166 | } 167 | 168 | public FixTrans3 Translate (FixVec3 delta) { 169 | return new FixTrans3( 170 | _m11, _m12, _m13, _m14 + delta.X, 171 | _m21, _m22, _m23, _m24 + delta.Y, 172 | _m31, _m32, _m33, _m34 + delta.Z 173 | ); 174 | } 175 | 176 | public FixVec3 Apply (FixVec3 vec) { 177 | return this * vec; 178 | } 179 | 180 | public override string ToString () { 181 | return string.Format ("[[{0}, {1}, {2}], [{3}, {4}, {5}]]", _m11, _m12, _m13, _m21, _m22, _m23); 182 | } 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /FixVec2.cs: -------------------------------------------------------------------------------- 1 | /* FixedPointy - A simple fixed-point math library for C#. 2 | * 3 | * Copyright (c) 2013 Jameson Ernst 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | */ 23 | 24 | using System; 25 | 26 | namespace FixedPointy { 27 | public struct FixVec2 { 28 | public static readonly FixVec2 Zero = new FixVec2(); 29 | public static readonly FixVec2 One = new FixVec2(1, 1); 30 | public static readonly FixVec2 UnitX = new FixVec2(1, 0); 31 | public static readonly FixVec2 UnitY = new FixVec2(0, 1); 32 | 33 | public static FixVec2 operator + (FixVec2 rhs) { 34 | return rhs; 35 | } 36 | public static FixVec2 operator - (FixVec2 rhs) { 37 | return new FixVec2(-rhs._x, -rhs._y); 38 | } 39 | 40 | public static FixVec2 operator + (FixVec2 lhs, FixVec2 rhs) { 41 | return new FixVec2(lhs._x + rhs._x, lhs._y + rhs._y); 42 | } 43 | public static FixVec2 operator - (FixVec2 lhs, FixVec2 rhs) { 44 | return new FixVec2(lhs._x - rhs._x, lhs._y - rhs._y); 45 | } 46 | 47 | public static FixVec2 operator + (FixVec2 lhs, Fix rhs) { 48 | return lhs.ScalarAdd(rhs); 49 | } 50 | public static FixVec2 operator + (Fix lhs, FixVec2 rhs) { 51 | return rhs.ScalarAdd(lhs); 52 | } 53 | public static FixVec2 operator - (FixVec2 lhs, Fix rhs) { 54 | return new FixVec2(lhs._x - rhs, lhs._y - rhs); 55 | } 56 | public static FixVec2 operator * (FixVec2 lhs, Fix rhs) { 57 | return lhs.ScalarMultiply(rhs); 58 | } 59 | public static FixVec2 operator * (Fix lhs, FixVec2 rhs) { 60 | return rhs.ScalarMultiply(lhs); 61 | } 62 | public static FixVec2 operator / (FixVec2 lhs, Fix rhs) { 63 | return new FixVec2(lhs._x / rhs, lhs._y / rhs); 64 | } 65 | 66 | Fix _x, _y; 67 | 68 | public FixVec2 (Fix x, Fix y) { 69 | _x = x; 70 | _y = y; 71 | } 72 | 73 | public Fix X { get { return _x; } } 74 | public Fix Y { get { return _y; } } 75 | 76 | public Fix Dot (FixVec2 rhs) { 77 | return _x * rhs._x + _y * rhs._y; 78 | } 79 | 80 | public Fix Cross (FixVec2 rhs) { 81 | return _x * rhs._y - _y * rhs._x; 82 | } 83 | 84 | FixVec2 ScalarAdd (Fix value) { 85 | return new FixVec2(_x + value, _y + value); 86 | } 87 | FixVec2 ScalarMultiply (Fix value) { 88 | return new FixVec2(_x * value, _y * value); 89 | } 90 | 91 | public Fix GetMagnitude () { 92 | ulong N = (ulong)((long)_x.Raw * (long)_x.Raw + (long)_y.Raw * (long)_y.Raw); 93 | 94 | return new Fix((int)(FixMath.SqrtULong(N << 2) + 1) >> 1); 95 | } 96 | 97 | public FixVec2 Normalize () { 98 | if (_x == 0 && _y == 0) 99 | return FixVec2.Zero; 100 | 101 | var m = GetMagnitude(); 102 | return new FixVec2(_x / m, _y / m); 103 | } 104 | 105 | public override string ToString () { 106 | return string.Format("({0}, {1})", _x, _y); 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /FixVec3.cs: -------------------------------------------------------------------------------- 1 | /* FixedPointy - A simple fixed-point math library for C#. 2 | * 3 | * Copyright (c) 2013 Jameson Ernst 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | */ 23 | 24 | using System; 25 | 26 | namespace FixedPointy { 27 | public struct FixVec3 { 28 | public static readonly FixVec3 Zero = new FixVec3(); 29 | public static readonly FixVec3 One = new FixVec3(1, 1, 1); 30 | public static readonly FixVec3 UnitX = new FixVec3(1, 0, 0); 31 | public static readonly FixVec3 UnitY = new FixVec3(0, 1, 0); 32 | public static readonly FixVec3 UnitZ = new FixVec3(0, 0, 1); 33 | 34 | public static implicit operator FixVec3 (FixVec2 value) { 35 | return new FixVec3(value.X, value.Y, 0); 36 | } 37 | 38 | public static FixVec3 operator + (FixVec3 rhs) { 39 | return rhs; 40 | } 41 | public static FixVec3 operator - (FixVec3 rhs) { 42 | return new FixVec3(-rhs._x, -rhs._y, -rhs._z); 43 | } 44 | 45 | public static FixVec3 operator + (FixVec3 lhs, FixVec3 rhs) { 46 | return new FixVec3(lhs._x + rhs._x, lhs._y + rhs._y, lhs._z + rhs._z); 47 | } 48 | public static FixVec3 operator - (FixVec3 lhs, FixVec3 rhs) { 49 | return new FixVec3(lhs._x - rhs._x, lhs._y - rhs._y, lhs._z - rhs._z); 50 | } 51 | 52 | public static FixVec3 operator + (FixVec3 lhs, Fix rhs) { 53 | return lhs.ScalarAdd(rhs); 54 | } 55 | public static FixVec3 operator + (Fix lhs, FixVec3 rhs) { 56 | return rhs.ScalarAdd(lhs); 57 | } 58 | public static FixVec3 operator - (FixVec3 lhs, Fix rhs) { 59 | return new FixVec3(lhs._x - rhs, lhs._y - rhs, lhs._z - rhs); 60 | } 61 | public static FixVec3 operator * (FixVec3 lhs, Fix rhs) { 62 | return lhs.ScalarMultiply(rhs); 63 | } 64 | public static FixVec3 operator * (Fix lhs, FixVec3 rhs) { 65 | return rhs.ScalarMultiply(lhs); 66 | } 67 | public static FixVec3 operator / (FixVec3 lhs, Fix rhs) { 68 | return new FixVec3(lhs._x / rhs, lhs._y / rhs, lhs._z / rhs); 69 | } 70 | 71 | Fix _x, _y, _z; 72 | 73 | public FixVec3 (Fix x, Fix y, Fix z) { 74 | _x = x; 75 | _y = y; 76 | _z = z; 77 | } 78 | 79 | public Fix X { get { return _x; } } 80 | public Fix Y { get { return _y; } } 81 | public Fix Z { get { return _z; } } 82 | 83 | public Fix Dot (FixVec3 rhs) { 84 | return _x * rhs._x + _y * rhs._y + _z * rhs._z; 85 | } 86 | 87 | public FixVec3 Cross (FixVec3 rhs) { 88 | return new FixVec3( 89 | _y * rhs._z - _z * rhs._y, 90 | _z * rhs._x - _x * rhs._z, 91 | _x * rhs._y - _y * rhs._x 92 | ); 93 | } 94 | 95 | FixVec3 ScalarAdd (Fix value) { 96 | return new FixVec3(_x + value, _y + value, _z + value); 97 | } 98 | FixVec3 ScalarMultiply (Fix value) { 99 | return new FixVec3(_x * value, _y * value, _z * value); 100 | } 101 | 102 | public Fix GetMagnitude () { 103 | ulong N = (ulong)((long)_x.Raw * (long)_x.Raw + (long)_y.Raw * (long)_y.Raw + (long)_z.Raw * (long)_z.Raw); 104 | 105 | return new Fix((int)(FixMath.SqrtULong(N << 2) + 1) >> 1); 106 | } 107 | 108 | public FixVec3 Normalize () { 109 | if (_x == 0 && _y == 0 && _z == 0) 110 | return FixVec3.Zero; 111 | 112 | var m = GetMagnitude(); 113 | return new FixVec3(_x / m, _y / m, _z / m); 114 | } 115 | 116 | public override string ToString () { 117 | return string.Format("({0}, {1}, {2})", _x, _y, _z); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /FixedPointy.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | 10.0.0 7 | 2.0 8 | {11552C15-9E8E-4036-B7F0-EA4919977C01} 9 | Library 10 | FixedPointy 11 | FixedPointy 12 | v4.5 13 | 14 | 15 | true 16 | full 17 | false 18 | bin\Debug 19 | DEBUG; 20 | prompt 21 | 4 22 | x86 23 | false 24 | 25 | 26 | true 27 | bin\Release 28 | prompt 29 | 4 30 | x86 31 | false 32 | 33 | 34 | true 35 | full 36 | false 37 | bin\Debug 38 | DEBUG; 39 | prompt 40 | 4 41 | false 42 | 43 | 44 | true 45 | bin\Release 46 | prompt 47 | 4 48 | false 49 | 50 | 51 | true 52 | full 53 | false 54 | bin\Debug 55 | DEBUG; 56 | prompt 57 | 4 58 | x64 59 | false 60 | 61 | 62 | true 63 | bin\Release 64 | prompt 65 | 4 66 | x64 67 | false 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /FixedPointy.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FixedPointy", "FixedPointy.csproj", "{11552C15-9E8E-4036-B7F0-EA4919977C01}" 5 | EndProject 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scratch", "Scratch\Scratch.csproj", "{E289E228-2478-408C-8E33-DA9F0239CA8C}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x86 = Debug|x86 11 | Release|x86 = Release|x86 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | Debug|x64 = Debug|x64 15 | Release|x64 = Release|x64 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Debug|x64.ActiveCfg = Debug|x64 21 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Debug|x64.Build.0 = Debug|x64 22 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Debug|x86.ActiveCfg = Debug|x86 23 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Debug|x86.Build.0 = Debug|x86 24 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Release|x64.ActiveCfg = Release|x64 27 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Release|x64.Build.0 = Release|x64 28 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Release|x86.ActiveCfg = Release|x86 29 | {11552C15-9E8E-4036-B7F0-EA4919977C01}.Release|x86.Build.0 = Release|x86 30 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Debug|x64.ActiveCfg = Debug|x64 33 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Debug|x64.Build.0 = Debug|x64 34 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Debug|x86.ActiveCfg = Debug|x86 35 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Debug|x86.Build.0 = Debug|x86 36 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Release|x64.ActiveCfg = Release|x64 39 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Release|x64.Build.0 = Release|x64 40 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Release|x86.ActiveCfg = Release|x86 41 | {E289E228-2478-408C-8E33-DA9F0239CA8C}.Release|x86.Build.0 = Release|x86 42 | EndGlobalSection 43 | GlobalSection(MonoDevelopProperties) = preSolution 44 | StartupItem = Scratch\Scratch.csproj 45 | EndGlobalSection 46 | EndGlobal 47 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | FixedPointy - A C# fixed-point math library. 2 | 3 | Copyright (c) 2013 Jameson Ernst 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | FixedPointy 2 | =========== 3 | A simple fixed-point math library for C#. 4 | 5 | All standard math functions are implemented, with the exception of hyperbolic trig. 6 | Precision can be configured by adjusting the FractionalBits constant in Fix.cs, 7 | ranging from Q9.22 through Q23.8 formats. 8 | 9 | Available under MIT license. See LICENSE.txt for details. 10 | -------------------------------------------------------------------------------- /Scratch/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FixedPointy; 3 | 4 | namespace FixedPointy { 5 | public static class Program { 6 | public static void Main (string[] args) { 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Scratch/Scratch.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | x86 6 | 10.0.0 7 | 2.0 8 | {E289E228-2478-408C-8E33-DA9F0239CA8C} 9 | Exe 10 | Scratch 11 | Scratch 12 | v4.5 13 | 14 | 15 | true 16 | full 17 | false 18 | bin\Debug 19 | DEBUG; 20 | prompt 21 | 4 22 | x86 23 | false 24 | 25 | 26 | true 27 | bin\Release 28 | prompt 29 | 4 30 | x86 31 | false 32 | 33 | 34 | true 35 | full 36 | false 37 | bin\Debug 38 | DEBUG; 39 | prompt 40 | 4 41 | false 42 | 43 | 44 | true 45 | bin\Release 46 | prompt 47 | 4 48 | false 49 | 50 | 51 | true 52 | full 53 | false 54 | bin\Debug 55 | DEBUG; 56 | prompt 57 | 4 58 | x64 59 | false 60 | 61 | 62 | true 63 | bin\Release 64 | prompt 65 | 4 66 | x64 67 | false 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | {11552C15-9E8E-4036-B7F0-EA4919977C01} 76 | FixedPointy 77 | 78 | 79 | 80 | 81 | 82 | --------------------------------------------------------------------------------