Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Negative value from modulus?  RSS feed

 
paul c. kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you try it?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can look in the Java® Language Specification, but that can be difficult to read.
 
paul c. kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Seems to me that mileAge and initMiles should be swapped.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
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.
 
paul c. kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried with the swapped formation, gives me the same outcome. So java disregards the negative integer from the modulus?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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®.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, I wrote:. . . The designers of the language have to decide on a convention and stick to it. . . .
    Not like Forth where they have been trying to change the convention for about the last 5 years.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    paul c. kim wrote:So each time I use the modulus operator on the values, Java compiler automatically produces two answers, . . .
    No. You get one result and one only. It says in the Language Specification what its sign is.
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    There is no “seems” about it. The JLS is very definite about the sign and magnitude of the result of applying the % operator.
     
    paul c. kim
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, Ritchie, for your clarification.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!