Skip to content
Browse files

Need to have a range from -maxValue..+maxValue not 0..maxValue

  • Loading branch information...
1 parent 7443f57 commit 8212621c073f7ab58483aa61a27da89fdfcef80b @palfrey committed Aug 20, 2012
Showing with 11 additions and 2 deletions.
  1. +11 −2 src/clostridium/core.clj
View
13 src/clostridium/core.clj
@@ -100,10 +100,19 @@
)
)
-(def maxValue Integer/MAX_VALUE)
+(def maxValue (quot Integer/MAX_VALUE 2)) ; divided by two to avoid exceeding MAX_VALUE while we're doing clipValue
+(def negMaxValue (* -1 maxValue))
+
+; want a value between -maxValue .. +maxValue
+; Start by adding maxValue to get all the valid values within 0..(2*maxValue)
+; Then mod to clip within that range
+; Then subtract maxValue to get it back to the original value
(defn clipValue [val]
- (mod val maxValue)
+ (if (and (< val maxValue) (< negMaxValue val))
+ val
+ (- (mod (+ val maxValue) (* maxValue 2)) maxValue)
+ )
)
(defn current

0 comments on commit 8212621

Please sign in to comment.
Something went wrong with that request. Please try again.