[SOLVED] gcc 4.6.3 'invalid operands to binary % ( have 'float' and 'float' ) error

ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

gcc 4.6.3 'invalid operands to binary % ( have 'float' and 'float' ) error

Code:

#include <stdio.h>
int main(void)
{
float m, n, rem, gcd;
printf("Enter two numbers consecutively(with a space between them): ");
scanf("%f %f", &m, &n);
for (m; m > 0; m % n, rem = n) //the error is here 'm % n'
{
n = m;
}
gcd = m;
printf("%.1f is the GCD", gcd);
return 0;
}

When I try to compile this program it produces the invalid operands to binary error. What am I doing wrong?

You are trying to use an operator which does not work on floating point numbers. Take a look at fmod() and remainder().

But what you actually doing wrong is trying to use floating point numbers. Note that GCD makes little sense for real numbers. Instead you should use unsigned long or something.

If you use (unsigned) (long (long)) integers, then you can just use % operator like you did in your original code (except for the bug millgates pointed).

From man pages:

The remainder() function computes the remainder of dividing x by y. The return value is x-n*y, where n is the value x / y, rounded to the nearest integer. If the absolute value of x-n*y is 0.5, n is chosen to be even.

The fmod() function computes the floating-point remainder of dividing x by y. The return value is x - n * y, where n is the quotient of x / y, rounded toward zero to an integer.

So the difference is in a way values are rounded.

Code:

$ cat a.c
#include <fenv.h>
#include <math.h>
#include <stdio.h>
int main(void) {
double a = 2.0, b = 3.0, c = a / b, d;
printf("%g / %g = %g\n\n", a, b, c);
fesetround(FE_TOWARDZERO); d = nearbyint(c);
printf("%g / %g = %g (rounded towards zero)\n", a, b, d);
printf("%g - %g * %g = %g\n", a, d, b, a - d * b);
printf("fmod(%g, %g) = %g\n\n", a, b, fmod(a, b));
fesetround(FE_TONEAREST); d = nearbyint(c);
printf("%g / %g = %g (rounded towards nearest)\n", a, b, d);
printf("%g - %g * %g = %g\n", a, d, b, a - d * b);
printf("remainder(%g, %g) = %g\n", a, b, remainder(a, b));
return 0;
}
$ gcc -o a a.c -lm && ./a
2 / 3 = 0.666667
2 / 3 = 0 (rounded towards zero)
2 - 0 * 3 = 2
fmod(2, 3) = 2
2 / 3 = 1 (rounded towards nearest)
2 - 1 * 3 = -1
remainder(2, 3) = -1
$

Interesting, I just realized math.h only uses double type. Thanks for the help, I'll look forward to learning about the math.h library.

Each mathematical function has 3 versions, for instance: fabs, fabsf and fabsl. Each of those operate on different types: double, float and long double respectively. The last version is of course only available with C99.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.