Shop OBEX P1 Docs P2 Docs Learn Events
Int32 divide probleme — Parallax Forums

Int32 divide probleme

js_flashjs_flash Posts: 4
edited 2006-11-08 19:22 in General Discussion
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!!

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-11-06 20:20
    The results look ok to me
    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
  • js_flashjs_flash Posts: 4
    edited 2006-11-06 21:43
    I thougth that the remainder function return the fractionnal part of the division like the .4 of 14512/5 = 2902.4

    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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-11-06 21:53
    Yes, the remainder is the modulo.
    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
  • js_flashjs_flash Posts: 4
    edited 2006-11-08 19:22
    Cool that perfect! You seem to be a lot more proficient en math then me. Luck you!!

    Thank you very much for your help!
Sign In or Register to comment.