Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Addition in C#.net doesn't work in some cases [duplicate]

Tags:

c#

.net

i have following case

beleg.PreisDB = (double?)orders.Where(x => x.orderId == beleg.auftrnr).Sum(x => x.itemPrice + x.shippingPrice + x.giftWrapPrice) ?? 0;
beleg.PreisCouponDB = (double?)orders.Where(x => x.orderId == beleg.auftrnr).Sum(x => x.itemPromotionDiscount + x.shipPromotionDiscount) ?? 0;
var gesamtPreis = Math.Round(beleg.PreisDB??0 + beleg.PreisCouponDB??0, 2);

I have added a quickwatch in debug to some fields in my case:

beleg.PreisDB == 8.39
beleg.PreisDB??0 == 8.39
beleg.PreisCouponDB == -0.49
beleg.PreisCouponDB??0 == -0.49

And now the strange behaviour also from quickwatch and of course the result

beleg.PreisDB??0 + beleg.PreisCouponDB??0 == 8.39
Math.Round(beleg.PreisDB??0 + beleg.PreisCouponDB??0, 2) == 8.39
gesamtPreis == 8.39

So the addition of 8.39 + -0.49 doesn't give me 7.9 but 8.39 This code was running for 600k cases on at least two i had this behaviour the others behaved well. I'm to blind to see my error at the moment. The question is why is .net behaving like this? I'm Using Visual Studio 2015 with .net 4.5.2.

like image 815
Lars Avatar asked Feb 28 '19 07:02

Lars


People also ask

How do you do addition in C?

printf("Enter two integers: "); scanf("%d %d", &number1, &number2); Then, these two numbers are added using the + operator, and the result is stored in the sum variable. Finally, the printf() function is used to display the sum of numbers. printf("%d + %d = %d", number1, number2, sum);

Why scanf is used in C?

In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards.

What is matrix addition in C?

In this program, the user is asked to enter the number of rows r and columns c . Then, the user is asked to enter the elements of the two matrices (of order rxc ). We then added corresponding elements of two matrices and saved it in another matrix (two-dimensional array). Finally, the result is printed on the screen.

How to addition two numbers in C?

Addition of two numbers in C. Addition of two numbers in C: This C language program performs the basic arithmetic operation of addition of two numbers and then prints their sum on the screen. For example, if a user will input two numbers as; '5', '6' then '11' (5 + 6) will be printed. Addition program in C. int a, b, c;

How to use addition operator in C programming?

In C Programming, Addition Operator is used to find the sum of two numbers. The operator takes two operands and returns the sum of these two operands. In this tutorial, we shall learn about Arithmetic Addition Operator and its usage with examples.

What is the difference between input () and addition ()?

The input () function, which takes no arguments from the main method, takes input from the user and returned this value to the main function. The addition () function takes two arguments because it will add two numbers. To add two numbers, first of all, numbers should be passed to the addition () function.

How to add two integers in C program?

Program to Add Two Integers. In this program, user is asked to enter two integers. Two integers entered by the user is stored in variables firstNumber and secondNumber respectively. This is done using scanf() function. Then, variables firstNumber and secondNumber are added using + operator and the result is stored in sumOfTwoNumbers.


1 Answers

The problem is precedence - + has higher precedence than ??, so it "binds tighter".

Here's a complete example to demonstrate:

using System;

class Test
{
    static void Main()
    {
        double? x = 8.39;
        double? y = -0.49;

        // Your expression
        Console.WriteLine(x ?? 0 + y ?? 0);

        // The equivalent you're expecting
        Console.WriteLine((x ?? 0) + (y ?? 0));

        // The actual bracketing
        Console.WriteLine(x ?? ((0 + y) ?? 0));
    }
}

Another alternative would be to use Nullable<T>.GetValueOrDefault() instead of ?? 0:

Console.WriteLine(x.GetValueOrDefault() + y.GetValueOrDefault());

But I think I'd probably just use the version with brackets - so in your case:

var gesamtPreis = Math.Round((beleg.PreisDB ?? 0) + (beleg.PreisCouponDB ?? 0), 2);

I would definitely put the spaces in round ?? just like most other operators, as otherwise it's giving you the impression of binding very tightly (like the . operator does).

like image 124
Jon Skeet Avatar answered Nov 03 '22 13:11

Jon Skeet