Using the suffix f for numeric literals

I see the following code:

float num2 = ( ( this.X * this.X ) + ( this.Y * this.Y ) ) + ( this.Z * this.Z );
float num = 1f / ( ( float ) Math.Sqrt ( ( double ) num2 ) );
this.X *= num;
this.Y *= num;
this.Z *= num;

It doesn't matter if it was like this:

float num2 = ( ( this.X * this.X ) + ( this.Y * this.Y ) ) + ( this.Z * this.Z );
float num = 1 / ( ( float ) Math.Sqrt ( ( double ) num2 ) );
this.X *= num;
this.Y *= num;
this.Z *= num;

Does the compiler (float) / (float)use or try to use (double) / (float)for the second example for line 2?

EDIT: Btw will there be any performance difference?

+3
source share
2 answers

In fact, he uses (int)/(float)for the second example. Since Int32 is implicitly converted to Single, the compiler will not complain, and it will work fine.

However, he will complain if you do this:

float num = 1.0 / ( ( float ) Math.Sqrt ( ( double ) num2 ) );

(double)/(float), (double)/(double). , float.


EDIT: Btw - ?

, . IL. JIT - , .

, , , , :

double num2 = (this.X * this.X) + (this.Y * this.Y) + (this.Z * this.Z);
float num = (float) (1.0 / Math.Sqrt(num2));
this.X *= num;
// ...
+6

; .

1f 1.0 ( 1d), double.

+1

Source: https://habr.com/ru/post/1786512/


All Articles