MetallicPriest
MetallicPriest

Reputation: 30841

Is there a difference between these two implemenations?

Is there any difference on how the compiler will generate the code for the following two. Secondly, will they produce the same return values.

  static inline float fix2float(int64_t f)
  {
    return (float)f / ((int64_t)1 << 60); 
  }

and this

  static inline float fix2float(int64_t f)
  {
    return (float)(f / ((int64_t)1 << 60)); 
  }

Upvotes: 1

Views: 95

Answers (2)

Jon
Jon

Reputation: 437904

These two functions are very different: the second one performs integer division, while the first one does floating-point.

In particular, the return value of the second version is always going to be an integer in the range [-8, 7].

Update: Of course this is only true if you first correct the typo that unwind caught.

Upvotes: 8

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727107

Yes, there is: in C, typecast has higher precedence than division, so the first one will divide in float, while the second one will perform an integer division.

Upvotes: 5

Related Questions