Click to See Complete Forum and Search --> : Division in C++


July 25th, 1999, 04:59 PM
In C++ how may I divide and get a floating point result (ie 7/4 = 1.75). Wound you have to do this, or rather is this the only way?

int a, b;
float answer, answer2;

a=7;
b=4;
answer = a/b;
answer2 = a%b;
answer += answer2;

Is this the only way to do it? Is this the best way if I must compare two division results and I have to be able to compare all of the decimal places also. Thanks!

Paul Burns
July 25th, 1999, 07:35 PM
you can cast the int's before division, i.e. use...


int a,b;
float answer;

answer = (float)a / (float)b;

Oleg M.
July 25th, 1999, 09:41 PM
...or use something like that:

int a, b;
float answer=(float)a*exp(-log(b));