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

Seeding data with double values can lose precision #15980

Closed
edwiles opened this issue Jun 6, 2019 · 2 comments
Closed

Seeding data with double values can lose precision #15980

edwiles opened this issue Jun 6, 2019 · 2 comments

Comments

@edwiles
Copy link

edwiles commented Jun 6, 2019

If HasData is used to seed fields of type double, the generated migration often loses precision in the values.

Steps to reproduce

  1. Create an entity with a double property:
public class MyEntity
{
    public int Id { get; set; }
    public double Data { get; set; }
}
  1. Seed the data during OnModelCreating:
    modelBuilder.Entity<MyEntity>().HasData(new[]
        {
            new MyEntity { Id = 1, Data = 0.00404686 },
            new MyEntity { Id = 2, Data = 79.8978 },
        });
  1. Add an initial migration:
dotnet ef migrations add InitialCreate

The generated migration seeds the data but uses imprecise values:

            migrationBuilder.InsertData(
                table: "MyEntities",
                columns: new[] { "Id", "Data" },
                values: new object[] { 1, 0.0040468600000000002 });

            migrationBuilder.InsertData(
                table: "MyEntities",
                columns: new[] { "Id", "Data" },
                values: new object[] { 2, 79.897800000000004 });
  1. Change "double" to "float" in MyEntity, and put an "f" beside the floating-point values in HasData. This results in precise floats being generated.

For reference, our system's first migration contains the doubles precisely and it was generated using version 2.1.1.

Further technical details

EF Core version: 2.2.4
.NET SDK: 2.2.104
Database Provider: Microsoft.EntityFrameworkCore.Sqlite (though in our "real" code we use SqlServer)
Operating system: Windows 10 Enterprise
IDE: Visual Studio 15.9.7

@ajcvickers
Copy link
Contributor

Related to #14952

@ajcvickers
Copy link
Contributor

@edwiles We discussed this in triage and we don't believe there is any error here. The nature of floating point representations means that values often cannot be represented exactly. This means that 0.00404686 as a C# literal is exactly the same value as 0.0040468600000000002. (This is pretty easy to check.)

The change we made from 2.1 is to avoid other cases of data loss by always writing the literals out with full precision.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants