When casting baseline values ​​is necessary in C #

I have a general question regarding the casting of basic value types when using a mathematical operation. Are there any written rules in the Microsoft C # specs? I could not find them. Does he work at Mono?

Example:

double a = 1 / 1234;                    //Result is 0
double b = 1.0 / 1234;                  //Result is 0,000810372771474878
double c = 1.0 / (double)1234;          //Result is 0,000810372771474878
double d = (double)1 / (double)1234;    //Result is 0,000810372771474878
double e = 1 / 1234.0;                  //Result is 0,000810372771474878

Is it possible to assume that the result is always equal to double when dividends or dividers double? Or for some reason it’s better to make sure that both are doubles (see Case d - I saw a lot of such codes that make me ask this question)

Regards, Sebastian

+4
source share
2 answers

, , , double, . (, , , , , ).

, , , , , , .

, .

double a = (double)(1 / 1234);

, double, , 1 / 1234 , , 0, . - , , . , 7.8.2 , 0.


7.3.6 # 5 ( , ), .

7.3.6

...

(§7.5.3), , , . , b * s, b , s , * (int, int) . , , b s int, - int. , * d, - int, d - , * (double, double) .

, , , , .

, 7.3.6.2, .

• decimal, , float double.
, double, double.
• , float, float.
• , ulong, ulong , sbyte, short, int long.
• , long, .
• , uint, sbyte, short int, long.
• , uint, uint.
• int.

, , double double.

+4

, . int 0,000810372771474878, 0, 0 (int) double, 0.0.

, . double/float decimal, .

C# Specification 7.7.2 Division operator

+1

All Articles