Provenance of calculation

Asked

Viewed 44 times

0

I cannot understand why you are giving 0.00 for the calculation, and if I change the order of the calculation of the expected value, the calculation has only division and multiplication.

import java.util.Locale;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    int n, i, animal, total = 0, totalRato = 0, totalSapo = 0, totalCoelho = 0; 
    double percentualCoelhos, percentualRatos, percentualSapos;
    char categoriaAnimal;
    Locale.setDefault(Locale.US);
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    for (i = 0; i < n; i++) {
        animal = sc.nextInt();
        categoriaAnimal = sc.next().charAt(0);
        if (categoriaAnimal == 'R') {
            total += animal;
            totalRato += animal;
        }
        else if (categoriaAnimal == 'S') {
            total += animal;
            totalSapo += animal;
        }
        else if (categoriaAnimal == 'C') {
            total += animal;
            totalCoelho += animal;
        }
    }
    sc.close();
    percentualCoelhos = totalCoelho * 100.0 / total; 
    percentualRatos = totalRato / total * 100.0;
    percentualSapos = totalSapo / total * 100.0;
    System.out.println("Total: " + total + " cobaias");
    System.out.println("Total de coelhos: " + totalCoelho);
    System.out.println("Total de ratos: " + totalRato);
    System.out.println("Total de sapos: " + totalSapo);
    System.out.printf("Percentual de coelhos: %.2f%%%n", percentualCoelhos);
    System.out.printf("Percentual de ratos: %.2f%%%n", percentualRatos);
    System.out.printf("Percentual de sapos: %.2f%%%n", percentualSapos);
}

}

The problem is in the code percentualCoelhos = totalCoelho * 100.0 / total; I see no difference in calculating in this way: percentualCoelhos = totalCoelho / total * 100.0;, but the value of the different. Why?

  • 1

    Because total and total are integers and the division between integers is rounded to zero. If you want the result to be double, cast: https://ideone.com/sYfGXB

1 answer

2


The problem is the typing system.

The variable total is whole and depending on where it is will use the integer operator, which results in an integer and depending on where it has "contaminates" the next result for integer as well. If precedence (and not provenance) prioritize a value of type double then the operator to be used is the double which will result in a decimal part value and can "contaminate" for the next operation with this type as well.

So totalCoelho * 100.0 gives a type value double (multiply an integer value with a double gives a double), which will then be divided by an integer value, so it means that you want an integer value and that is what results.

Already totalRato / total gives an integer for both are whole, there is a loss of the fractional part (some people consider this a language error, but it is so). When you multiply an integer with a double, the * 100.0, gives double, but the loss was already considered.

Since you haven’t said what you expect as correct, I can’t say which is what you want, but it doesn’t matter if you wanted to lose the decimal part then the second is the most appropriate, if you didn’t want the loss the first is the most appropriate.

Another way to solve would be to use parentheses, thus changing the precedence.

You can also force one type to be another using one cast turning the kind into what you want.

See the differences:

class Main {
    public static void main (String[] args) {
        System.out.println(123 * 100.0 / 10);
        System.out.println(123 * (100.0 / 10));
        System.out.println((double)123 * 100.0 / 10);       
        System.out.println(123 * 100.0 / (double)10);
        System.out.println(123 * 100 / 10);
        System.out.println(123 / 10 * 100.0);
        System.out.println(123 / (10 * 100.0));
        System.out.println((double)123 / 10 * 100.0);
        System.out.println(123 / (double)10 * 100.0);
        System.out.println(123 / 10 * 100);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.