Shop OBEX P1 Docs P2 Docs Learn Events
How do I handle this division equation on the Propeller? — Parallax Forums

How do I handle this division equation on the Propeller?

Don MDon M Posts: 1,652
edited 2014-03-02 08:52 in Propeller 1
Lets say I have a counter counting up seconds. When I'm ready to display the elapsed time I want to display anything over 60 seconds as minutes & seconds.

So if I have a total seconds of say 125. So lets assign a long variable named time that adds up all the seconds.
time := time + seconds     ' time = 125

minutes := time / 60         ' answer is 2.083


So I know that minutes = 2 since the Prop doesn't deal with less than whole numbers but how do I get the .083 to convert back into seconds?

Thanks.
Don

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2014-03-02 08:44
    Actually, the division operator does integer division, so the answer is 2, not 2.083. There's a corresponding remainder operation, "//" and "125 // 60" is 5 which is exactly what you want.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-03-02 08:45
    Look up the modulus operator // in the Spin reference.

    -Phil
  • JonnyMacJonnyMac Posts: 9,107
    edited 2014-03-02 08:46
    This is where the modulus (remainder of a division) operator is your friend. I'm coding a laser-tag application that has no spare cogs, so I synthesize the game timer in the main loop like this:
    t1 := cnt
      repeat
    
        ' loop code
    
        t2 := cnt
        elapsedtix += t2 - t1
        if (elapsedtix => CLK_FREQ)
          gametimer += elapsedtix / CLK_FREQ
          elapsedtix //= CLK_FREQ
        t1 := t2
    


    I convert seconds to HH:MM:SS format like this:
    pub show_time(s) | h, m
    
    '' Display timer as HH:MM:SS
    '' -- time passed in seconds
    
      h := s  / 3600
      m := s // 3600 / 60
      s := s // 60
    
      dec2(h)
      term.tx(":")
      dec2(m)
      term.tx(":")
      dec2(s)
    
  • Don MDon M Posts: 1,652
    edited 2014-03-02 08:52
    Thanks gentleman. Exactly what I needed.
Sign In or Register to comment.