Skip to content

Cosmos: Materializer rounds when deserializing floating point numbers as fixed point numbers #38138

@JoasE

Description

@JoasE

Bug description

Cosmos performs all mathematical operations on numbers as doubles.
This means cosmos will return a double for projections like: 3 / 4 (= 0.75)
When the user projects a mathematical operation over fixed point numbers, the materialized result is rounded, while convention for these cases is truncating the decimal points. int test = 3/4 // 0

Your code

using Microsoft.EntityFrameworkCore;

var options = new DbContextOptionsBuilder<MyDbContext>()
    .EnableSensitiveDataLogging()
    .UseCosmos("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", "test")
    .LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information)
    .Options;

using var context = new MyDbContext(options);
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

context.Products.Add(new Product { Id = 1, Int = 3 });
await context.SaveChangesAsync();

var result = await context.Products.Select(p => p.Int / (p.Int + 1)).SingleAsync();

                                                                              // 1                               0
Console.WriteLine("Cosmos db returned  0.75, the materialized result is: " + result + ", but should be: " + (3 / (3 + 1)));


public class MyDbContext(DbContextOptions options) : DbContext(options)
{
    public DbSet<Product> Products { get; set; } = null!;
}

public class Product
{
    public int Id { get; set; }
    public int Int { get; set; }
}

Stack traces

SELECT VALUE (c["Int"] / (c["Int"] + 1))
      FROM root c
      OFFSET 0 LIMIT 2
Cosmos db returned  0.75, the materialized result is: 1, but should be: 0

Verbose output


EF Core version

10.0.0

Database provider

Microsoft.EntityFrameworkCore.Cosmos

Target framework

.NET 10

Operating system

W11

IDE

VS2026

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions