4

I have a line of code

double i = 1 + (long)1.5* 5.0f

My question is what is the conversion order and the result? Been searching for examples like this, but to no avail. Any good guides out there that may help me understand it?

user1601401
  • 732
  • 1
  • 12
  • 25

4 Answers4

6

My question is what is the conversion order and the result?

The cast is applied to 1.5, giving a long with value 1.

That's converted to float for multiplication with 5.0f, giving a float with value 5.0f.

1 is converted to float for addition with that value, giving a float with value 6.0f.

Finally, that's promoted to double (retaining the value 6.0) to assign to i.

This assumes a non-crazy floating point format that can represent small integers exactly; otherwise, there may be rounding errors.

If you wanted to cast the result of the multiplication, then use parentheses to control the operator precedence:

double i = 1 + (long)(1.5* 5.0f);  // = 8.0

or use a C++-style cast, which forces the use of parentheses:

double i = 1 + static_cast<long>(1.5* 5.0f)

Any good guides out there that may help me understand it?

Here's one: http://en.cppreference.com/w/cpp/language/operator_precedence. Note that the type cast has a higher precedence than multiplication, which is in turn higher than addition (3 vs. 5 vs. 6).

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • *"1 is promoted to float"*. Is the term "promoted" correct here? I think it should be "converted". – Nawaz Oct 01 '13 at 13:03
  • 1
    @Nawaz: Yes, you're right. I can never remember exactly which is which. – Mike Seymour Oct 01 '13 at 13:06
  • @MikeSeymour Is is really important? (For what it's worth: promotions occur even if the two operands have the same type. Every thing else is conversion. Also, I think that the only time a floating point promotion takes place is as a function argument matching `...`) – James Kanze Oct 01 '13 at 13:10
  • @JamesKanze: No, it's not important, but there are enough pedants around here that I prefer to get things right. – Mike Seymour Oct 01 '13 at 13:13
  • @JamesKanze: It is not important here. But I'm working hard to get used to the terms that the spec uses, so that when I look at the spec/proposals/drafts, it may appear comprehensible to me. And in my first comment, I was merely looking for confirmation that I understand the thing correctly. :-) – Nawaz Oct 01 '13 at 13:19
1

As you can see from this table, the cast operator has higher precedence than multiplication, but follow the advice to use parentheses.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
0

This precedence table should tell you everything you need to know.

  1. Casting: 1.5 is cast to a long
  2. Multiplication: 1.5 * 5.0f, which casts this product as a float
  3. Addition: 1 + ( ((long) 1.5) * 5.0f)
  4. Assignment: i = 1 + ((long) 1.5 * 5.0f)
Bucket
  • 7,415
  • 9
  • 35
  • 45
0

If you're not sure what the precedence of the casting operator is then rewrite the expression (in your head)

(long)1.5 * 5.0

to

5.0 * (long)1.5

Here its pretty obvious what has precedence and its the same with the first version

Serve Laurijssen
  • 9,266
  • 5
  • 45
  • 98