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.

Recommended Answers

All 4 Replies

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...

Thanks. Here's what I mean:

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

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

if (output > 1.25) printf("Yes\n");
else printf("No\n");

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


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.

Cool. Thanks very much!!

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.