Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of casting/conversion int/double in C#

I coded some calculation stuff (I copied below a really simplifed example of what I did) like CASE2 and got bad results. Refactored the code like CASE1 and worked fine. I know there is an implicit cast in CASE 2, but not sure of the full reason. Any one could explain me what´s exactly happening below?

  //CASE 1, result 5.5
    double auxMedia = (5 + 6);
    auxMedia = auxMedia / 2;

    //CASE 2, result 5.0
    double auxMedia1 = (5 + 6) / 2;

    //CASE 3, result 5.5
    double auxMedia3 = (5.0 + 6.0) / 2.0;

    //CASE 4, result 5.5
    double auxMedia4 = (5 + 6) / 2.0;

My guess is that /2 in CASE2 is casting (5 + 6) to int and causing round of division to 5, then casted again to double and converted to 5.0.

CASE3 and CASE 4 also fixes the problem.

like image 347
Oscar Foley Avatar asked Mar 31 '10 18:03

Oscar Foley


People also ask

What does casting int () do in C?

The Conversion of Char to Int We can use the process of type casting to convert the character (char) data type to the int (integer) data type in C. When we are performing a conversion between these two, the resultant value would be an integer (int) data type.

Can we convert int to double in C?

The %d format specifier expects an int argument, but you're passing a double . Using the wrong format specifier invokes undefined behavior. To print a double , use %f .

What is casting explain the type conversion?

In type casting, a data type is converted into another data type by a programmer using casting operator. Whereas in type conversion, a data type is converted into another data type by a compiler. 2. Type casting can be applied to compatible data types as well as incompatible data types.

How does type casting work in C?

Type casting refers to changing an variable of one data type into another. The compiler will automatically change one type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point variable, the compiler will convert the int to a float.


4 Answers

  1. 5 + 6 is integer 11; which you then cast to double (in the assignment) and divide by two; 5.5
  2. 5 + 6 is integer 11; integer 11 / 2 = 5 under integer arithmetic, which you then cast to double (in the assignment)
  3. 5.0 + 6.0 is double 11.0; divide by double 2.0 giving double 5.5
  4. 5 + 6 is integer 11; there is an implicit cast to double 11.0 for the division, then divide double 2.0 giving double 5.5
like image 135
Marc Gravell Avatar answered Oct 05 '22 03:10

Marc Gravell


To expand on Marc's (correct) answer a bit, whole numbers are interpreted as integer, whereas numbers with decimal points are interpreted as double. To declare a whole number as a literal double, append a "D" to it:

        //CASE 2b, result 5.5
        double auxMedia2b = (5D + 6D) / 2;
like image 37
Jamie Ide Avatar answered Oct 05 '22 04:10

Jamie Ide


You are correct. CASE 2 uses integer arithmetic until the assignment is made. You can also fix the problem by making an explicit cast:

double auxMedia1 = ((double) (5 + 6)) / 2;
like image 29
Keltex Avatar answered Oct 05 '22 03:10

Keltex


//CASE 2, result 5.0
double auxMedia1 = (5 + 6) / 2;

The result of the (5 + 6) operation is integer. Because both operands are of type integer. Then, the compiler performs 11 / 2, where both operand are also integers. The result of the last division is obviously 5, because it is an integer division (don't know the proper English word).

like image 38
n535 Avatar answered Oct 05 '22 04:10

n535