LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-08-2013, 11:56 AM   #1
curious95
Member
 
Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83

Rep: Reputation: Disabled
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?
 
Old 02-08-2013, 12:06 PM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 374

Rep: Reputation: 150Reputation: 150
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.
 
Old 02-08-2013, 12:37 PM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Code:
for (m; m > 0; m % n, rem = n)
Floating point or not, the statement does not have any effect whatsoever anyway.
 
Old 02-09-2013, 02:52 AM   #4
curious95
Member
 
Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83

Original Poster
Rep: Reputation: Disabled
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?
 
Old 02-11-2013, 07:15 AM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 374

Rep: Reputation: 150Reputation: 150
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
$
 
Old 02-22-2013, 07:58 AM   #6
curious95
Member
 
Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-22-2013, 08:51 AM   #7
mina86
Member
 
Registered: Aug 2008
Distribution: Slackware
Posts: 374

Rep: Reputation: 150Reputation: 150
Quote:
Originally Posted by curious95 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Error: invalid operands to binary & (have ‘char *’ and ‘int’) chinabenjamin66 Linux - Software 4 12-05-2012 01:36 AM
compile error: invalid operands for binary >> for pgd_index(addr) macro rashmikant Linux - Kernel 1 06-10-2009 03:36 PM
float to binary anoosh Programming 1 04-15-2006 02:45 AM
How to convert a float to its binary or hex representation in Python? zero79 Programming 1 09-01-2005 10:19 AM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM


All times are GMT -5. The time now is 01:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration