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 
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto 
Site FAQ 
Sitemap 
Register Now
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 LQrelated 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. 


02082013, 12:56 PM

#1

Member
Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83
Rep:

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?



02082013, 01:06 PM

#2

Member
Registered: Aug 2008
Distribution: Slackware
Posts: 421
Rep:

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.



02082013, 01:37 PM

#3

Member
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

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



02092013, 03:52 AM

#4

Member
Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83
Original Poster
Rep:

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?



02112013, 08:15 AM

#5

Member
Registered: Aug 2008
Distribution: Slackware
Posts: 421
Rep:

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 xn*y, where n is the value x / y, rounded to the nearest integer. If the absolute value of xn*y is 0.5, n is chosen to be even.
 The fmod() function computes the floatingpoint 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
$



02222013, 08:58 AM

#6

Member
Registered: Oct 2012
Location: /home/v
Distribution: Slackware 14.0
Posts: 83
Original Poster
Rep:

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.



02222013, 09:51 AM

#7

Member
Registered: Aug 2008
Distribution: Slackware
Posts: 421
Rep:

Quote:
Originally Posted by curious95
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.



Thread Tools 
Search this Thread 


Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off



All times are GMT 5. The time now is 07:14 AM.

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

Latest Threads
LQ News

