Int32 divide probleme
I try to divide 2 integers and get the remainder using the Int32 library from Parallax site.
Below my code:
import stamp.math.Int32;
public class DivisionTest {
· static Int32 num1 = new Int32();
· static Int32 num2 = new Int32();
· public static void main() {
······ num1.set(14512);
······ num2.set(5);
······ System.out.println("Before divide:");
······ System.out.println("num1: " + num1.toString());
······ System.out.println("num2: " + num2.toString());
······ num1.divide(num2);
······ num1.remainder(num2);
······ System.out.println("After divide:");
······ System.out.println("num1: " + num1.toString());
······ System.out.println("num2: " + num2.toString());
· }
}
Here's the output:
Before divide:
num1: 14512
num2: 5
After divide:
num1: 2902
num2: 2
The remainder·never seem to be exact. Is it a error in the library or there something that I don't catch?
Thanks in advance!!
Below my code:
import stamp.math.Int32;
public class DivisionTest {
· static Int32 num1 = new Int32();
· static Int32 num2 = new Int32();
· public static void main() {
······ num1.set(14512);
······ num2.set(5);
······ System.out.println("Before divide:");
······ System.out.println("num1: " + num1.toString());
······ System.out.println("num2: " + num2.toString());
······ num1.divide(num2);
······ num1.remainder(num2);
······ System.out.println("After divide:");
······ System.out.println("num1: " + num1.toString());
······ System.out.println("num2: " + num2.toString());
· }
}
Here's the output:
Before divide:
num1: 14512
num2: 5
After divide:
num1: 2902
num2: 2
The remainder·never seem to be exact. Is it a error in the library or there something that I don't catch?
Thanks in advance!!
Comments
14512/5 = 2902.4
int(14512/5) = 2902
remain(14512/5) = 14512 - (2902*5) = 14512 - 14510 = 2
Edit:
num1.divide(5) = 14512/5 = 2902 (is now num1)
num1.remainder(5) = 2902 % 5 = 2
In this case both remainders are 2.
Is the calculation the one you wanted
or did you actually wanted to calculate 14512 % 5?
regards peter
Post Edited (Peter Verkaik) : 11/6/2006 8:33:33 PM GMT
I'm french so I think I just misunderstand the words;
So the remainder function actualy return the modulo. Is it right?
Thank you very much Peter
if z = x % y
then·fraction = z/y
You can calculate the fraction (for ints)
using UnsignedIntMath.frac(z,y) or UnsignedIntMath.ufrac(z,y)
You could rewrite those functions for Int32
(and also for·the other functions in UnsignedIntMath class)
regards peter
Thank you very much for your help!