3

In Unity 3D, setting 0.45f as a variable gives a different result than writing it inline:

float val = 0.45f; // THIS IS NOT A DOUBLE in my test case
int result = (int)(val * 100);  //output : 44

Versus:

int result = (int)(0.45f * 100); // output : 45

The first case gives 44 while the second case gives 45. Is this legal in C#? I would have thought the compiler would have to truncate the precision of the variable due to the "f" suffix, even when it is written inline.

Alternative explanation:

Note that the compiler is not necessarily upgrading the precision. Rather, it could be choosing a lower value for 0.45f in the first case, and choosing a higher value in the second case. Is that legal for a C# compiler?

Third explanation:

The compiler could use different floating point calculations than the runtime. So in the second case, the compiler could decide the answer is 45 based on one legal value of "0.45f", and the runtime could choose a different answer based on a different legal value of "0.45f". Is that legal?

piojo
  • 6,351
  • 1
  • 26
  • 36
  • 1
    The compiler is actually performing the multiplication and conversion at compile time and the 2nd piece of code is actually compiled as `int result = 45;`. – Lasse V. Karlsen Aug 09 '19 at 07:08
  • @LasseVågsætherKarlsen But the correct answer is not 45, according to the second piece of code. Unless the compiler is allowed to upgrade the precision. Hence this question. – piojo Aug 09 '19 at 07:10
  • @LasseVågsætherKarlsen But shouldn't the compiler calculate at compile time that it's 44 and compile `int result = 44`? – GSerg Aug 09 '19 at 07:10
  • To be honest, I don't know, I tried it in LINQPad, and it gave me 45 in both cases. – Lasse V. Karlsen Aug 09 '19 at 07:13
  • 1
    @LasseVågsætherKarlsen I tried in LinqPad as well and I have reproduced it. – vsarunov Aug 09 '19 at 07:14
  • @LasseVågsætherKarlsen examle code was not correct, the first line is with `double` – Ehsan Sajjad Aug 09 '19 at 07:14
  • check this same question : https://stackoverflow.com/questions/29492878/convert-float-to-double – Ehsan Sajjad Aug 09 '19 at 07:15
  • @EhsanSajjad There is no double in this testcase. Please don't change it. – piojo Aug 09 '19 at 07:16
  • Floating point math is not **that** simple. For instance, if you only involve the cpu, the cpu might have higher precision available than the variables, which means as soon as you store a floating point value in a variable you lose precision. However, if you only involve the cpu, it might be higher, so the compiler might be right. – Lasse V. Karlsen Aug 09 '19 at 07:16
  • @piojo with float there this is not reproducible for me in .net core 2.2. project – Ehsan Sajjad Aug 09 '19 at 07:19
  • @EhsanSajjad Right, neither am I. That's why I'm asking about the C# standard, not about dotnet. Thanks for adding that, though. – piojo Aug 09 '19 at 07:21
  • @piojo i think i am missing something, your first example is not giving 44 as output – Ehsan Sajjad Aug 09 '19 at 07:22
  • @EhsanSajjad You'll need to install the same compiler as me if you want to see the same result. You probably want to just take my word for it, since Unity 3D is more than a gigabyte and has some learning curve. Perhaps Mono gives the same result, but I don't have any Mono project on hand that I could run to test. – piojo Aug 09 '19 at 07:24
  • 1
    In duplicate, this answer provides a clarity into issue by the compiler, https://stackoverflow.com/a/8911817/1559611, what seems to be happening is compiler is not even running a conversion when you use the inline code, thus it directly gets integer as result, when on using variable its calling conversion of double to integer, which explain the discrepancy in the compiler behavior – Mrinal Kamboj Aug 09 '19 at 07:52
  • it's is allowed to use a higher-precision intermediate, so in some cases the result can be different for the same expression: [Why does this floating-point calculation give different results on different machines?](https://stackoverflow.com/q/2342396/995714), [C# - Inconsistent math operation result on 32-bit and 64-bit](https://stackoverflow.com/q/2461319/995714), [Why does Math.Exp give different results between 32-bit and 64-bit, with same input, same hardware](https://stackoverflow.com/q/4018895/995714) – phuclv Sep 03 '19 at 10:34

0 Answers0