-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNumber.cs
370 lines (312 loc) · 12.3 KB
/
Number.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using static System.Runtime.CompilerServices.MethodImplOptions;
namespace Plato
{
/// <summary>
/// A simple wrapper around the built-in <c>float</c> type,
/// forwarding all arithmetic and common methods to <c>float</c>.
/// </summary>
[DataContract]
public partial struct Number
{
// -------------------------------------------------------------------------------
// Field (the wrapped float)
// -------------------------------------------------------------------------------
[DataMember] public readonly float Value;
// -------------------------------------------------------------------------------
// Constructors
// -------------------------------------------------------------------------------
[MethodImpl(AggressiveInlining)]
public Number(float value) => Value = value;
// -------------------------------------------------------------------------------
// Convert to/from float
// -------------------------------------------------------------------------------
[MethodImpl(AggressiveInlining)]
public static Number FromSystem(float f) => new(f);
[MethodImpl(AggressiveInlining)]
public static implicit operator Number(float f) => FromSystem(f);
[MethodImpl(AggressiveInlining)]
public static implicit operator float(Number n) => n.Value;
// -------------------------------------------------------------------------------
// Operators (forward to float)
// -------------------------------------------------------------------------------
[MethodImpl(AggressiveInlining)]
public static Number operator +(Number a, Number b)
=> a.Value + b.Value;
[MethodImpl(AggressiveInlining)]
public static Number operator -(Number a, Number b)
=> a.Value - b.Value;
[MethodImpl(AggressiveInlining)]
public static Number operator *(Number a, Number b)
=> a.Value * b.Value;
[MethodImpl(AggressiveInlining)]
public static Number operator /(Number a, Number b)
=> a.Value / b.Value;
[MethodImpl(AggressiveInlining)]
public static Number operator -(Number n)
=> -n.Value;
[MethodImpl(AggressiveInlining)]
public static Boolean operator <(Number a, Number b)
=> a.Value < b.Value;
[MethodImpl(AggressiveInlining)]
public static Boolean operator <=(Number a, Number b)
=> a.Value <= b.Value;
[MethodImpl(AggressiveInlining)]
public static Boolean operator >(Number a, Number b)
=> a.Value > b.Value;
[MethodImpl(AggressiveInlining)]
public static Boolean operator >=(Number a, Number b)
=> a.Value >= b.Value;
[MethodImpl(AggressiveInlining)]
public Integer CompareTo(Number other)
=> Value.CompareTo(other.Value);
//-------------------------------------------------------------------------------
// Math Intrinsic Functions
//-------------------------------------------------------------------------------
/// <summary>
/// The absolute value of a single-precision floating-point number.
/// </summary>
public Number Abs
{
[MethodImpl(AggressiveInlining)] get => MathF.Abs(Value);
}
/// <summary>
/// The angle whose cosine is the specified number.
/// </summary>
public Angle Acos
{
[MethodImpl(AggressiveInlining)] get => MathF.Acos(Value);
}
/// <summary>
/// The angle whose hyperbolic cosine is the specified number.
/// </summary>
public Angle Acosh
{
[MethodImpl(AggressiveInlining)] get => MathF.Acosh(Value);
}
/// <summary>
/// The angle whose sine is the specified number.
/// </summary>
public Angle Asin
{
[MethodImpl(AggressiveInlining)] get => MathF.Asin(Value);
}
/// <summary>
/// The angle whose hyperbolic sine is the specified number.
/// </summary>
public Angle Asinh
{
[MethodImpl(AggressiveInlining)] get => MathF.Asinh(Value);
}
/// <summary>
/// The angle whose tangent is the specified number.
/// </summary>
public Angle Atan
{
[MethodImpl(AggressiveInlining)] get => MathF.Atan(Value);
}
/// <summary>
/// The angle whose tangent is the quotient of this number with another.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Angle Atan2(Number x) => MathF.Atan2(Value, x);
/// <summary>
/// The angle whose hyperbolic tangent is the specified number.
/// </summary>
public Angle Atanh
{
[MethodImpl(AggressiveInlining)] get => MathF.Atanh(Value);
}
/// <summary>
/// The largest value that compares less than the value.
/// </summary>
public Number BitDecrement
{
[MethodImpl(AggressiveInlining)] get => MathF.BitDecrement(Value);
}
/// <summary>
/// The smallest value that compares greater than the value.
/// </summary>
public Number BitIncrement
{
[MethodImpl(AggressiveInlining)] get => MathF.BitIncrement(Value);
}
/// <summary>
/// The cube root of the number.
/// </summary>
public Number Cbrt
{
[MethodImpl(AggressiveInlining)] get => MathF.Cbrt(Value);
}
/// <summary>
/// The smallest integral value that is greater than or equal to the specified single-precision floating-point number.
/// </summary>
public Number Ceiling
{
[MethodImpl(AggressiveInlining)] get => MathF.Ceiling(Value);
}
/// <summary>
/// A value with the magnitude of this number and the sign of another number.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number CopySign(Number y) => MathF.CopySign(Value, y);
/// <summary>
/// Returns e raised to the specified power.
/// </summary>
public Number Exp
{
[MethodImpl(AggressiveInlining)] get => MathF.Exp(Value);
}
/// <summary>
/// The largest integral value less than or equal to this value.
/// </summary>
public Number Floor
{
[MethodImpl(AggressiveInlining)] get => MathF.Floor(Value);
}
/// <summary>
/// Returns (Value * y) + z, rounded as one ternary operation.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number FusedMultiplyAdd(Number y, Number z) => MathF.FusedMultiplyAdd(Value, y, z);
/// <summary>
/// The remainder resulting from the division of by another number.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number IEEERemainder(Number y) => MathF.IEEERemainder(Value, y);
/// <summary>
/// The base 2 integer logarithm of the number.
/// </summary>
public Number ILogB
{
[MethodImpl(AggressiveInlining)] get => MathF.ILogB(Value);
}
/// <summary>
/// The logarithm of the number in the base.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Log(Number newBase) => MathF.Log(Value, newBase);
/// <summary>
/// The natural (base e) logarithm of the number.
/// </summary>
public Number NaturalLog
{
[MethodImpl(AggressiveInlining)] get => MathF.Log(Value);
}
/// <summary>
/// The base 10 logarithm of the number.
/// </summary>
public Number Log10
{
[MethodImpl(AggressiveInlining)] get => MathF.Log10(Value);
}
/// <summary>
/// The base 2 logarithm of the number.
/// </summary>
public Number Log2
{
[MethodImpl(AggressiveInlining)] get => MathF.Log2(Value);
}
/// <summary>
/// The larger of two single-precision floating-point numbers.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Max(Number other) => MathF.Max(Value, other);
/// <summary>
/// The larger magnitude of two single-precision floating-point numbers.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number MaxMagnitude(Number other) => MathF.MaxMagnitude(Value, other);
/// <summary>
/// The smaller of two single-precision floating-point numbers.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Min(Number other) => MathF.Min(Value, other);
/// <summary>
/// The smaller magnitude of two single-precision floating-point numbers.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number MinMagnitude(Number other) => MathF.MinMagnitude(Value, other);
/// <summary>
/// The number raised to the specified power.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Pow(Number power) => MathF.Pow(Value, power);
/// <summary>
/// Clamp number within the range of two numbers.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Clamp(Number a, Number b) => MathF.Max(a, MathF.Min(Value, b));
/// <summary>
/// The reciprocal of the number.
/// </summary>
public Number Reciprocal
{
[MethodImpl(AggressiveInlining)] get => 1f / Value;
}
/// <summary>
/// An estimate of the reciprocal of the number.
/// </summary>
public Number ReciprocalEstimate
{
[MethodImpl(AggressiveInlining)] get => MathF.ReciprocalEstimate(Value);
}
/// <summary>
/// An estimate of the reciprocal square root of the number.
/// </summary>
public Number ReciprocalSquareRootEstimate
{
[MethodImpl(AggressiveInlining)] get => MathF.ReciprocalSqrtEstimate(Value);
}
/// <summary>
/// Rounds the value to an integer using the specified rounding convention.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number RoundToZero(int digits = 0) => MathF.Round(Value, digits, MidpointRounding.ToZero);
/// <summary>
/// Rounds the value to an integer using the specified rounding convention.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number RoundAwayFromZero(int digits = 0) => MathF.Round(Value, digits, MidpointRounding.AwayFromZero);
/// <summary>
/// Rounds the value to the nearest integral value, rounding midpoint values to the nearest even number.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Round(int digits = 0) => MathF.Round(Value, digits);
/// <summary>
/// Returns x multiplied by 2 raised to the power of n, computed efficiently.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number ScaleB(Integer n) => MathF.ScaleB(Value, n);
/// <summary>
/// An integer that indicates the sign of the number.
/// </summary>
public Integer Sign
{
[MethodImpl(AggressiveInlining)] get => MathF.Sign(Value);
}
/// <summary>
/// The square root of the number.
/// </summary>
public Number SquareRoot
{
[MethodImpl(AggressiveInlining)] get => MathF.Sqrt(Value);
}
/// <summary>
/// The square root of the number.
/// </summary>
public Number Square
{
[MethodImpl(AggressiveInlining)]
get => Value * Value;
}
/// <summary>
/// Calculates the integral part of the single-precision floating-point number.
/// </summary>
public Number Truncate
{
[MethodImpl(AggressiveInlining)] get => MathF.Truncate(Value);
}
}
}