-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathIFloatingPoint.cs
More file actions
241 lines (214 loc) · 14.7 KB
/
Copy pathIFloatingPoint.cs
File metadata and controls
241 lines (214 loc) · 14.7 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace System.Numerics
{
/// <summary>Defines a floating-point type.</summary>
/// <typeparam name="TSelf">The type that implements the interface.</typeparam>
public interface IFloatingPoint<TSelf>
: IFloatingPointConstants<TSelf>,
INumber<TSelf>,
ISignedNumber<TSelf>
where TSelf : IFloatingPoint<TSelf>?
{
/// <summary>Computes the ceiling of a value.</summary>
/// <param name="x">The value whose ceiling is to be computed.</param>
/// <returns>The ceiling of <paramref name="x" />.</returns>
static virtual TSelf Ceiling(TSelf x) => TSelf.Round(x, digits: 0, MidpointRounding.ToPositiveInfinity);
/// <summary>Computes the floor of a value.</summary>
/// <param name="x">The value whose floor is to be computed.</param>
/// <returns>The floor of <paramref name="x" />.</returns>
static virtual TSelf Floor(TSelf x) => TSelf.Round(x, digits: 0, MidpointRounding.ToNegativeInfinity);
/// <summary>Rounds a value to the nearest integer using the default rounding mode (<see cref="MidpointRounding.ToEven" />).</summary>
/// <param name="x">The value to round.</param>
/// <returns>The result of rounding <paramref name="x" /> to the nearest integer using the default rounding mode.</returns>
static virtual TSelf Round(TSelf x) => TSelf.Round(x, digits: 0, MidpointRounding.ToEven);
/// <summary>Rounds a value to a specified number of fractional-digits using the default rounding mode (<see cref="MidpointRounding.ToEven" />).</summary>
/// <param name="x">The value to round.</param>
/// <param name="digits">The number of fractional digits to which <paramref name="x" /> should be rounded.</param>
/// <returns>The result of rounding <paramref name="x" /> to <paramref name="digits" /> fractional-digits using the default rounding mode.</returns>
static virtual TSelf Round(TSelf x, int digits) => TSelf.Round(x, digits, MidpointRounding.ToEven);
/// <summary>Rounds a value to the nearest integer using the specified rounding mode.</summary>
/// <param name="x">The value to round.</param>
/// <param name="mode">The mode under which <paramref name="x" /> should be rounded.</param>
/// <returns>The result of rounding <paramref name="x" /> to the nearest integer using <paramref name="mode" />.</returns>
static virtual TSelf Round(TSelf x, MidpointRounding mode) => TSelf.Round(x, digits: 0, mode);
/// <summary>Rounds a value to a specified number of fractional-digits using the default rounding mode (<see cref="MidpointRounding.ToEven" />).</summary>
/// <param name="x">The value to round.</param>
/// <param name="digits">The number of fractional digits to which <paramref name="x" /> should be rounded.</param>
/// <param name="mode">The mode under which <paramref name="x" /> should be rounded.</param>
/// <returns>The result of rounding <paramref name="x" /> to <paramref name="digits" /> fractional-digits using <paramref name="mode" />.</returns>
static abstract TSelf Round(TSelf x, int digits, MidpointRounding mode);
/// <summary>Truncates a value.</summary>
/// <param name="x">The value to truncate.</param>
/// <returns>The truncation of <paramref name="x" />.</returns>
static virtual TSelf Truncate(TSelf x) => TSelf.Round(x, digits: 0, MidpointRounding.ToZero);
/// <summary>Gets the number of bytes that will be written as part of <see cref="TryWriteExponentLittleEndian(Span{byte}, out int)" />.</summary>
/// <returns>The number of bytes that will be written as part of <see cref="TryWriteExponentLittleEndian(Span{byte}, out int)" />.</returns>
int GetExponentByteCount();
/// <summary>Gets the length, in bits, of the shortest two's complement representation of the current exponent.</summary>
/// <returns>The length, in bits, of the shortest two's complement representation of the current exponent.</returns>
int GetExponentShortestBitLength();
/// <summary>Gets the length, in bits, of the current significand.</summary>
/// <returns>The length, in bits, of the current significand.</returns>
int GetSignificandBitLength();
/// <summary>Gets the number of bytes that will be written as part of <see cref="TryWriteSignificandLittleEndian(Span{byte}, out int)" />.</summary>
/// <returns>The number of bytes that will be written as part of <see cref="TryWriteSignificandLittleEndian(Span{byte}, out int)" />.</returns>
int GetSignificandByteCount();
/// <summary>Tries to write the current exponent, in big-endian format, to a given span.</summary>
/// <param name="destination">The span to which the current exponent should be written.</param>
/// <param name="bytesWritten">The number of bytes written to <paramref name="destination" />.</param>
/// <returns><c>true</c> if the exponent was successfully written to <paramref name="destination" />; otherwise, <c>false</c>.</returns>
bool TryWriteExponentBigEndian(Span<byte> destination, out int bytesWritten);
/// <summary>Tries to write the current exponent, in little-endian format, to a given span.</summary>
/// <param name="destination">The span to which the current exponent should be written.</param>
/// <param name="bytesWritten">The number of bytes written to <paramref name="destination" />.</param>
/// <returns><c>true</c> if the exponent was successfully written to <paramref name="destination" />; otherwise, <c>false</c>.</returns>
bool TryWriteExponentLittleEndian(Span<byte> destination, out int bytesWritten);
/// <summary>Tries to write the current significand, in big-endian format, to a given span.</summary>
/// <param name="destination">The span to which the current significand should be written.</param>
/// <param name="bytesWritten">The number of bytes written to <paramref name="destination" />.</param>
/// <returns><c>true</c> if the significand was successfully written to <paramref name="destination" />; otherwise, <c>false</c>.</returns>
bool TryWriteSignificandBigEndian(Span<byte> destination, out int bytesWritten);
/// <summary>Tries to write the current significand, in little-endian format, to a given span.</summary>
/// <param name="destination">The span to which the current significand should be written.</param>
/// <param name="bytesWritten">The number of bytes written to <paramref name="destination" />.</param>
/// <returns><c>true</c> if the significand was successfully written to <paramref name="destination" />; otherwise, <c>false</c>.</returns>
bool TryWriteSignificandLittleEndian(Span<byte> destination, out int bytesWritten);
/// <summary>Writes the current exponent, in big-endian format, to a given array.</summary>
/// <param name="destination">The array to which the current exponent should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" />.</returns>
int WriteExponentBigEndian(byte[] destination)
{
if (!TryWriteExponentBigEndian(destination, out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current exponent, in big-endian format, to a given array.</summary>
/// <param name="destination">The array to which the current exponent should be written.</param>
/// <param name="startIndex">The starting index at which the exponent should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" /> starting at <paramref name="startIndex" />.</returns>
int WriteExponentBigEndian(byte[] destination, int startIndex)
{
if (!TryWriteExponentBigEndian(destination.AsSpan(startIndex), out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current exponent, in big-endian format, to a given span.</summary>
/// <param name="destination">The span to which the current exponent should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" />.</returns>
int WriteExponentBigEndian(Span<byte> destination)
{
if (!TryWriteExponentBigEndian(destination, out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current exponent, in little-endian format, to a given array.</summary>
/// <param name="destination">The array to which the current exponent should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" />.</returns>
int WriteExponentLittleEndian(byte[] destination)
{
if (!TryWriteExponentLittleEndian(destination, out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current exponent, in little-endian format, to a given array.</summary>
/// <param name="destination">The array to which the current exponent should be written.</param>
/// <param name="startIndex">The starting index at which the exponent should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" /> starting at <paramref name="startIndex" />.</returns>
int WriteExponentLittleEndian(byte[] destination, int startIndex)
{
if (!TryWriteExponentLittleEndian(destination.AsSpan(startIndex), out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current exponent, in little-endian format, to a given span.</summary>
/// <param name="destination">The span to which the current exponent should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" />.</returns>
int WriteExponentLittleEndian(Span<byte> destination)
{
if (!TryWriteExponentLittleEndian(destination, out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current significand, in big-endian format, to a given array.</summary>
/// <param name="destination">The array to which the current significand should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" />.</returns>
int WriteSignificandBigEndian(byte[] destination)
{
if (!TryWriteSignificandBigEndian(destination, out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current significand, in big-endian format, to a given array.</summary>
/// <param name="destination">The array to which the current significand should be written.</param>
/// <param name="startIndex">The starting index at which the significand should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" /> starting at <paramref name="startIndex" />.</returns>
int WriteSignificandBigEndian(byte[] destination, int startIndex)
{
if (!TryWriteSignificandBigEndian(destination.AsSpan(startIndex), out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current significand, in big-endian format, to a given span.</summary>
/// <param name="destination">The span to which the current significand should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" />.</returns>
int WriteSignificandBigEndian(Span<byte> destination)
{
if (!TryWriteSignificandBigEndian(destination, out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current significand, in little-endian format, to a given array.</summary>
/// <param name="destination">The array to which the current significand should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" />.</returns>
int WriteSignificandLittleEndian(byte[] destination)
{
if (!TryWriteSignificandLittleEndian(destination, out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current significand, in little-endian format, to a given array.</summary>
/// <param name="destination">The array to which the current significand should be written.</param>
/// <param name="startIndex">The starting index at which the significand should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" /> starting at <paramref name="startIndex" />.</returns>
int WriteSignificandLittleEndian(byte[] destination, int startIndex)
{
if (!TryWriteSignificandLittleEndian(destination.AsSpan(startIndex), out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
/// <summary>Writes the current significand, in little-endian format, to a given span.</summary>
/// <param name="destination">The span to which the current significand should be written.</param>
/// <returns>The number of bytes written to <paramref name="destination" />.</returns>
int WriteSignificandLittleEndian(Span<byte> destination)
{
if (!TryWriteSignificandLittleEndian(destination, out int bytesWritten))
{
ThrowHelper.ThrowArgumentException_DestinationTooShort();
}
return bytesWritten;
}
}
}