-2

So I have been trying to write this function and implement it without luck. The function has to be written as void divisor (float x ) and it will ask a user for a int number and run until the user inputs a value of 0 . I get a few errors one which concerns me the most is invalid operands of types ‘float’ and ‘int ’ to binary ‘operator

I have tried writing it so:

#include <iostream>
using namespace std ;

void divisor (float x )
  { 
     int result ;
     int a ; 
     result =   x % a << endl ; 
     a++ 
     return 0 ; 
  }  
    };
      
 int main () 
{   int n ;
    float arg; 
    cin >> arg ;
    cin >> n ; 
    cin >> arg; 
    if    ( n =! 0 ){ 
    divisor ( arg) ;
        }else{
          cin >> n ;
      } return 0 ; 
    } ;
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
mmXXII
  • 7
  • 1
  • 4
    Welcome to Stack Overflow! I fixed the code-fence problems in your post but I am reluctant to change the *actual* code. There are problems with indentation and at least one "typo" (missing `;` after `a++`). Maybe you would like to revisit the "Edit" button and address these issues. – Adrian Mole Jan 29 '21 at 14:26
  • 2
    `result = x % a << endl ;` means "assign the value of `x % a << endl` to the variable `result`. It looks like you need [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or, if you've got one, reviewing the first chapter or two. – molbdnilo Jan 29 '21 at 14:29
  • 2
    What is a divisor of a float? What are the divisors of 2.5? 0.5 and 5? 0.00001 and 250000? – Kevin Jan 29 '21 at 14:30
  • Given the value (precision) change when converting from decimal to binary (see https://stackoverflow.com/questions/588004/is-floating-point-math-broken ) I don't think this question has an answer. – Richard Critten Jan 29 '21 at 15:06

1 Answers1

2

The expression x % a is only valid for integral types x and a. Since x is a float type, compilation fails.

If you want the floating point modulus for a float type, then use

std::fmodf

instead. (Note that a will be implicitly converted to a float.)

Reference: https://en.cppreference.com/w/cpp/numeric/math/fmod

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    even with `fmodf` I don't understand how to find divisors of a `float`, because `std::fmodf( x,y) == 0` requires a bit of "luck" to get `true` – 463035818_is_not_an_ai Jan 29 '21 at 14:31
  • @largest_prime_is_463035818: I don't think IEEE754 offers any guarantees. But I would regard any implementation of `fmodf` that doesn't return the best `float` possible back to be defective. – Bathsheba Jan 29 '21 at 14:32
  • 1
    what I mean is: OP seems to be looking for `int` divisors of `float`s, but only integers have integer divisors – 463035818_is_not_an_ai Jan 29 '21 at 14:34
  • what you wrote isnt wrong, but the question needs clarification. There is more wrong with the code (eg what is the role of `a` supposed to be in `divisor`) – 463035818_is_not_an_ai Jan 29 '21 at 14:37