Skip to content

Commit 7a72c5f

Browse files
authored
Update BigRational to be compatible with .NET 2.1 (#2277)
* Avoid Double.IsSubnormal and BitConverter.DoubleToUInt64Bits * Update DafnyRuntime version to 1.2.0 * Update release notes Fixes #2276
1 parent c85bbd1 commit 7a72c5f

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Upcoming
22

3+
- fix: The Dafny runtime library for C# is now compatible with .NET Standard 2.1, as it was before 3.7.0. Its version has been updated to 1.2.0 to reflect this.
4+
35
# 3.7.0
46

57
- feat: The Dafny CLI, Dafny as a library, and the C# runtime are now available on NuGet. You can install the CLI with `dotnet tool install --global Dafny`. The library is available as `DafnyPipeline` and the runtime is available as `DafnyRuntime`. (https://github.com/dafny-lang/dafny/pull/2051)

Source/DafnyRuntime/DafnyRuntime.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,23 +1583,25 @@ public BigRational(double n) {
15831583
throw new ArgumentException(
15841584
"Can't convert +/- infinity to a rational.");
15851585
}
1586-
if (Double.IsSubnormal(n)) {
1587-
throw new ArgumentException(
1588-
"Can't convert a subnormal value to a rational (yet).");
1589-
}
15901586

15911587
// Double-specific values
15921588
const int exptBias = 1023;
15931589
const ulong signMask = 0x8000000000000000;
15941590
const ulong exptMask = 0x7FF0000000000000;
15951591
const ulong mantMask = 0x000FFFFFFFFFFFFF;
15961592
const int mantBits = 52;
1597-
ulong bits = BitConverter.DoubleToUInt64Bits(n);
1593+
ulong bits = BitConverter.ToUInt64(BitConverter.GetBytes(n), 0);
15981594

15991595
// Generic conversion
16001596
bool isNeg = (bits & signMask) != 0;
16011597
int expt = ((int)((bits & exptMask) >> mantBits)) - exptBias;
16021598
var mant = (bits & mantMask);
1599+
1600+
if (expt == -exptBias && mant != 0) {
1601+
throw new ArgumentException(
1602+
"Can't convert a subnormal value to a rational (yet).");
1603+
}
1604+
16031605
var one = BigInteger.One;
16041606
var negFactor = isNeg ? BigInteger.Negate(one) : one;
16051607
var two = new BigInteger(2);

Source/DafnyRuntime/DafnyRuntime.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
88
<DefineConstants>TRACE;ISDAFNYRUNTIMELIB</DefineConstants>
9-
<PackageVersion>1.1.0</PackageVersion>
9+
<PackageVersion>1.2.0</PackageVersion>
1010
<TargetFramework>net6.0</TargetFramework>
1111
<OutputPath>..\..\Binaries\</OutputPath>
1212
<LangVersion>7.3</LangVersion>

0 commit comments

Comments
 (0)