> Java program..What is the correct statement to calculate the real-number ratio of a to b?

Java program..What is the correct statement to calculate the real-number ratio of a to b?

Posted at: 2014-12-18 
int a=4; int b=5;

double ratio;

A ratio is just another word for a fraction. So simply divide:

ratio = a/b;

Note that ratio must be a double you will lose data through rounding to integer.

You should also be aware that there is an intrinsic problem with binary when storing fractional components. Binary can only store fractions that can be made by adding together 1/2, 1/4, 1/8, 1/16, 1/32, etc. In just the same way that 1/3 can only be expressed in decimal as 0.33333..., so there are many fractions that can only be expressed in binary as a group of recurring bits. Since each value is limited to a finite number of bits, the result of many divisions end being stored approximately rather than with 100% accuracy. There's not much you can do to overcome this so you just have to live with it, but just sometimes you get an error that's difficult to pin down and this could be a possible cause.

You can't store a ratio in a double or in any data type in Java as far as I know. The closest you could get is making a fraction out of the two numbers (i.e. a / b or b / a)

int a=4; int b=5;

double ratio;