Shop OBEX P1 Docs P2 Docs Learn Events
Error in Simple_Numbers library object — Parallax Forums

Error in Simple_Numbers library object

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2012-05-06 16:33 in Propeller 1
Here's the program:
CON

  _clkmode      = xtal1 + pll16x
  _xinfreq      = 5_000_000

OBJ

  pst   : "Parallax Serial Terminal"
  num   : "Simple_Numbers"

PUB  start | seed, value, best, worst, sum, n

  pst.start(9600)
  pst.str(num.decx(1_580_219_585, 10))

Here's what gets output:
0170154177

-Phil

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-06 14:28
    You probably already know this but it looks like this line is the culprit.
      value //= (div * 10)                                  ' truncate unused digits
      
    

    "div * 10" in this case is over 32 bits.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-05-06 14:37
    Duane,

    Thanks. I hadn't bothered yet to find the cause, but I'm sure you're right.

    -Phil
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-06 14:42
    I modified method I use a lot to return a string address instead of having the string as an argument in the call.

    I think most of it originated in Tim Moore's four port serial object.

    The number displays correctly with this modified method (there are actually two different methods used).
    PUB Decx(value, digits)  
    '' Prints zero-padded, signed-decimal string
    '' -- if value is negative, field width is digits+1
      result := decl(value, digits, 2)
    PUB Decl(value, digits, flag) | localI, localX, localPtr
    '' DWD Fixed with FDX 1.2 code
      digits := 1 #> digits <# 10
      localPtr := @nstr  
      localX := value == NEGX       'Check for max negative
      if value < 0
        value := ||(value + localX) 'If negative, make positive; adjust for max negative
        byte[localPtr++] := "-"
      localI := 1_000_000_000
      if flag & 3
        if digits < 10                                      ' less than 10 digits?
          repeat (10 - digits)                              '   yes, adjust divisor
            localI /= 10
      repeat digits
        if value => localI
          byte[localPtr++] := value / localI + "0" + localX * (localI == 1)
          value //= localI
          result~~
        elseif (localI == 1) OR result OR (flag & 2)
          byte[localPtr++] := "0"
        elseif flag & 1
          byte[localPtr++] := " "
        localI /= 10
      result := @nstr
        
    
    

    It should be cleaned up before being added to Simple_Numbers officially.

    Again, most of it is not my code.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-05-06 16:33
    Frankly, I've never liked the way Simple_Numbers handles fixed-length fields when it comes to negative numbers. To make the columns line up correctly, the minus sign should be included in the digit count. IMO, anything short of that is just plain lazy programming.

    -Phil
Sign In or Register to comment.