-3

The error i get is "bad operand types for binary operator ^ first type: float second type: float" this error happens in case 5 of the switch x y and r are floats and n is an Int

  switch (n) 
         {
             case 1: r=x+y;
             case 2: r=x-y;
             case 3: r=x*y;
             case 4: r=x/y;
             case 5: r=x^y;
             default: System.out.println("Elija un numero valido");}

1 Answers1

-2

^ is only defined for the integer-like types (byte, short, int, long, char) and booleans.

In other words: exactly what the error message says. floats are not legal operands for the ^ operator.

And that makes sense: most people wouldn't be able to tell what the binary representation of e.g. 23.42 is. Would you? (This is actually a trick question: you cannot even represent 23.42 in binary. The closest you can get is 23.4200000000000017053025658242 whose binary representation is 01000000 00110111 01101011 10000101 00011110 10111000 01010001 11101100.)

If the ^ operator did work with floats, it wouldn't be very obvious. E.g.

2.0 ^ 3.0

Would be

0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111253692925360069154511635867

(I think, if I didn't make a mistake somewhere.)

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 1
    Your answer makes no sense for people who think `x^y` means *exponentiation*, which seems to be the case for the questioner. – Andreas May 19 '20 at 20:35
  • The result of `2.0 ^ 3.0` would be easier to read if you said it is `1.1125369292536007E-308`, because no one will try to count zeroes. – Andreas May 19 '20 at 20:41