0

Can anyone help?

Instead of outputting the decimal 1.75, C outputs 7/4 as 1.0000.
I very much need to obtain the exact decimal product of integer ratios to 2 decimal places and then compare it to another decimal number.

For example,

if (7/4 > 1.25) printf("Yes\n");
else printf("No\n"); --> So the output SHOULD be "Yes", but instead will be "No".

Also for some reason, the gcc compiler I'm using does not recognize the "floor" function.

If anyone has any suggestions I would really appreciate it. Thanks in advance.

2
Contributors
4
Replies
5
Views
13 Years
Discussion Span
Last Post by idr1978
0

It would be better if you would post some code...But I assume you declared the result of that 7/4 division as an int, and it should be float...

0

Thanks. Here's what I mean:

#include <stdio.h>
#include <math.h>

int main()
{
double output;
int x=7;
int y=4;

output=x/y;
if (output > 1.25) printf("Yes\n");
else printf("No\n");
}

THE RESULT IS "No" even though it should be "Yes".

Thanks.

0

int x
int y

This means the compiler will process a division between 2 ints, and the result will be an int (truncated, of course).
You have to typecast those 2 operators x and y

output= ((float) x)/((float) y);

or, simply declare them as float.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.