Floating point evaluation order in C -


I am using Code :: Blocks 10.05 on Windows 7.

I have compiled the following program,

contains #lt; Stdio.h & gt; Int main () {float f = 1.5; Printf ("% f \ n", (f1) * 1/2); Return 0; }

The output was 0.250000 . I understand this as follows,

Since f is a float (f-1) 0.500000 and full expression floating Point has been upgraded to arithmetic and therefore it is also considered as float to get 1/2 to 0.500000 and hence the result.

In addition to the following statement,

  float F = 1.5; Printf ("% f \ n", 1/2 * (f -1));  

gave the answer as 0.000000 expression here 1/2 divided the integer here (f-1) was evaluated for the first and full expression upgrade to floating point arithmetic.

Here again I thought that since the Polish notation,

  * / 1 2 - f1  

the first operation of the division And therefore the result is. Am I right about this assumption?

Finally, the following statement rejects all arguments;

  float f = 1.5; Printf ("% f \ n", (f1) * (1/2));  

The output is 0.000000 , but there is a Polish signage here,

  * - f 1/2 2  < / Pre> 

then (f-1) should be evaluated first and therefore the whole expression upgrade for the floating point arithmetic and the output should be 0.250000 .

Where have I gone wrong? Does this operator have to do something with the order of evaluation? What is the expression in C? Since

Since F is a float (F1) return float value of 0.500000 and the complete expression Has been upgraded to floating point arithmetic

No, where did you get that wrong information? The promotion for floating point only applies to one operator, one of which is an floating-point number operator. 1/2 is considered as a temporary point expression - like, it has not been evaluated. Since multiplication and division have the same priority and left-assessed as Associate,

  (f - 1) * 1/2  

Goes

  (<-> 1) / 2)  

and there 1 to float (At least), and then because ((1 - 1) * 1) also runs, then the second operator of the partition is 2 even float (or a high precision FP number, which is based on the compiler wants) has also been promoted.

Here I expect that (F1) will be evaluated first, the full expression has to be upgraded to floating point arithmetic.

No, this is a wrong hope, as I explained. This is a complete expression that is under conversion at floating-point not . This is another immediate operation of the only operator, one operand is a float in the expression:

  (f - 1) * (1/2)  

f - 1 a name . 1/2 is an integer, it is zero, and then the integer zero is promoted to float because the other operand of * Code> Name . The scope of / , however, is integers, so the division is evaluated according to the rules of integer arithmetic.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -