hilaloytun
2022-7-24 08:58:39

Hello, this time I have a math question. I need to keep the result greater than 0. How can we do that? (define (handle-key c ke) (cond [(and (key=? ke “up”) (< c HEI)) (+ c (/ c 3))] [(and (key=? ke “down”) (> c 0)) (- c (/ c 5))] ;; This shouldn’t be lower than 0. Otherwise it errors at the end: “rectangle: expects a non negative real number as first argument, given –0.0664” [else c]))


soegaard2
2022-7-24 09:59:45

@hilaloytun The easiest is to use (max 0 x) since if x is negative, 0 will the greater of the two numbers.


laurent.orseau
2022-7-24 13:21:10

Your function handle-key always returns a positive value if the input value c is positive, so if you see a negative c that means it comes from somewhere else (i.e., this function is not at fault)