'-' prefix in ASCII string representation negative integers refuses to stick

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

'-' prefix in ASCII string representation negative integers refuses to stick

Rather Iffy
This post was updated on .
This code has a bug somewhere: it does not correctly handle -32767 , but -7 is correct.
-----------------------------------------------------------------------------------------------------------------------------
    method void int2String(int number) {
      var int n, lastDigit, c ;
      let n = number;
      let lastDigit =  n - (10 * Math.divide(n,10));
      let c  = 48 + lastDigit ;                  
      if (n < 10)     { do appendChar(c);                  
                           return ;}
      else            {  do setInt(Math.divide(n,10));
                           do  appendChar(c);return;}
    }

    /** Sets this String to hold a representation of the given number. */
    method void setInt(int number) {
      let length = 0;
      if (number < 0) { do appendChar(45);
                              do int2String(-number);                
                              return; }
      else {                do int2String(number);
                              return;}            
    }  
-----------------------------------------------------------------------------------------------------------------------------





By calling int2String from within setInt the appended - prefix somehow gets lost.
Because the base case '-7' somehow works but negative integers > 10 not, maybe there is a problem with the recursion.

Also i am not sure what the return value of int2String should be : void or String ?
I  studied all the Jack documentation over and over but i cannot find the material that would indicate where i have been mistaken.

I would appreciate some help on this.
--Chrit
 
Reply | Threaded
Open this post in threaded view
|

Re: '-' prefix in ASCII string representation negative integers refuses to stick

cadet1620
Administrator
Every time you call setInt(), you start by clearing the string buffer by setting length = 0.
For -7 you don't recurse into setInt(), so the '-' remains in the string buffer.

For -17:
setInt(-17) clears the buffer, appends '-' and calls
    int2String(17) which calls
        SetInt(1) which clears the buffer and calls
            int2String(1) which appends '1' to the just cleared buffer and returns
        SetInt(1) returns -- the buffer contains "1"
    int2String(17) appends "7" to the buffer and returns
SetInt(-17) returns -- the buffer contains "17"

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: '-' prefix in ASCII string representation negative integers refuses to stick

Rather Iffy
This post was updated on .
Thnx hawkeye .

--Chrit

PS
I was not careful enough when i got the recursive part out of my first version of setInt.

At the other hand I successfully resisted the temptation to study the VM again to see what is going wrong under the hood.