4 Replies Latest reply: Sep 24, 2012 5:08 AM by DarrylBurke

# Question about how Modulus works

Hi there I just started programing and was practicing by making a program that converts meters into inches, feet, and miles. I finished it up and decided that rather than having feet be in decimal I wanted to make it look better by cutting off the decimal and converting it to inches.

What however my formula which was inches%12 wasn't giving me just the decimal. It gives me the decimal in inches. So using 10 meters converting it into inches I get 393.7 inches and then divided by 12 gives you 32.8 and some change. But instead of just giving me that .8 and change it was giving me .8x12 to get 9.6 and change.

It's what I want in the end but I'm completely confused on how the program is getting it when it shouldn't ( I think?)

My Code (I entered the remain line since I wanted to see the actual value.
--------------------------------------------------------------------------------------------------------------------------------------------

import java.util.*;

public class Prac4 { public static void main(String[] args) {
Scanner scan = new Scanner(System.in);

System.out.print("Enter a measurement in meters: ");
double meters = scan.nextDouble();

double inches = meters*39.37;
int feet =(int) inches/12;
double remain = inches % 12;
double finches = remain*12;
double miles = feet/1760.0;

System.out.println("Remain: " + remain);
System.out.println(meters +" meters is equivalent to "+ inches +" inches");
System.out.println(meters +" meters is equivalent to "+ feet +" feet and " + finches + " inches");
System.out.println(meters +" meters is equivalent to "+ miles +" miles");

} }

----------------------------------------------------------------------------------------------------------------------------
What happens when you run the program

Enter a measurement in meters: 10
Remain: 9.699999999999989
10.0 meters is equivalent to 393.7 inches
10.0 meters is equivalent to 32 feet and 116.39999999999986 inches
10.0 meters is equivalent to 0.01818181818181818 miles
• ###### 1. Re: Question about how Modulus works
>
It's what I want in the end but I'm completely confused on how the program is getting it when it shouldn't ( I think?)
>
Yes it should.

393.7 divided by 12 = 32.80833

An even number of feet is 32 so 32 * 12 = 384 inches.

You started with 393.7 inches so 393.7 - 384 = 9.7 inches remaining.
• ###### 2. Re: Question about how Modulus works
Ah I see now. I was under the misunderstanding that Java converted to decmial form when instead it stays elementry and just leaves everything as a remainder and doesn't convert it.

Alright that makes a lot of sense now. Thank you.

EDIT: I think the reason I was getting tripped up was because when I was just having inches/12 it was giving me the answer with a decimal form and I was expecting the modulus to do the same but only the decimal part.

Edited by: 960813 on Sep 23, 2012 6:28 PM
• ###### 3. Re: Question about how Modulus works
For the detailed spec see 15.17.3. Remainder Operator % (the entire section 15.17 really) of The Java Language Specification
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html
>
The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.

In C and C++, the remainder operator accepts only integral operands, but in the Java programming language, it also accepts floating-point operands.

The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to a.

This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0).

It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor.

If the value of the divisor for an integer remainder operator is 0, then an ArithmeticException is thrown.
• ###### 4. Re: Question about how Modulus works
Cross posted (and didn't bother to inform the other forum about the responses and help received here)