Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UInt64.CreateSaturating<Int128> truncates instead of saturates #96369

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libraries/System.Private.CoreLib/src/System/Int128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1904,7 +1904,8 @@ static bool INumberBase<Int128>.TryConvertToSaturating<TOther>(Int128 value, [Ma
}
else if (typeof(TOther) == typeof(ulong))
{
ulong actualResult = (value <= 0) ? ulong.MinValue : (ulong)value;
ulong actualResult = (value <= 0) ? ulong.MinValue :
(value >= ulong.MaxValue) ? ulong.MaxValue : (ulong)value;
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
result = (TOther)(object)actualResult;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ public static void CreateSaturatingFromInt128Test()
Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.MaxValue));
Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.MinValue));
Assert.Equal((ulong)0x0000_0000_0000_0000, NumberBaseHelper<ulong>.CreateSaturating<Int128>(Int128.NegativeOne));
Assert.Equal((ulong)0xFFFF_FFFF_FFFF_FFFF, NumberBaseHelper<ulong>.CreateSaturating<Int128>((Int128)ulong.MaxValue + (Int128)10L));
}

[Fact]
Expand Down