-1

I just wrote this and I can't find the reason it won't solve the division as expected. Can someone please explain what's going on in here?

Here's the code:

/*
3/2
 */
package paradoja;

public class Paradoja {

    public static void main(String[] args) {
        float dividendo, divisor, resto, cociente;

        dividendo = 3;
        divisor = 2;
        resto = dividendo % divisor;
        cociente = dividendo / divisor;

        System.out.printf("--------DIVISION--------\n");
        System.out.printf("El propósito es dividir 3 entre 2 y, a continuación, hacer la prueba.\n------------------------\n");
        System.out.printf("Dividendo = %.2f\nDivisor = %.2f\nCociente = %.2f\nResto = %.2f\n", dividendo, divisor, cociente, resto);
        System.out.printf("--------PRUEBA--------\n");
        System.out.printf("%.2f * %.2f + %.2f = %.2f (¿?)\n----------------------\n", cociente, divisor, resto, cociente * divisor + resto);
    }

}

It's just a 3/2 division and further test. It returns 4 instead of 3. Thank you for your time!

David
  • 7
  • 1
    What do you mean with "paradox"? Could you explain what the code is doing? You're possibly comparing `float`s to integers, or apples to oranges. – Mick Mnemonic Nov 02 '19 at 00:01
  • The calculation is done using binary numbers and 0.5 does not have a finite representation as far i remeber from school – Eineki Nov 02 '19 at 00:03
  • 3
    @Einek Yes it does, in both decimal (you just wrote it) and binary. – user207421 Nov 02 '19 at 00:08
  • The correct return is 4 no 3, if you see the calculation in your test is '1.50 * 2.00 + 1.00' = 4.0 – hiral2 Nov 02 '19 at 00:10

2 Answers2

1

I have commented the values of your variables at each point in the program.

package paradoja;

public class Paradoja {

    public static void main(String[] args) {
        float dividendo, divisor, resto, cociente;

        dividendo = 3;
        divisor = 2;
        resto = dividendo % divisor;     // resto = 3.0 % 2.0 = 1.0
        cociente = dividendo / divisor;  // cociente = 3.0 / 2.0 = 1.5

        System.out.printf("--------DIVISION--------\n");
        System.out.printf("El propósito es dividir 3 entre 2 y, a continuación, hacer la prueba.\n------------------------\n");
        System.out.printf("Dividendo = %.2f\nDivisor = %.2f\nCociente = %.2f\nResto = %.2f\n", dividendo, divisor, cociente, resto);
        System.out.printf("--------PRUEBA--------\n");
        // The last parameter passed to the System.out.printf() statement is cociente * divisor + resto = 1.5 * 2.0 + 1.0 = 4.0
        System.out.printf("%.2f * %.2f + %.2f = %.2f (¿?)\n----------------------\n", cociente, divisor, resto, cociente * divisor + resto);
    }

}
Josh Evans
  • 567
  • 1
  • 5
  • 16
0

Okay. resto = the remainder of an integer division - 3 % 2 = 1.

But you're not doing integer division. You're doing floating point division. So your last calculation is:

cociente * divisor + resto
1.5 * 2 + 1 = 4

If you stored all this in ints instead of floats, it would be doing what you expect. Or if you converted to ints in your final calculation, it would work the way you expect.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36