-1

Let's say I have the following:

double f;
double c = 5;

Console.WriteLine("{0} | {1}", c, f = 9 / 5 * c + 32);

Which will give the output: 5 | 37

Meanwhile:

double f;
double c = 5;

Console.WriteLine("{0} | {1}", c, f = 9.0 / 5.0 * c + 32);

Will give the correct output: 5 | 41 (Representing how much 5 degrees Celsius is in Farenheit)

I know that I am working with a double, but I lack the insight into why both outcomes aren't equivalent with each other?

StarPilot
  • 2,246
  • 1
  • 16
  • 18
XT_Nova
  • 1,110
  • 5
  • 17
  • 32
  • 2
    Search for integer division in C#. – Tim Schmelter Sep 20 '13 at 21:30
  • 6
    Arghh. Read about **integer division** – I4V Sep 20 '13 at 21:30
  • Why soo upset? I'm sorry for my question. – XT_Nova Sep 20 '13 at 21:36
  • 1
    @htmlNewbie, I'm perplexed by the aggression your question has invoked. It seems a perfectly sensible one to me. Please don't let this bad experience put you off from asking future questions. – David Arno Sep 20 '13 at 21:45
  • 1
    @DavidArno Thank you David, for that comment and for your answer to my question. I have had some bad experiences asking questions before, but i thought that i had improved my question-asking-skill by now... Still some work to do i guess. :) – XT_Nova Sep 20 '13 at 21:48

2 Answers2

2

When you use the integer syntax (9 and not 9.0), each operations was rounded without decimal

tdelepine
  • 1,986
  • 1
  • 13
  • 19
1

Because the C language was designed [essentially] as a hardware-agnostic assembly language. Consequently, division, unless it involves a floating point value is integer division, the a CPU will do it in a single instruction. That means that an expression like:

int x = 9 ;
int y = 5 ;
int z = x / y ;

That will usually compile down to a single machine instruction (possibly with others to load/store values to/from registers, the net result of which is a quotient and a remainder. So, the above int z = x / y will resolve to 1, leaving the remainder of 4 on the table (note that if you want the remainder, use the % operator: int z = x % y yields the expected 4.

If one of the operands is a floating point value, then floating point division is used (much expensive, computationally speaking).

Since C# inherits from that legacy, it behaves in the same way. So, §14.7.2 of the Standard says

14.7.2 Division operator

For an operation of the form x / y, binary operator overload resolution (§14.2.4) is applied to select a specific operator implementation. The operands are converted to the parameter types of the selected operator, and the type of the result is the return type of the operator.

The predefined division operators are listed below. The operators all compute the quotient of x and y.

  • Integer division:

    int   operator /( int   x, int   y );
    uint  operator /( uint  x, uint  y );
    long  operator /( long  x, long  y );
    ulong operator /( ulong x, ulong y );
    void  operator /( long  x, ulong y );
    void  operator /( ulong x, long  y );
    

The operators with void return type always produce a compile-time error. Consequently, it is an error for one operand to be of type long and the other to be of type ulong.

If the value of the right operand is zero, a System.DivideByZeroException is thrown.

The division rounds the result towards zero, and the absolute value of the result is the largest possible integer that is less than the absolute value of the quotient of the two operands. The result is zero or positive when the two operands have the same sign and zero or negative when the two operands have opposite signs.

If the left operand is the smallest int or long value (−231 or −263, respectively) and the right operand is –1, an overflow occurs. In a checked context, this causes a System.ArithmeticException (or a subclass thereof) to be thrown. In an unchecked context, it is implementation-defined as to whether a System.ArithmeticException (or a subclass thereof) is thrown or the overflow goes unreported with the resulting value being that of the left operand.

[discussion of floating-point and decimal division elided]

That's why.

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135