paul c. kim

Greenhorn

Posts: 14

posted 2 years ago

Source Code:

int driven = initMiles - mileAge;

if (driven % MPG == 0)

Given that the initMiles and mileAge are both private fields, and have constructors set initial values to zero. (MPG variable = 24).

initMiles will always be zero, however, mileAge will be incremented from a void method. The first iteration of the source code above will be set to

int driven = 0 - 1;

if( -1 % 24 == 0)

How does java work with the negative integer operations? Can I disregard the negative integers?

This is the code I had to implement from someone else because I got stuck where I had to make the program know exactly when ( in this case 24) to switch on/off and I tried that with nested for loops and ended up

confusing myself the entire time I was coding. I would greatly appreciate any advice in thinking into writing such codes in the way I provided above and in general. Thank you.

int driven = initMiles - mileAge;

if (driven % MPG == 0)

Given that the initMiles and mileAge are both private fields, and have constructors set initial values to zero. (MPG variable = 24).

initMiles will always be zero, however, mileAge will be incremented from a void method. The first iteration of the source code above will be set to

int driven = 0 - 1;

if( -1 % 24 == 0)

How does java work with the negative integer operations? Can I disregard the negative integers?

This is the code I had to implement from someone else because I got stuck where I had to make the program know exactly when ( in this case 24) to switch on/off and I tried that with nested for loops and ended up

confusing myself the entire time I was coding. I would greatly appreciate any advice in thinking into writing such codes in the way I provided above and in general. Thank you.

Campbell Ritchie

Marshal

Posts: 56599

172

posted 2 years ago

Or you can look in the Java® Language Specification, but that can be difficult to read.

paul c. kim

Greenhorn

Posts: 14

posted 2 years ago

Well, I have mentioned earlier that the source code was implemented from someone else's idea, which I had adopted to my program. The source code I had stated above does it's job by iterating the statements if the boolean is true. I can provide all the source codes for the class if that is required to identify the solution to my question.

posted 2 years ago

If it seems I'm being around-about with my answers, it's because we at the Ranch try to have the OP get to the answer through self-discovery, with little nudges in the right direction.

Have you tried doing something like this?

When you're doing a % 24 operation above? I would think so, but it depends on the program.

How does java work with the negative integer operations?

Have you tried doing something like this?

Can I disregard the negative integers?

When you're doing a % 24 operation above? I would think so, but it depends on the program.

All things are lawful, but not all things are profitable.

paul c. kim

Greenhorn

Posts: 14

posted 2 years ago

Well, technically, you can't just ignore the sign -- as the result would be mathematically incorrect.

For example, the expression "-1 % 24" results with a value of "-1". The positive remainder result is actually "23" -- and not "1", which would be ignoring the sign on the "-1" which is returned.

Henry

Knute Snortum wrote:

Can I disregard the negative integers?

When you're doing a % 24 operation above? I would think so, but it depends on the program.

Well, technically, you can't just ignore the sign -- as the result would be mathematically incorrect.

For example, the expression "-1 % 24" results with a value of "-1". The positive remainder result is actually "23" -- and not "1", which would be ignoring the sign on the "-1" which is returned.

Henry

paul c. kim

Greenhorn

Posts: 14

Campbell Ritchie

Marshal

Posts: 56599

172

posted 2 years ago

That is how the Java® Language Specification defines it. If you can find a copy of the IEEE754 standard, that may help explain it.

There are all sorts of conventions for sign of remainders. You can have a positive regardless remainder, or you can have the sign dependent on the sign of one of the operands. The designers of the language have to decide on a convention and stick to it. Note that if you have positive regardless remainders, you might end up with this sort of thing:--7 / 3 = -3 because of rounding down rather than rounding towards 0. -7 % 3 = (+)2 -3 × 3 + 2 = 7 That is how some languages might do it, not necessarily Java®.

There are all sorts of conventions for sign of remainders. You can have a positive regardless remainder, or you can have the sign dependent on the sign of one of the operands. The designers of the language have to decide on a convention and stick to it. Note that if you have positive regardless remainders, you might end up with this sort of thing:-

Campbell Ritchie

Marshal

Posts: 56599

172

posted 2 years ago

First of all, this discussion has nothing (or little) to do with Java. We are talking eight grade mathematics here... so, apologies if I messed this up, as it has been a very long time since I was in the eighth grade...

The Java "%" operator is the "remainder" operator. The result is the remainder after the whole components are calculated out (sorry, if I messed up the terminology).

So, for example... the expression "43 % 5" is equal to "3", as 8 times 5 gets you 40, leaving you a remainder of 3... ie. 8 * 5 + 3 = 43.

Interestingly ... the expression "43 % 5" is also equal to "-2", as 9 times 5 gets you 45, leaving you a remainder of -2 ... ie. 9 * 5 - 2 = 43.

Doing the remainder math for "-1 % 24", the possible results are "-1" and "23", as zero times 24 gets you zero, leaving a remainder of -1; and as -1 times 24 gets you -24, leaving a remainder of 23, respectively.

Now, as for Java, it seems to like to return the sign based on the left operand... so, "-1 % 24" is returned as "-1" (instead of 23).

Henry

paul c. kim wrote:I am currently replying from mobile phone so I can't quote. Henry, could you explain how -1 % 24 results in -1? Have I misunderstood your comment by any chance?

First of all, this discussion has nothing (or little) to do with Java. We are talking eight grade mathematics here... so, apologies if I messed this up, as it has been a very long time since I was in the eighth grade...

The Java "%" operator is the "remainder" operator. The result is the remainder after the whole components are calculated out (sorry, if I messed up the terminology).

So, for example... the expression "43 % 5" is equal to "3", as 8 times 5 gets you 40, leaving you a remainder of 3... ie. 8 * 5 + 3 = 43.

Interestingly ... the expression "43 % 5" is also equal to "-2", as 9 times 5 gets you 45, leaving you a remainder of -2 ... ie. 9 * 5 - 2 = 43.

Doing the remainder math for "-1 % 24", the possible results are "-1" and "23", as zero times 24 gets you zero, leaving a remainder of -1; and as -1 times 24 gets you -24, leaving a remainder of 23, respectively.

Now, as for Java, it seems to like to return the sign based on the left operand... so, "-1 % 24" is returned as "-1" (instead of 23).

Henry

paul c. kim

Greenhorn

Posts: 14

posted 2 years ago

So each time I use the modulus operator on the values, Java compiler automatically produces two answers, the remainder, and the least absolute remainder? When you have stated an outcome such as -1 % 24 equals 23, I would've assumed that because I wasn't sure which type of outcome java provides for "%" operand. Also, if you pay close attention to my comments, I was actually curious of whether the outcome of negative integer has an effect of the source code I have provided above, and if there are any alternative ways to get the same operation. Thank you though, for your kind explanation of "eight grade mathematics" which was indeed, none or little reference in regards to my request in help.

Campbell Ritchie

Marshal

Posts: 56599

172

Campbell Ritchie

Marshal

Posts: 56599

172

posted 2 years ago

There is no “seems” about it. The JLS is very definite about the sign and magnitude of the result of applying the % operator.Henry Wong wrote: . . .

Now, as for Java, it seems to like to return the sign based on the left operand... so, "-1 % 24" is returned as "-1" (instead of 23).

Henry