Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 4
    I agree with the comment wondering whether having (neg % pos) go negative is ever useful? On a related note, I wonder if the required arithmetically-incorrect behavior in some cases of "unsignedvar > signedvar" is ever useful? I can understand the rationale for not requiring always-correct behavior; I see no rationale for requiring wrong behavior. Commented Aug 30, 2010 at 18:34
  • 9
    +1 for an excellent reference on why flooring is the correct behavior for integer division (contrary to C's definition, which is broken and almost-never useful). Commented Aug 30, 2010 at 19:19
  • @supercat Consider: filtered = (k - 1) * filtered + value + carry; carry = filtered % factor; filtered /= factor, iterated with changing values of value. It makes a nice integer approximation to a first-order lowpass filter with time constant k... but it's only symmetric if division is truncating and carry gets negative values. Both behaviors for division come in handy from time to time. Commented Dec 21, 2017 at 15:01
  • 1
    @hobbs: I don't think the above code would behave cleanly when signals cross zero. If div is a floored division operator and factor is odd, then filtered += (filter+(factor div 2)) div factor would yield clean and symmetrical behavior for all values up to INT_MAX-(factor div 2). Commented Dec 21, 2017 at 18:35
  • @supercat it does work though; that code is only slightly distilled from something I've had running in the controller of an atomic clock for a while. Commented Jan 16, 2018 at 22:11