Skip to content

Commit acd0eb7

Browse files
committed
Sqrt(negative number) throws ArgumentOutOfRangeException
1 parent 5d368c3 commit acd0eb7

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

Fix64.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public static Fix64 FastMul(Fix64 x, Fix64 y) {
264264
}
265265

266266
[MethodImplAttribute(MethodImplOptions.AggressiveInlining)]
267-
static int Clz(ulong x) {
267+
static int CountLeadingZeroes(ulong x) {
268268
int result = 0;
269269
while ((x & 0xF000000000000000) == 0) { result += 4; x <<= 4; }
270270
while ((x & 0x8000000000000000) == 0) { result += 1; x <<= 1; }
@@ -292,7 +292,7 @@ static int Clz(ulong x) {
292292
}
293293

294294
while (remainder != 0 && bitPos >= 0) {
295-
int shift = Clz(remainder);
295+
int shift = CountLeadingZeroes(remainder);
296296
if (shift > bitPos) {
297297
shift = bitPos;
298298
}
@@ -368,14 +368,16 @@ public static Fix64 FastMod(Fix64 x, Fix64 y) {
368368

369369
/// <summary>
370370
/// Returns the square root of a specified number.
371-
/// Throws an ArgumentException if the number is negative.
372371
/// </summary>
372+
/// <exception cref="ArgumentOutOfRangeException">
373+
/// The argument was negative.
374+
/// </exception>
373375
public static Fix64 Sqrt(Fix64 x) {
374376
var xl = x.m_rawValue;
375377
if (xl < 0) {
376378
// We cannot represent infinities like Single and Double, and Sqrt is
377379
// mathematically undefined for x < 0. So we just throw an exception.
378-
throw new ArgumentException("Negative value passed to Sqrt", "x");
380+
throw new ArgumentOutOfRangeException("Negative value passed to Sqrt", "x");
379381
}
380382

381383
var num = (ulong)xl;

Fix64Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void Sqrt() {
312312
for (int i = 0; i < m_testCases.Length; ++i) {
313313
var f = Fix64.FromRaw(m_testCases[i]);
314314
if (Fix64.Sign(f) < 0) {
315-
Assert.Throws<ArgumentException>(() => Fix64.Sqrt(f));
315+
Assert.Throws<ArgumentOutOfRangeException>(() => Fix64.Sqrt(f));
316316
}
317317
else {
318318
var expected = Math.Sqrt((double)f);

0 commit comments

Comments
 (0)