Recently there was a place where the probabilities were used in the project, and the probability of calculating the probabilities would naturally use division. My side of the probability of the algorithm is this, from 0 to 10000 to get a random number, the random value divided by 10000 is the probability, but the careless attention to detail, the result is directly in addition to the following:
= New Random ();
int= rdo. Next (0,10000);
float=/10000;
If Intnum is 845, you should theoretically get 0.0845. But the result let me plunge glasses, regardless of how to calculate, the result is 0.0, if no matter how random words are 0.0, then lose meaning. So Baidu went, the original C # Division of precision is based on dividend to decide, here is the dividend is intnum, its data type is plastic, so this side of the result of the natural to take the whole. Since this way does not pass, it is only a different way, in short, the problem must be solved, so I changed to the following this writing:
Random RDO = new Random ();
float Intnum = ( float ) RDO. Next ( 0 10000 );
float result = Intnum / 10000
Ok, achieved, got the answer I want, such as Intnum is 845, then get the result is 0.0845.c# solved this problem, so I think of SQL Server will also have this situation, so I tried, as follows:
Select 845 / 10000
Query results equal to 0, it seems that SQL and C # is the same, so I also changed the way, as follows:
Select CONVERT (float,845) / 10000
Query result equals 0.0845, correct.?
The details of C # Division