473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Advice on some code

32
I'll be posting the code in the next post I just want to list an explanation and some known bugs.

Note: This is NOT a homework assignment, I'm doing this just because I was interesting in trying to do it.

This code is to be an object that represents a rational number with a numerator and denominator and provides the simple addition subtraction multiplication and division functions.

Possible inefficiency:
It might be best for me to check for greatest common factor using a table of primes, but that might take too much memory to hold.

Another option is to start at the smaller number and check in reverse by decrementing a count variable until the square root of the smaller number is reached, if both are divisable by a numer, then its the greatest common factor. I don't know if that would be more efficient or not.

Known/suspected bug.
If a a mathematical operation is attempted that causes multiplication (addition might cause multiplication so that the fraction denominators match), and the absolute value of the resulting numerator or denominator is too large for the variable type, even if the answer would have simplified to be in-range, the program will probably crash

I don't know how to solve this, I've been thinking it through, and the only idea I've had so far was to limit the numerator/denominator to have max values that are the square root of the normal max-value. The only problem with this solution is prime numbers might prevent simplification in these circumstances.

Does anyone have any ideas or see any other bugs?
Dec 2 '06 #1
12 2072
kotoro
32
Expand|Select|Wrap|Line Numbers
  1. public class RatNum {
  2.     private int Num=0, Den = 1;
  3.  
  4.     public RatNum() {
  5.         Num = 0;
  6.         Den = 1;
  7.         return;
  8.     }
  9.  
  10.     private void copy(RatNum other)
  11.     {
  12.         Num=other.Num;
  13.         Den=other.Den;
  14.     }
  15.  
  16.     public RatNum(Double decimal) {
  17.         copy(new RatNum("" + decimal));
  18.         return;
  19.     }
  20.  
  21.     public RatNum(RatNum other){
  22.         Num=other.Num;
  23.         Den=other.Den;
  24.     }
  25.  
  26.     public RatNum(String s) {
  27.         int numer = 0, denom = 0;
  28.         int decpos = s.indexOf(".");
  29.         int slashpos = s.indexOf("/");
  30.         if (decpos > -1) {
  31.             Double dec = Double.parseDouble(s);
  32.             if (dec == 0) {
  33.                 denom = 1;
  34.                 return;
  35.             }
  36.             s = "" + dec;
  37.             String frac = s.substring(decpos + 1);
  38.             String whole = s.substring(0, decpos);
  39.             if (frac.equals("0")) {
  40.                 Num = Integer.parseInt(whole);
  41.                 return;
  42.             }
  43.             String d = "1";
  44.             if (whole.equals("0")) {
  45.                 whole = "";
  46.             }
  47.             while (frac.length() > 0) {
  48.                 whole += frac.substring(0, 1);
  49.                 frac = frac.substring(1);
  50.                 d += "0";
  51.             }
  52.             numer = Integer.parseInt(whole);
  53.             denom = Integer.parseInt(d);
  54.         }
  55.         if (slashpos > -1) {
  56.             String Snum=s.substring(0, slashpos);
  57.             String Sden=s.substring(slashpos + 1);
  58.             numer = Integer.parseInt(Snum);
  59.             denom = Integer.parseInt(Sden);
  60.         }
  61.         if (denom == 0) {
  62.             throw new NumberFormatException();
  63.         }
  64.         Num = numer;
  65.         Den = denom;
  66.         Simplify();
  67.         return;
  68.  
  69.     }
  70.  
  71.     public RatNum(int n) {
  72.         Num = n;
  73.     }
  74.  
  75.     public RatNum(int n, int d) {
  76.         if (d == 0) {
  77.             throw new NumberFormatException();
  78.         }
  79.         if (d < 0) {
  80.             n = n * -1;
  81.             d = d * -1;
  82.         }
  83.  
  84.         Num = n;
  85.         Den = d;
  86.         Simplify();
  87.     }
  88.  
  89.     public RatNum add(RatNum other) {
  90.         int lcd=LCD(other);
  91.         int a,b;
  92.         a=lcd/Den;
  93.         b=lcd/other.Den;
  94.         return new RatNum(a*Num+b*other.Num,lcd);
  95.     }
  96.  
  97.     public RatNum add(int n) {
  98.         return add(new RatNum(n));
  99.     }
  100.  
  101.     public RatNum add(int n, int d) {
  102.         return add(new RatNum(n, d));
  103.     }
  104.  
  105.     public RatNum add(double decimal) {
  106.         return add(new RatNum(decimal));
  107.     }
  108.  
  109.     public RatNum subtract(RatNum other) {
  110.         return add(new RatNum(other.Num * -1, other.Den));
  111.     }
  112.  
  113.     public RatNum subtract(int n) {
  114.         return subtract(new RatNum(n));
  115.     }
  116.  
  117.     public RatNum subtract(int n, int d) {
  118.         return subtract(new RatNum(n, d));
  119.     }
  120.  
  121.     public RatNum subtract(double decimal) {
  122.         return subtract(new RatNum(decimal));
  123.     }
  124.  
  125.     public RatNum multiply(RatNum other) {
  126.         int aNum,aDen;
  127.         aNum=Num*other.Num;
  128.         aDen=Den*other.Den;
  129.         RatNum answer= new RatNum(aNum,aDen);
  130.         return answer;
  131.     }
  132.  
  133.     public RatNum multiply(int n) {
  134.         return multiply(new RatNum(n));
  135.     }
  136.  
  137.     public RatNum multiply(int n, int d) {
  138.         return multiply(new RatNum(n, d));
  139.     }
  140.  
  141.     public RatNum multiply(double decimal) {
  142.         return multiply(new RatNum(decimal));
  143.     }
  144.  
  145.     public RatNum divide(RatNum other) {
  146.         int recipNum, recipDen;
  147.         recipNum = other.Den;
  148.         recipDen = other.Num;
  149.         int aNum,aDen;
  150.         aNum=Num*recipNum;
  151.         aDen=Den*recipDen;
  152.         RatNum answer= new RatNum(aNum,aDen);
  153.         return answer;
  154.     }
  155.  
  156.     public RatNum divide(int n) {
  157.         return divide(new RatNum(n));
  158.     }
  159.  
  160.     public RatNum divide(int n, int d) {
  161.         return divide(new RatNum(n, d));
  162.     }
  163.  
  164.     public RatNum divide(double decimal) {
  165.         return divide(new RatNum(decimal));
  166.     }
  167.  
  168.     public String toString() {
  169.         return "" + Num + "/" + Den;
  170.     }
  171.  
  172.     public LongRatNum toLongRatNum() {
  173.         return new LongRatNum(toString());
  174.     }
  175.  
  176.     public double toDouble() {
  177.         double a = Num, b = Den;
  178.         return a / b;
  179.     }
  180.  
  181.     public int toInt() {
  182.         return Num / Den;
  183.     }
  184.  
  185.     public int LCD(RatNum other) {
  186.         int a=Den;
  187.         int b=other.Den;
  188.         if(a==b){
  189.             return a;
  190.         }
  191.         if(a<b&&b%a==0){
  192.             return b;
  193.         }
  194.         if(b<a&&a%b==0){
  195.             return a;
  196.         }
  197.         int gcf=GCF(Den,other.Den);
  198.         int uf=(a/gcf)*(b/gcf);
  199.         return gcf*uf;
  200.  
  201.     }
  202.  
  203.     private void Simplify() {
  204.         if(Num==0)
  205.         {
  206.             Den=1;
  207.             return;
  208.         }
  209.         if(Den<0){
  210.             Num=Num*-1;
  211.             Den=Den*-1;
  212.         }
  213.         int gcf = GCF(Num, Den);
  214.         Num = Num / gcf;
  215.         Den = Den / gcf;
  216.     }
  217.  
  218.     public int GCF(int a, int b) {
  219.         if(a==0||b==0)
  220.         {
  221.             return 1;
  222.         }
  223.         int gcf=1;
  224.         IntNode headA,headB,tailA,tailB;
  225.         headA=new IntNode(1);
  226.         headB=new IntNode(1);
  227.         tailA=headA;
  228.         tailB=headB;
  229.         if(a<b&&b%a==0) {
  230.             return a;
  231.         }
  232.         if(b<a&&a%b==0) {
  233.             return b;
  234.         }
  235.         if(a==b) {
  236.             return a;
  237.         }
  238.         IntNode currentA=headA;
  239.         for(int number=2;number<=Math.sqrt(a);number++) {
  240.             if(a%number==0){
  241.                 currentA.next=new IntNode(currentA,number);
  242.                 tailA=currentA.next;
  243.             }
  244.         }
  245.         IntNode currentB=headB;
  246.         for(int number=2;number<=Math.sqrt(b);number++) {
  247.             if(b%number==0){
  248.                 currentB.next=new IntNode(currentB,number);
  249.                 tailB=currentB.next;
  250.             }
  251.         }
  252.         currentA=tailA;
  253.         while(currentA!=null){
  254.             currentB=tailB;
  255.             while(currentB!=null){
  256.                 if(currentA.data==currentB.data){
  257.                     gcf=currentA.data;
  258.                 }
  259.                 currentB=currentB.prev;
  260.             }
  261.             currentA=currentA.prev;
  262.         }
  263.         return gcf;
  264.     }
  265.  
  266.     /**
  267.      * @return the den
  268.      */
  269.     public int getDen() {
  270.         return Den;
  271.     }
  272.  
  273.     /**
  274.      * @return the num
  275.      */
  276.     public int getNum() {
  277.         return Num;
  278.     }
  279.  
  280.     /* (non-Javadoc)
  281.      * @see java.lang.Object#hashCode()
  282.      */
  283.     @Override
  284.     public int hashCode() {
  285.         final int PRIME = 31;
  286.         int result = 1;
  287.         result = PRIME * result + Den;
  288.         result = PRIME * result + Num;
  289.         return result;
  290.     }
  291.  
  292.     /* (non-Javadoc)
  293.      * @see java.lang.Object#equals(java.lang.Object)
  294.      */
  295.     @Override
  296.     public boolean equals(Object obj) {
  297.         if (this == obj)
  298.             return true;
  299.         if (obj == null)
  300.             return false;
  301.         if (getClass() != obj.getClass())
  302.             return false;
  303.         final RatNum other = (RatNum) obj;
  304.         if (Den != other.Den)
  305.             return false;
  306.         if (Num != other.Num)
  307.             return false;
  308.         return true;
  309.     }
  310.  
  311.  
  312. }
Expand|Select|Wrap|Line Numbers
  1. public class IntNode {
  2.     public int data;
  3.  
  4.     public IntNode next;
  5.  
  6.     public IntNode prev;
  7.  
  8.     public IntNode() {
  9.         this.data = 0;
  10.         this.next = null;
  11.     }
  12.  
  13.     public IntNode(int data) {
  14.         this.data = data;
  15.         this.next = null;
  16.     }
  17.  
  18.     public IntNode(int data, IntNode next) {
  19.         this.data = data;
  20.         this.next = next;
  21.         this.prev = null;
  22.     }
  23.  
  24.     public IntNode(IntNode prev, int data, IntNode next) {
  25.         this.data = data;
  26.         this.next = next;
  27.         this.prev = prev;
  28.     }
  29.  
  30.     public IntNode(IntNode prev, int data) {
  31.         this.data = data;
  32.         this.next = null;
  33.         this.prev = prev;
  34.     }
  35. }
Dec 2 '06 #2
kotoro
32
I made another version of this for largerand smaller numbers using longs to hold the integers

Expand|Select|Wrap|Line Numbers
  1. public class LongRatNum {
  2.     private long Num=0, Den = 1;
  3.  
  4.     public LongRatNum() {
  5.         Num = 0;
  6.         Den = 1;
  7.         return;
  8.     }
  9.  
  10.     private void copy(LongRatNum other)
  11.     {
  12.         Num=other.Num;
  13.         Den=other.Den;
  14.     }
  15.  
  16.     public LongRatNum(Double decimal) {
  17.         copy(new LongRatNum("" + decimal));
  18.         return;
  19.     }
  20.  
  21.     public LongRatNum(LongRatNum other){
  22.         Num=other.Num;
  23.         Den=other.Den;
  24.     }
  25.  
  26.     public LongRatNum(RatNum other){
  27.         copy(other.toLongRatNum());
  28.     }
  29.  
  30.     public LongRatNum(String s) {
  31.         long numer = 0, denom = 0;
  32.         int decpos = s.indexOf(".");
  33.         int slashpos = s.indexOf("/");
  34.         if (decpos > -1) {
  35.             Double dec = Double.parseDouble(s);
  36.             if (dec == 0) {
  37.                 denom = 1;
  38.                 return;
  39.             }
  40.             s = "" + dec;
  41.             String frac = s.substring(decpos + 1);
  42.             String whole = s.substring(0, decpos);
  43.             if (frac.equals("0")) {
  44.                 Num = Long.parseLong(whole);
  45.                 return;
  46.             }
  47.             String d = "1";
  48.             if (whole.equals("0")) {
  49.                 whole = "";
  50.             }
  51.             while (frac.length() > 0) {
  52.                 whole += frac.substring(0, 1);
  53.                 frac = frac.substring(1);
  54.                 d += "0";
  55.             }
  56.             numer = Long.parseLong(whole);
  57.             denom = Long.parseLong(d);
  58.         }
  59.         if (slashpos > -1) {
  60.             String Snum=s.substring(0, slashpos);
  61.             String Sden=s.substring(slashpos + 1);
  62.             numer = Long.parseLong(Snum);
  63.             denom = Long.parseLong(Sden);
  64.         }
  65.         if (denom == 0) {
  66.             throw new NumberFormatException();
  67.         }
  68.         Num = numer;
  69.         Den = denom;
  70.         Simplify();
  71.         return;
  72.  
  73.     }
  74.  
  75.     public LongRatNum(int n) {
  76.         Num = n;
  77.     }
  78.  
  79.     public LongRatNum(int n, int d) {
  80.         if (d == 0) {
  81.             throw new NumberFormatException();
  82.         }
  83.         if (d < 0) {
  84.             n = n * -1;
  85.             d = d * -1;
  86.         }
  87.  
  88.         Num = n;
  89.         Den = d;
  90.         Simplify();
  91.     }
  92.  
  93.     public LongRatNum(long n) {
  94.         Num = n;
  95.     }
  96.  
  97.     public LongRatNum(long n, long d) {
  98.         if (d == 0) {
  99.             throw new NumberFormatException();
  100.         }
  101.         if (d < 0) {
  102.             n = n * -1;
  103.             d = d * -1;
  104.         }
  105.  
  106.         Num = n;
  107.         Den = d;
  108.         Simplify();
  109.     }
  110.  
  111.     public LongRatNum add(LongRatNum other) {
  112.         long lcd=LCD(other);
  113.         long a,b;
  114.         a=lcd/Den;
  115.         b=lcd/other.Den;
  116.         return new LongRatNum(a*Num+b*other.Num,lcd);
  117.     }
  118.  
  119.     public LongRatNum add(RatNum other) {
  120.         LongRatNum other2= new LongRatNum(other.toString());
  121.         long lcd=LCD(other2);
  122.         long a,b;
  123.         a=lcd/Den;
  124.         b=lcd/other2.Den;
  125.         return new LongRatNum(a*Num+b*other2.Num,lcd);
  126.     }
  127.  
  128.     public LongRatNum add(int n) {
  129.         return add(new LongRatNum(n));
  130.     }
  131.  
  132.     public LongRatNum add(int n, int d) {
  133.         return add(new LongRatNum(n, d));
  134.     }
  135.     public LongRatNum add(long n) {
  136.         return add(new LongRatNum(n));
  137.     }
  138.  
  139.     public LongRatNum add(long n, long d) {
  140.         return add(new LongRatNum(n, d));
  141.     }
  142.  
  143.     public LongRatNum add(double decimal) {
  144.         return add(new LongRatNum(decimal));
  145.     }
  146.  
  147.     public LongRatNum subtract(LongRatNum other) {
  148.         return add(new LongRatNum(other.Num * -1, other.Den));
  149.     }
  150.  
  151.     public LongRatNum subtract(RatNum other) {
  152.         LongRatNum other2= new LongRatNum(other.toString());
  153.         return add(new LongRatNum(other2.Num * -1, other2.Den));
  154.     }
  155.  
  156.     public LongRatNum subtract(int n) {
  157.         return subtract(new LongRatNum(n));
  158.     }
  159.  
  160.     public LongRatNum subtract(int n, int d) {
  161.         return subtract(new LongRatNum(n, d));
  162.     }
  163.  
  164.     public LongRatNum subtract(long n) {
  165.         return subtract(new LongRatNum(n));
  166.     }
  167.  
  168.     public LongRatNum subtract(long n, long d) {
  169.         return subtract(new LongRatNum(n, d));
  170.     }
  171.  
  172.     public LongRatNum subtract(double decimal) {
  173.         return subtract(new LongRatNum(decimal));
  174.     }
  175.  
  176.     public LongRatNum multiply(LongRatNum other) {
  177.         long aNum,aDen;
  178.         aNum=Num*other.Num;
  179.         aDen=Den*other.Den;
  180.         LongRatNum answer= new LongRatNum(aNum,aDen);
  181.         return answer;
  182.     }
  183.  
  184.     public LongRatNum multiply(RatNum other) {
  185.         LongRatNum other2= new LongRatNum(other.toString());
  186.         long aNum,aDen;
  187.         aNum=Num*other2.Num;
  188.         aDen=Den*other2.Den;
  189.         LongRatNum answer= new LongRatNum(aNum,aDen);
  190.         return answer;
  191.     }
  192.  
  193.     public LongRatNum multiply(int n) {
  194.         return multiply(new LongRatNum(n));
  195.     }
  196.  
  197.     public LongRatNum multiply(int n, int d) {
  198.         return multiply(new LongRatNum(n, d));
  199.     }
  200.  
  201.     public LongRatNum multiply(long n) {
  202.         return multiply(new LongRatNum(n));
  203.     }
  204.  
  205.     public LongRatNum multiply(long n, long d) {
  206.         return multiply(new LongRatNum(n, d));
  207.     }
  208.  
  209.     public LongRatNum multiply(double decimal) {
  210.         return multiply(new LongRatNum(decimal));
  211.     }
  212.  
  213.     public LongRatNum divide(LongRatNum other) {
  214.         long recipNum, recipDen;
  215.         recipNum = other.Den;
  216.         recipDen = other.Num;
  217.         long aNum,aDen;
  218.         aNum=Num*recipNum;
  219.         aDen=Den*recipDen;
  220.         LongRatNum answer= new LongRatNum(aNum,aDen);
  221.         return answer;
  222.     }
  223.  
  224.     public LongRatNum divide(RatNum other) {
  225.         LongRatNum other2= new LongRatNum(other.toString());
  226.         long recipNum, recipDen;
  227.         recipNum = other2.Den;
  228.         recipDen = other2.Num;
  229.         long aNum,aDen;
  230.         aNum=Num*recipNum;
  231.         aDen=Den*recipDen;
  232.         LongRatNum answer= new LongRatNum(aNum,aDen);
  233.         return answer;
  234.     }
  235.  
  236.     public LongRatNum divide(int n) {
  237.         return divide(new LongRatNum(n));
  238.     }
  239.  
  240.     public LongRatNum divide(int n, int d) {
  241.         return divide(new LongRatNum(n, d));
  242.     }
  243.  
  244.     public LongRatNum divide(long n) {
  245.         return divide(new LongRatNum(n));
  246.     }
  247.  
  248.     public LongRatNum divide(long n, long d) {
  249.         return divide(new LongRatNum(n, d));
  250.     }
  251.  
  252.     public LongRatNum divide(double decimal) {
  253.         return divide(new LongRatNum(decimal));
  254.     }
  255.  
  256.     public String toString() {
  257.         return "" + Num + "/" + Den;
  258.     }
  259.  
  260.     public double toDouble() {
  261.         double a = Num, b = Den;
  262.         return a / b;
  263.     }
  264.  
  265.     public long toLong() {
  266.         return Num / Den;
  267.     }
  268.  
  269.     public long LCD(LongRatNum other) {
  270.         long a=Den;
  271.         long b=other.Den;
  272.         if(a==b){
  273.             return a;
  274.         }
  275.         if(a<b&&b%a==0){
  276.             return b;
  277.         }
  278.         if(b<a&&a%b==0){
  279.             return a;
  280.         }
  281.         long gcf=GCF(Den,other.Den);
  282.         long uf=(a/gcf)*(b/gcf);
  283.         return gcf*uf;
  284.  
  285.     }
  286.  
  287.     private void Simplify() {
  288.         if(Num==0)
  289.         {
  290.             Den=1;
  291.             return;
  292.         }
  293.         if(Den<0){
  294.             Num=Num*-1;
  295.             Den=Den*-1;
  296.         }
  297.         long gcf = GCF(Num, Den);
  298.         Num = Num / gcf;
  299.         Den = Den / gcf;
  300.     }
  301.  
  302.     public long GCF(long a, long b) {
  303.         if(a==0||b==0)
  304.         {
  305.             return 1;
  306.         }
  307.         long gcf=1;
  308.         LongNode headA,headB,tailA,tailB;
  309.         headA=new LongNode(1);
  310.         headB=new LongNode(1);
  311.         tailA=headA;
  312.         tailB=headB;
  313.         if(a<b&&b%a==0) {
  314.             return a;
  315.         }
  316.         if(b<a&&a%b==0) {
  317.             return b;
  318.         }
  319.         if(a==b) {
  320.             return a;
  321.         }
  322.         LongNode currentA=headA;
  323.         for(long number=2;number<=Math.sqrt(a);number++) {
  324.             if(a%number==0){
  325.                 currentA.next=new LongNode(currentA,number);
  326.                 tailA=currentA.next;
  327.             }
  328.         }
  329.         LongNode currentB=headB;
  330.         for(long number=2;number<=Math.sqrt(b);number++) {
  331.             if(b%number==0){
  332.                 currentB.next=new LongNode(currentB,number);
  333.                 tailB=currentB.next;
  334.             }
  335.         }
  336.         currentA=tailA;
  337.         while(currentA!=null){
  338.             currentB=tailB;
  339.             while(currentB!=null){
  340.                 if(currentA.data==currentB.data){
  341.                     gcf=currentA.data;
  342.                 }
  343.                 currentB=currentB.prev;
  344.             }
  345.             currentA=currentA.prev;
  346.         }
  347.         return gcf;
  348.     }
  349.  
  350.     /**
  351.      * @return the den
  352.      */
  353.     public long getDen() {
  354.         return Den;
  355.     }
  356.  
  357.     /**
  358.      * @return the num
  359.      */
  360.     public long getNum() {
  361.         return Num;
  362.     }
  363.  
  364.     /* (non-Javadoc)
  365.      * @see java.lang.Object#hashCode()
  366.      */
  367.     @Override
  368.     public int hashCode() {
  369.         final int PRIME = 31;
  370.         int result = 1;
  371.         result = PRIME * result + (int) (Den ^ (Den >>> 32));
  372.         result = PRIME * result + (int) (Num ^ (Num >>> 32));
  373.         return result;
  374.     }
  375.  
  376.     /* (non-Javadoc)
  377.      * @see java.lang.Object#equals(java.lang.Object)
  378.      */
  379.     @Override
  380.     public boolean equals(Object obj) {
  381.         if (this == obj)
  382.             return true;
  383.         if (obj == null)
  384.             return false;
  385.         if (getClass() != obj.getClass())
  386.             return false;
  387.         final LongRatNum other = (LongRatNum) obj;
  388.         if (Den != other.Den)
  389.             return false;
  390.         if (Num != other.Num)
  391.             return false;
  392.         return true;
  393.     }
  394.  
  395.  
  396. }
Expand|Select|Wrap|Line Numbers
  1. public class LongNode {
  2.     public long data;
  3.  
  4.     public LongNode next;
  5.  
  6.     public LongNode prev;
  7.  
  8.     public LongNode() {
  9.         this.data = 0;
  10.         this.next = null;
  11.     }
  12.  
  13.     public LongNode(long data) {
  14.         this.data = data;
  15.         this.next = null;
  16.     }
  17.  
  18.     public LongNode(long data, LongNode next) {
  19.         this.data = data;
  20.         this.next = next;
  21.         this.prev = null;
  22.     }
  23.  
  24.     public LongNode(LongNode prev, long data, LongNode next) {
  25.         this.data = data;
  26.         this.next = next;
  27.         this.prev = prev;
  28.     }
  29.  
  30.     public LongNode(LongNode prev, long data) {
  31.         this.data = data;
  32.         this.next = null;
  33.         this.prev = prev;
  34.     }
  35. }
Dec 2 '06 #3
kotoro
32
I'd post my test driver code, but its larger than the Limit for the number of characters per message.
Dec 2 '06 #4
kotoro
32
an example of the problem i'm talking about would be the following:

if my code were altered to run on this variable type and I did the following:
Expand|Select|Wrap|Line Numbers
  1. RatNum a=new(46341); //creates 46341/1 without problems
  2. RatNum b=new(1,46341); // creates 1/46341 without problems
  3. RatNum c=a.add(b); // Here is where I get a bug
  4.  
the program would try to modify 46341/1 to 2147488281/46341, t it looks like going over the maxvalue puts you in the negative range, because the data held in the object is now -2147479013/46341 instead of 2147488282/46341


--also I just noticed a problem with the way my program parses small doubles, I'll be investigating my constructor for problems with it later.
Dec 2 '06 #5
Ganon11
3,652 Expert 2GB
While I'm not sure how to help you with your adding errors, I know I recently had an assignment to implement a greatest common denominator function recursively. If you can wait until Monday, I can post the formula or the code, depending on how much you'd like to do on your own.

Of course, as I just realized, the code is in C++, so you'd have to translate to Java anyway.
Dec 3 '06 #6
kotoro
32
I'm thinking it may be sidesteppable if I set the max value of the whole thing to be the square root of MAX_VALUE by throwing an error or something at the simplify step, and make the smallest increment the equivalent of 1 / Math.sqrt(MAXVALUE); of course i'd be converting the square root of maxvalue to an integer before doing it. Rather than only controlling the max values of namerator and denominator independently (which I should also do)
Dec 3 '06 #7
kotoro
32
I know the program is quite large and a lot to sort through, I gave it several copies of each type of procedure so it could do math with several types of input, I've tested the math functions, and as long as you don't do anything that causes the variables to pass their max_values at any point, the procedures work fine, my issue is figuring out how to prevent the data from being spoiled when incompatible numbers are being mathmatically combined. I say inompatible to mean when the fraction is likely to become irreducible to within the bounds that will be set on the values.
Dec 4 '06 #8
Ganon11
3,652 Expert 2GB
How often do you expect this to happen? You could have your program run with the warning to users that it does not handle fractions past a certain point of accuracy. I know it's skipping around the solution rather than solving it, but...I have no ideas, you have no ideas, and no one else seems to have any input...
Dec 4 '06 #9
r035198x
13,262 8TB
How often do you expect this to happen? You could have your program run with the warning to users that it does not handle fractions past a certain point of accuracy. I know it's skipping around the solution rather than solving it, but...I have no ideas, you have no ideas, and no one else seems to have any input...
These decisions have to be made everytime one thinks of writting a program. You are not going to have a solution that will make an int hold a value lager than the largest int. The only option is to specify the behaviour of the program when the data type is overrun. A good method is to throw an exception. You could also change the data types you've selected and use say BigInteger depending on the purpose of your program.
Dec 5 '06 #10
kotoro
32
I like the idea of throwing an exception, is there a pre-existing exception that looks like it makes sense to use here like a math error exception or something?
Otherwise how does one create a new exception type? I haven't figured that one out yet.
Dec 7 '06 #11
r035198x
13,262 8TB
I like the idea of throwing an exception, is there a pre-existing exception that looks like it makes sense to use here like a math error exception or something?
Otherwise how does one create a new exception type? I haven't figured that one out yet.
Better to read a tutorial on exception handling first. It wouldn't take you long(They are usually very short and precise). You should also have a look at the strictfp keyword. It might come in there somewhere.
Dec 7 '06 #12
kotoro
32
I already know about try{}catch() is there anything else that I need to know (We did a little bit of this in class, but I doubt we covered everything)
Dec 9 '06 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Rick Muller | last post by:
I have a problem that I would like to get some advice on from other Pythonistas. I currently manage a (soon to be) open source project for displaying molecular graphics for a variety of different...
3
by: Andy Dingley | last post by:
I've just started on a new project and inherited a huge pile of XSLT (and I use the term "pile" advisedly !) It runs at glacial speed, and I need to fix this this. Platform is MSXML 4 / ASP ...
4
by: Marquisha | last post by:
If this is off-topic, please forgive me. But I thought this might be the perfect spot to get some advice about how to proceed with a project. Working on a Web site design for a nonprofit...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
13
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those...
9
by: Duncan Smith | last post by:
Hello, I find myself in the, for me, unusual (and at the moment unique) position of having to write a web application. I have quite a lot of existing Python code that will form part of the...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
3
by: MIUSS | last post by:
Hi! I am modifying some part of source code and I now I am suspended by one thing I really don't understand and I got no one experience with it. I got fully working source code (it reports no...
0
by: waggledance | last post by:
I was wondering if anyone here might be able to offer me some advice for someone who can only use Macromedia Dreamweaver MX. I am (clearly!) a web design novice so apologies in advance if this is a...
7
by: SM | last post by:
Hello, I have a index.php template (2 columns). The right columns contains a bunch of links (interviews, poems, etc...) The left columns contains the actual article. So if I click on a link on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.