-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
check if unsigned numbers underflow #1025
Conversation
I tested it and this provides correct underflow errors. I think it would make sense to use
Otherwise, this seems solid. |
87b405a
to
e962184
Compare
Looks good to me! I have no gripes. I think there should be a future issue to have unsigned numbers error on overflow too, but that can be a separate thing. I'm going to approve it but I'll wait to see if anyone else wants to review it before merging. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR on the whole isn't structured correctly. We are defining Num for the same unsigned types multiple times, once for wrapping behavior, and then for underflow behavior. We can only define them once.
library/math/num.lisp
Outdated
`(define-instance (Num ,type) | ||
(define (+ a b) | ||
(lisp ,type (a b) | ||
(cl:if (cl:and (cl:< b 0) (cl:< a (cl:- 0 b))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(< x 0) ==> (minusp x)
(- 0 x) ==> (- x)
library/math/num.lisp
Outdated
|
||
(define (- a b) | ||
(lisp ,type (a b) | ||
(cl:if (cl:and (cl:>= b 0) (cl:< a (cl:+ 0 b))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b is guaranteed >= 0 for unsigned numbers
library/math/num.lisp
Outdated
`(define-instance (Num ,type) | ||
(define (+ a b) | ||
(lisp ,type (a b) | ||
(cl:if (cl:and (cl:< b 0) (cl:< a (cl:- 0 b))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b can never be negative for unsigned numbers
library/math/num.lisp
Outdated
|
||
(define (* a b) | ||
(lisp ,type (a b) | ||
(cl:if (cl:or (cl:and (cl:and (cl:> b 0) (cl:< a 0)) (cl:< a (cl:/ 0 b))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsigned multiplication can't underflow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean by this? are you saying the code that i wrote doesnt detect the underflow or that in general unsigned multiplication doesnt underflow?
wouldnt this be an underflow: 2 * -3
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-3 isn't an unsigned number
(Incidentally this means we have no tests that actually look for correct overflow / underflow behavior.) |
@stylewarning thank you for this. I will try to address your review as soon as I can. Thanks. For the wrapper, I just have to make it so when the unsigned values underflow, the user can continue, if they want, with the highest number possible of the given unsigned type right? so if two addition of U8 integers underflow, the wrapper needs to allow the user continue from the error with the wrapped number which in this case would be 255? correct? ust trying to make sure I am thinking the right thing. thanks |
e962184
to
42ef9e3
Compare
42ef9e3
to
37854e6
Compare
@Izaakwltn addressed your review |
37854e6
to
ac8b9bc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your patience in review.
This PR needs to modify define-num-wrapping
's definition of -
and fromInt
only. Adding another define
macro is not needed (and is actually redundant), and the other "handler" functionality is not all that useful outside of the scope of this change, so it can be eliminated.
library/math/num.lisp
Outdated
@@ -161,6 +161,47 @@ | |||
;;; Num instances for integers | |||
;;; | |||
|
|||
(cl:defmacro %define-unsigned-underflow-handler (name bits) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this can be deleted
library/math/num.lisp
Outdated
(64 (cl:the (cl:unsigned-byte ,bits) 18446744073709551615))))) | ||
|
||
|
||
(%define-unsigned-underflow-handler %unsigned-8-bit-underflow-handler 8) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these can be deleted
library/math/num.lisp
Outdated
(define-num-wrapping UFix #.+unsigned-fixnum-bits+)) | ||
(define-num-wrapping UFix #.+unsigned-fixnum-bits+) | ||
|
||
(define-unsigned-num-underflow U8 %unsigned-8-bit-underflow-handler 8) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are re-defining Num
for types that already have it. Above, define-num-wrapping
is already defining -
and the like for each of the listed types.
mybad for the late reply. But your comment means that I only pretty much have take what I wrote in my function and move to the |
Yes, essentially. |
Ok I’ll get it done ASAP. |
ac8b9bc
to
a4a252c
Compare
At least one test failing due to the
Your I would recommend loading the coalton test suite and either running all tests locally or just running Edit: in terms of the failing iterator test, I think the culprit is behavior in iter:range-decreasing. |
429e5fa
to
7665659
Compare
I think You might be able to do an |
7665659
to
2ba6cd6
Compare
@Izaakwltn @stylewarning hey guys, my apologies for the delay on this issue. Im going to try izaak's suggestions today or tomorrow and figure out the cause. i do plan to finish this. |
Due to the age of this PR, I'm going to be closing it. Please feel free to open another PR when you believe you've solved the issue. If the issue needs more clarification, please ask in the issue. |
@Izaakwltn Hopefully this is not an issue that I made another pr. I had git issues with the other one.
This solves #921.
This is the interaction I just had in this new pr:
Thanks