Frage

I have a function in which I need to pass a double. To call that function, I am using the following code:-

static int Main()
{
     double d = 1/7 ; 
     Console.WriteLine("The value of d is {0}", d) ; 
     calc(d) ; 
     return 0 ; 
}

The output of the following program is

The value of d is 0

Why is this so, why is C# truncating the part ahead of decimal, despite storing 1/7 in a double?

War es hilfreich?

Lösung

An int divided by an int uses integer truncation.

Use:

static int Main()
{
    double d = 1.0 / 7 ; 
             //^^   or d = 1.0 / 7.0 
    Console.WriteLine("The value of d is {0}", d) ; 
    calc(d) ; 
    return 0 ; 
}

Promoting either numerator or denominator (or both) to a floating point type promotes the result of the division to a floating point type.

Refs:

Andere Tipps

Because what you doing is here called integer division. It always discards the fractional part. That's why 1 / 7 always give you 0 as a result regardless which type you assign it.

.NET has 3 type of division. From 7.7.2 Division operator

  • Integer division
  • Floating-point division
  • Decimal division

Also from / Operator (C# Reference)

When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2. To obtain a quotient as a rational number or fraction, give the dividend or divisor type float or type double.

So, as a result, you can use one of these if you want fractional part;

double d = 1.0 / 7 ;
double d = 1 / 7.0 ;
double d = 1.0 / 7.0 ;

According to C# reference

For an operation of the form x / y, binary operator overload resolution (Section 7.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.

This means that the operator / selects the correct overloads looking at its parameters. In your case your parameters are integer so, the operator selects the integer division that returns an integer (truncating the remainder)

To avoid this and select the floating point division you should give an hint forcing one of your constants to be a double/float

double d = 1.0 / 7 ; 

Because the first parameter in 1/7 is an integer, so c# does a integer-division.

You'll get the correct result if you type:

double d = (double)1/7;

What you have here is operation precedence. In effect you have written

int temp = 1 / 7;
double d = temp;

Which actually gets compiled to

int temp = 0;
double d = temp;

or

double d = 0;

The reason being is that you are using the int divide operator

static operator int / (int, int)

when you meant to use the

static operator double /(double, double)

You can force that by writing

double d = 1.0 / 7;

OR

double d = 1d / 7d;

etc etc

C# is statically-typed at compile time. Your code (double d = 1/7;) is run in the following manner in the run time.

var temp = 1/7;
double d = temp;

Here, 1 and 7 are integers. So, the division operation returns only integer and stored it in the temporary location. After that, the variable d is created and the temporary value is stored in that variable. So, here the implicit type conversion will not work.

So, you have to done the explicit type conversion at the time of division. 1.0/7 or 1/7.0 or (double)1/7 or 1/(double)7 will return the double value. So, the integer to double implicit cast will not apply here and you will get your desired result.

If you specify your number as 1 without decimal point, an int type is assumed. Replace the line

double d = 1/7 ;

with

double d = 1.0/7 ;

Alternatively you can specify the type as double using suffix:

double d = 1d/7 ;

In C# and Java (and most programming languages) the type of the result is the type of the of the numerator and denominator. You have to cast the integers that make up the numerator and denominator into doubles if you want the result to be a double.

Try double d = 1d/7d or double d = (double)(1/7)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top