LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   gcc 4.6.3 'invalid operands to binary % ( have 'float' and 'float' ) error (http://www.linuxquestions.org/questions/programming-9/gcc-4-6-3-%27invalid-operands-to-binary-have-%27float%27-and-%27float%27-error-4175449194/)

curious95 02-08-2013 12:56 PM

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?

mina86 02-08-2013 01:06 PM

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.

millgates 02-08-2013 01:37 PM

Code:

for (m; m > 0; m % n, rem = n)
Floating point or not, the statement does not have any effect whatsoever anyway.

curious95 02-09-2013 03:52 AM

Okay, then what if I use integers instead of floating point numbers can that also work? I saw an example
Code:

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

int main()
{
 printf ("fmod of 2.5 / 2 is %lf\n", fmod(2.5,2));
 return 0;
}

I get how remainder() works but how does fmod() work?

mina86 02-11-2013 08:15 AM

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
$


curious95 02-22-2013 08:58 AM

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.

mina86 02-22-2013 09:51 AM

Quote:

Originally Posted by curious95 (Post 4897492)
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.


All times are GMT -5. The time now is 09:00 AM.