-
Notifications
You must be signed in to change notification settings - Fork 6
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
Add sqrt primitive. #57
Conversation
I require some guidance. I have put the necessary tests for positive numbers but I am unsure of what to do with negative numbers. In Java SOM, sqrt of negative numbers returns "NaN" and I am not sure what the equivalent of that is in yksom after some initial searching in the codebase. Would appreciate some help! |
It should probably either produce |
OK, having taken a quick look at this, I think the correct thing to do (in IEEE 574) is to return NaN, so let's do that (which, conveniently, is what Rust's |
Just implemented NaN for negative square roots. Let me know if you want me to squash. |
Removing all the above if statements produced the following error (for Integers): |
Let's remove the |
Done! |
Please squash. |
Squashed |
I just realised a problem. If |
If this is ready for rereview, you need to tell me explicitly. |
I know, I only just got round to doing a double check now (I've had coursework from my modules recently), and I think there's nothing more I need to do so feel free to take a look! |
I saw this before, and thought it is sensible. However, triggered by SOM-st/SOM#24, does yksom give the expected results for
and:
|
Good point! No, this PR will not produce ints even when it could. I'm not even entirely sure I know the best way to implement this semantically. Obvious possibilities are 1) see if the FP number produced is int-like and if so convert it 2) convert the result to an int, square it, and see if you get the original answer again. I think those two routes will behave subtly different in some edge conditions, though I haven't checked. Any thoughts? |
SOM and CSOM do basically the same for the case with an Integer as a receiver. https://github.com/SOM-st/CSOM/blob/master/src/primitives/Integer.c#L267-L274 Note that |
Ah, and at this point, in the sense of a spec, I wouldn't consider this required behavior... I think, I'd need to find the people who implemented that originally and get their reasoning, i.e., do some historical digging. |
OK, so that's option #1 of the two I suggested. It's a reasonable enough definition, at least from this PR's perspective. @ummarikar hopefully that gives you a bit of feedback for how to improve the PR a bit?
Fair enough. I think it's not a bad idea to implement it similarly though -- if nothing else, reducing the points of divergence (as far as possible) to only clear bugs will probably make everybody's life easier! |
Yes, this is great I'll implement it asap. Thanks for the feedback! |
Just did some playing around with yksom. It has the following behaviour:
I'll make sure that Integer sqrt produces a Double if the result is not a whole number. |
The links Stefan sent to other SOM's might give you a good pointer for how to go about this. |
Added some code to val.rs which ensures that a Double is returned when the square root of an Integer is not a whole number. I was not sure if you want me to change the implementation of sqrt for BigInts when the result is not a whole number. The current implementation in Java SOM is to return an error in this case. |
I also added an extra test case for "3 sqrt". |
We need to make sure both |
I think we also need checks along the lines of:
to make sure that things are printing to check that |
Sure I'll start that now. |
All done. |
Please squash. |
This adds the sqrt primitive to yksom for Int, BigInt and Double.
Squashed. |
bors r+ |
57: Add sqrt primitive. r=ltratt a=ummarikar This adds the sqrt primitive to yksom for Int, BigInt and Double. Co-authored-by: K1630549 <[email protected]>
Build succeeded |
This adds the sqrt primitive to yksom for Int, BigInt and Double.