Very small fractions and 'double' type
I'm using a formula of 1/x, where x could be very big (bigger than 1开发者_开发问答000)
When I use 'double' type, I always get zero!
For example:
int numOfDays = 1000;
double result = 1 / numOfDays;
Which type can I use?
Thanks in advance!!
You are doing integer division and assigning the result to a double.
Change one of the operands to a double
or float
, and you will be OK.
int numOfDays = 1000;
double result = 1.0 / numOfDays;
Or:
int numOfDays = 1000;
double result = 1D / numOfDays;
精彩评论