dotnet / dotnet/efcore

Cosmos: Warn when integers participate in arithmetic (which evaluates them as doubles)

Open
#38,139 4 comments 0 reactions 0 assignees View on GitHub
customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### What problem are you trying to solve?

Cosmos db treats all numbers as doubles when doing mathematical operations on them.
Users might be unaware of side effects this could have on queries containing mathematics with fixed point numbers.

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

var localResult = product.Int / 4 * 10; // 0
var result = await context.Products.Select(p => p.Int / 4 * 10).SingleAsync(); // 8 (or 7 depending on rounding, also see: #38138 )
```

### Describe the solution you'd like

EF could warn or error during query compilation when a fixed point number equation could result in a floating point result, requiring the user to cast to a double.
```csharp
await context.Products.Select(p => p.Int / 4 * 10).SingleAsync(); // Exception
await context.Products.Select(p => (double)p.Int / 4 * 10).SingleAsync(); // 7.5d
```
The user could still cast the result back to an int to retrieve the truncated result

```csharp
await context.Products.Select(p => (int)((double)3 / 4 * 10)).SingleAsync(); // 7
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.