double s = 1/(noOfPhases + 1);

When I print the s the value I get is zero however when i print noOfPhases the value is 3. If i do the following

double s = (noOfPhases + 1);

the value returned is 4. Why can I not not do 1 divided by...?

Hope someone can help?

Thank you

Recommended Answers

All 2 Replies

1/(noOfPhases + 1) = 1/(3+1) = 1/4 = 0 (int)
(int)/(int + int) = int/int = int

1.0/4 = 0.25
double/int = double

Get it?
1/4 = 0
1.0/4 = 0.25

So I would suggest something like this:

double s = 1.0/(noOfPhases + 1);

If "noOfPhase" is an int, you will need to cast it to double to get a double value result.

double s = 1/((double)noOfPhases + 1);

Edit: Oops, posted same time as above. Either of these will work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.