This repository has been archived by the owner on May 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathFix.cs
191 lines (154 loc) · 6.2 KB
/
Fix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* FixedPointy - A simple fixed-point math library for C#.
*
* Copyright (c) 2013 Jameson Ernst
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Text;
namespace FixedPointy {
public struct Fix {
internal const int FRACTIONAL_BITS = 16;
internal const int INTEGER_BITS = sizeof(int) * 8 - FRACTIONAL_BITS;
internal const int FRACTION_MASK = (int)(uint.MaxValue >> INTEGER_BITS);
internal const int INTEGER_MASK = (int)(-1 & ~FRACTION_MASK);
internal const int FRACTION_RANGE = FRACTION_MASK + 1;
internal const int MIN_INTEGER = int.MinValue >> FRACTIONAL_BITS;
internal const int MAX_INTEGER = int.MaxValue >> FRACTIONAL_BITS;
public static readonly Fix Zero = new Fix(0);
public static readonly Fix One = new Fix(FRACTION_RANGE);
public static readonly Fix MinValue = new Fix(int.MinValue);
public static readonly Fix MaxValue = new Fix(int.MaxValue);
public static readonly Fix Epsilon = new Fix(1);
static Fix () {
if (FRACTIONAL_BITS < 8)
throw new Exception("Fix must have at least 8 fractional bits.");
if (INTEGER_BITS < 10)
throw new Exception("Fix must have at least 10 integer bits.");
if (FRACTIONAL_BITS % 2 == 1)
throw new Exception("Fix must have an even number of fractional and integer bits.");
}
public static int FractionalBits { get { return FRACTIONAL_BITS; } }
public static int IntegerBits { get { return INTEGER_BITS; } }
public static int FractionMask { get { return FRACTION_MASK; } }
public static int IntegerMask { get { return INTEGER_MASK; } }
public static int FractionRange { get { return FRACTION_RANGE; } }
public static int MinInteger { get { return MIN_INTEGER; } }
public static int MaxInteger { get { return MAX_INTEGER; } }
public static Fix Mix (int integer, int numerator, int denominator) {
if (numerator < 0 || denominator < 0)
throw new ArgumentException("Ratio must be positive.");
int fraction = ((int)((long)FRACTION_RANGE * numerator / denominator) & FRACTION_MASK);
fraction = integer < 0 ? -fraction : fraction;
return new Fix((integer << FRACTIONAL_BITS) + fraction);
}
public static Fix Ratio (int numerator, int denominator) {
return new Fix((int)((((long)numerator << (FRACTIONAL_BITS + 1)) / (long)denominator + 1) >> 1));
}
public static explicit operator double (Fix value) {
return (double)(value._raw >> FRACTIONAL_BITS) + (value._raw & FRACTION_MASK) / (double)FRACTION_RANGE;
}
public static explicit operator float (Fix value) {
return (float)(double)value;
}
public static explicit operator int (Fix value) {
if (value._raw > 0)
return value._raw >> FRACTIONAL_BITS;
else
return (value._raw + FRACTION_MASK) >> FRACTIONAL_BITS;
}
public static implicit operator Fix (int value) {
return new Fix(value << FRACTIONAL_BITS);
}
public static bool operator == (Fix lhs, Fix rhs) {
return lhs._raw == rhs._raw;
}
public static bool operator != (Fix lhs, Fix rhs) {
return lhs._raw != rhs._raw;
}
public static bool operator > (Fix lhs, Fix rhs) {
return lhs._raw > rhs._raw;
}
public static bool operator >= (Fix lhs, Fix rhs) {
return lhs._raw >= rhs._raw;
}
public static bool operator < (Fix lhs, Fix rhs) {
return lhs._raw < rhs._raw;
}
public static bool operator <= (Fix lhs, Fix rhs) {
return lhs._raw <= rhs._raw;
}
public static Fix operator + (Fix value) {
return value;
}
public static Fix operator - (Fix value) {
return new Fix(-value._raw);
}
public static Fix operator + (Fix lhs, Fix rhs) {
return new Fix(lhs._raw + rhs._raw);
}
public static Fix operator - (Fix lhs, Fix rhs) {
return new Fix(lhs._raw - rhs._raw);
}
public static Fix operator * (Fix lhs, Fix rhs) {
return new Fix((int)(((long)lhs._raw * (long)rhs._raw + (FRACTION_RANGE >> 1)) >> FRACTIONAL_BITS));
}
public static Fix operator / (Fix lhs, Fix rhs) {
return new Fix((int)((((long)lhs._raw << (FRACTIONAL_BITS + 1)) / (long)rhs._raw + 1) >> 1));
}
public static Fix operator % (Fix lhs, Fix rhs) {
return new Fix(lhs.Raw % rhs.Raw);
}
public static Fix operator << (Fix lhs, int rhs) {
return new Fix(lhs.Raw << rhs);
}
public static Fix operator >> (Fix lhs, int rhs) {
return new Fix(lhs.Raw >> rhs);
}
int _raw;
public Fix (int raw) {
_raw = raw;
}
public int Raw { get { return _raw; } }
public override bool Equals (object obj) {
return (obj is Fix && ((Fix)obj) == this);
}
public override int GetHashCode () {
return Raw.GetHashCode();
}
public override string ToString () {
var sb = new StringBuilder();
if (_raw < 0)
sb.Append(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NegativeSign);
int abs = (int)this;
abs = abs < 0 ? -abs : abs;
sb.Append(abs.ToString());
ulong fraction = (ulong)(_raw & FRACTION_MASK);
if (fraction == 0)
return sb.ToString();
fraction = _raw < 0 ? FRACTION_RANGE - fraction : fraction;
fraction *= 1000000L;
fraction += FRACTION_RANGE >> 1;
fraction >>= FRACTIONAL_BITS;
sb.Append(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
sb.Append(fraction.ToString("D6").TrimEnd('0'));
return sb.ToString();
}
}
}