Skip to content

Commit

Permalink
Fix HalfNumberBufferLength (#100187)
Browse files Browse the repository at this point in the history
* Fix HalfNumberBufferLength

* Add regression test
  • Loading branch information
huoyaoyuan committed Mar 25, 2024
1 parent 13a9036 commit 06650b7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Number.NumberBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static partial class Number
internal const int Int64NumberBufferLength = 19 + 1; // 19 for the longest input: 9,223,372,036,854,775,807
internal const int Int128NumberBufferLength = 39 + 1; // 39 for the longest input: 170,141,183,460,469,231,731,687,303,715,884,105,727
internal const int SingleNumberBufferLength = 112 + 1 + 1; // 112 for the longest input + 1 for rounding: 1.40129846E-45
internal const int HalfNumberBufferLength = 21; // 19 for the longest input + 1 for rounding (+1 for the null terminator)
internal const int HalfNumberBufferLength = 21 + 1 + 1; // 21 for the longest input + 1 for rounding: 0.000122010707855224609375
internal const int UInt32NumberBufferLength = 10 + 1; // 10 for the longest input: 4,294,967,295
internal const int UInt64NumberBufferLength = 20 + 1; // 20 for the longest input: 18,446,744,073,709,551,615
internal const int UInt128NumberBufferLength = 39 + 1; // 39 for the longest input: 340,282,366,920,938,463,463,374,607,431,768,211,455
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,14 @@ public static void ToStringRoundtrip_R(object o_value)
AssertExtensions.Equal((Half)value, result);
}

[Fact] // https://github.com/dotnet/runtime/issues/98841
public static void ToString_MaxPrecision()
{
Half value = BitConverter.Int16BitsToHalf(0x07FF);
string str = value.ToString("F24");
Assert.Equal("0.000122010707855224609375", str);
}

public static IEnumerable<object[]> RoundTripFloat_CornerCases()
{
// Magnitude smaller than 2^-24 maps to 0
Expand Down

0 comments on commit 06650b7

Please sign in to comment.