Skip to content

Conversions from BigInteger to Double are not always as precise as from Long #49611

@SingleAccretion

Description

@SingleAccretion

Description

The gist of the issue is that some precision is apparently lost when using the explicit conversion operator operator double(BigInteger integer), as compared to a direct cast from long. Here's a very simple reproduction:

using System;
using System.Numerics;

BigInteger a = long.MaxValue / 2;

Console.WriteLine($"Source: {a}");
Console.WriteLine($"BigInt: {(double)a:F0}");
Console.WriteLine($"Long:   {(double)(long)a:F0}");

This will print:

Source: 4611686018427387903
BigInt: 4611686018427387392
Long:   4611686018427387904

Configuration

Tested with the latest bits from dotnet/installer - 6.0.100-preview.3.21164.5 on Windows 10 x64. I do not believe this is an architecture-specific issue as all the code that does it is fully managed and uses simple arithmetic.

Regression?

Not a regression - the reproduction works on net6.0, net5.0 and netcoreapp3.1. It appears that the code responsible for the conversion hasn't been touched in a long time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Numericsbugin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions