tags:

views:

290

answers:

9

To return a double, do I have to cast to double even if types are double?

e.g.

double a = 32.34;
double b = 234.24;

double result = a - b + 1/12 + 3/12;

Do I have to cast (double) ?

+2  A: 

Nope, no casting is needed.

Daniel A. White
+19  A: 

No, you don't. However, your expression almost certainly doesn't do what you want it to.

The expressions 1/12 and 3/12 will be performed using integer arithmetic.

You probably want:

double result = a - b + 1/12d + 3/12d;

or

double result = a - b + 1/(double) 12 + 3/(double) 12;

Both of these will force the division to be performed using floating point arithmetic.

The problem here is that if both operands of an arithmetic operator are integers, then the operation is performed using integer arithmetic, even if it's part of a bigger expression which is of type double. Here, your expression is effectively:

double result = a - b + (1 / 12) + (3 / 12);

The addition and subtraction is okay, because the types of a and b force them to be performed using floating point arithmetic - but because division "binds more tightly" than addition and subtraction (i.e. it's like using the brackets above) only the immediate operands are considered.

Does that make sense?

EDIT: As it's so popular, it makes sense to include devinb's comment here too:

double result = a - b + 1.0/12.0 + 3.0/12.0;

It's all the same thing as far as the compiler is concerned - you just need to decide which is clearer for you:

(double) 1
1.0
1d
Jon Skeet
wouldn't it be more appropriate to use (double)1 / (double)12 + (double)3 / (double)12? I realize the arithmetic operation will promote the type, but it seems clearer at least to me to explicitly describe the casts for everything.
McWafflestix
Why is it always so if there is suddenly a great answer it is almost certainly yours? ;)
User
@Mastermind: have you SEEN his rep? :-)
McWafflestix
@Mastermind - and I loose out.
Daniel A. White
(Wow, a lot can happen while I'm editing.) @McWafflestix: I think using *lots* of casts ends up being messy. I'd probably put extraneous brackets round, but just use "a - b + (1/12d) + (3/12d)". The suffix is clear enough to me, and it feels obvious *to me* that the arithmetic will then be done using floating point. But if the code is clearer *to you* with lots of casts, go for it. Check with team-mates either way :)
Jon Skeet
also, I believe 1.0/12.0 + 3.0/12.0 would do the trick, and not require the casting. Although, suggesting that Jon Skeet missed something makes me worry...
I like devinb's version the best. However, it really does not matter which version you use as far as casting and the like are concerned, since I am pretty sure the compiler will do those calculations at compile time and replace them with constants for use at runtime.
Brian
@devinb: 1.0/12.0 + 3.0/12.0 is exactly equivalent to 1d/12d + 3d/12d. Again, it's really a matter of taste. I find that a letter draws attention to itself as much as the dot does, but if you prefer the dot then go for it.
Jon Skeet
I prefer the dots, personally. It's more clear that it's a floating point number, whereas "d" could be misconstrued as "decimal", "date" or even a variable by new programmers. In some Math-based languages, I believe 12d means 12 * d.
Atømix
@Atomiton: That's fair enough - it's a personal opinion. On the other hand, I would hope that anyone *that* new to the language would be mentored appropriately in any company I work at :) (And 12.0 could still be a decimal of course...)
Jon Skeet
A: 

You should not need to, although it's hard to tell from your question where the cast would be.

McWafflestix
A: 

When doing math, C# will return the result as the type of whichever argument has the larger type.

As I recall, the order goes something like this (from largest to smallest):

  1. decimal
  2. double
  3. float
  4. long / int64
  5. int / int32
  6. short / int16
  7. byte

Of course, this is skipping the unsigned versions of each of these.

R. Bemrose
A: 

In general no casting is needed, but in your example, the 1/12 and 3/12 are integer division, resulting in 0 for each expression. You would need to cast one of the numerator or denominator to double.

John Feminella
A: 

Casting is used to indicate whenever you want to change one datatype to another type. This is because changing the type usually involves a possible loss of data.

e.g.

double a = 8.49374;

//casting is needed because the datatypes are different.
// a_int will end up with the value is 8, because the precision is lost.
int a_int = (int) a; 

double b = (double) a_int;

In that example 'b' will end up with the value of "8.00000..." this is because a_int does not contain ANY decimal information, and so b only has the integer related information at its disposal.

+2  A: 

Hmm - The way that I've done this for the integer division issue is to do something like:

double result = a - b + 1/12.0 + 3/12.0

Aside from those though, no casting would be needed.

Fry
+2  A: 

Of course, there's a clean way to do this without casting:

double result = a - b + 1.0/12 + 3.0/12;
Jekke
+2  A: 

That really depends on what you're asking about casting. Here are two different cases:

double a = 32.34;
double b = 234.24;
double result1 = a - b + 1/12 + 3/12;
double result2 = a - b + (double)1/12 + (double)3/12;
Console.WriteLine(result1);  // Result: -201.9
Console.WriteLine(result2);  // Result: -201.566666666667

Either way you're not going to get any complaints about assigning the value to result, but in the first case the division at the end is done using integers which resolve to 0.

Jason