ArcSoftware
ArcSoftware

Reputation: 23

Java doubles not comparing correctly in conditional

Trying to use conditionals instead of if statements for double comparison. Debugged step by step several times and can not figure out on what planet java thinks -9==0. When you highlight it, it says false but increases the "zero" double anyways.. The other ones seem to come come across just fine.

My input is:

6
-4 3 -9 0 4 1  

The code:

public class HRWPlusMinus {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int length = Integer.valueOf(in.nextLine());
    String[] input = in.nextLine().split(" ");
        solveWOF(length, input);
    }

    //solve with for loop
    public static void solve(int length, String[] input) {
        double pos = 0;
        double neg = 0;
        double zero = 0;
        for (int i = 0; i < length; i++){
            int test = Integer.valueOf(input[i]);
            if (test > 0) {pos++;}
            else if (test < 0) {neg++;}
            else {zero++;}
        }
        System.out.printf("%.6f \n%.6f \n%.6f", (pos/length), (neg/length), (zero/length));
    }

    //solve with conditionals
    public static void solveWOF(Integer length, String[] input) {
        double pos = 0, neg = 0, zero = 0;
        for (int i=0; i<length; i++) {
            Double test = Double.valueOf(input[i]);
            zero = (test == 0)? zero++:
            test > 0 ? pos++: neg++;
        }
        System.out.printf("%.6f \n%.6f \n%.6f", (pos/length), (neg/length), (zero/length));
    }
}

Top method works, bottom one is the one I'm having issues with.

Upvotes: 0

Views: 353

Answers (3)

ArcSoftware
ArcSoftware

Reputation: 23

double pos = 0, neg = 0, zero = 0;
        for (int i=0; i<length; i++) {
            Double test = Double.valueOf(input[i]);
            pos += (test > 0) ? 1:0;
            neg += (test < 0) ? 1:0;
            zero += (test == 0) ? 1:0;
        }

Fixed it, thank you. As suggested I realized that I assigning something that wasn't an assignment.

Upvotes: 0

user85421
user85421

Reputation: 29730

I know that normally a question should not be answered with another, but what are you trying to do with this

zero = (test == 0)? zero++:
        test > 0 ? pos++: neg++;

I mean why are you assigning to zero?

Explaining:

if test is zero, this reduces to:

zero = zero++;

which is equivalent to:

int tmp = zero;    // internal variable
zero = zero + 1;
zero = tmp;

that is nothing changed (as explained by EJoshuaS).

If test is positive (or negative) it reduces to:

zero = pos++;  // or neg++ if test is < 0

that is, zero will be set to the number of positives (or negatives) so far! That is not wanted, the assignment is wrong in all cases!

Use an if as in the other method solve().

Be aware that comparison with doubles can be tricky, see:

  1. What's wrong with using == to compare floats in Java?
  2. comparing float/double values using == operator

Upvotes: 0

Your first problem is that zero++ doesn't behave how you think it does. It retrieves the value and then increments it, so you're always assigning the value to itself. You can verify that the following prints zero:

int zero = 0;
zero = zero++;
System.out.println(zero);

Thus, this line won't work:

zero = (test == 0)? zero++:
        test > 0 ? pos++: neg++;

Some of the other problems have already been discussed in the comments.

Upvotes: 1

Related Questions