-2

I am trying to filter through a float number given by the user and find the largest digit within. However, I keep getting the error:

error: invalid operands to binary % (have 'float' and 'int')

I have read other forums stating us the fmod function, how would I apply that with a simple piece of code that I just want to find the remainder of a float?

  • can you please show your code? – Stephan Lechner May 29 '17 at 20:50
  • What would be the desired result of `%` on floats? – Eugene Sh. May 29 '17 at 20:50
  • Use the `fmod` function from `math.h` – Erik W May 29 '17 at 20:51
  • Possible duplicate of [Why does modulus division (%) only work with integers?](https://stackoverflow.com/questions/6102948/why-does-modulus-division-only-work-with-integers) – Dov Benyomin Sohacheski May 29 '17 at 20:51
  • Floats usually don't have decimal digits, that's an illusion offered by default ways of printing them. Extracting decimal digits from them typically results in a bunch of junk. – harold May 29 '17 at 20:52
  • Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon, but even more importantly, please read about how to create an MCVE ([MCVE]). You should include code showing what you've got. Granted, in this case, it isn't compiling, but even a 10 line (or less) example should be provided. – Jonathan Leffler May 29 '17 at 21:52

2 Answers2

1

Use the fmod function (Ideone example):

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

int main(void) {
    float val = 19.3;
    int div = 2;
    printf("%f\n", fmod(val, div));
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
aardvarkk
  • 14,955
  • 7
  • 67
  • 96
1

The % operator is only applicable between integer numbers because in order to generate an integer quotient is required an integer division. Use instead:

double remainder = fmod(arg1, arg2);
Overflow 404
  • 482
  • 5
  • 20