Issue #302: Add common math functions to RQuantity#445
Issue #302: Add common math functions to RQuantity#445Octogonapus merged 12 commits intoOkapiLib:developfrom breqdev:feature/WJC/#302_add_math_functions_rquantity
Conversation
|
It looks like the build failed. (I didn't think to run a I think this might be because the C I'm honestly not sure how best to proceed from here. I could put the functions into their own namespace, or make them member functions of RQuantity, but that doesn't seem ideal. |
Octogonapus
left a comment
There was a problem hiding this comment.
Thanks for taking the time to do this PR. I just had some small comments.
@theol0403 Can you comment on what may be missing from this PR?
|
Thanks for the feedback. To clarify, if (for instance) It looks like there are more files that use (e.g.) |
Yes.
Leave them in
Go ahead and change them. |
theol0403
left a comment
There was a problem hiding this comment.
Looks good to me, you've included all the useful functions.
I know you said it does not include rounding functions or log/exponent functions, but if possible those would be useful (especially pow). Pow would probably look different than the other methods, as the exponent would probably have to be given as a template parameter for it to know the return type. A good compromise would be a square and cube method.
Rounding methods like floor and ceil are also useful, but I can't think of a good way to express them in terms of units. An API such as floor<degree>(0.6_deg) might be possible, but that might belong to a separate PR.
I agree. @Breq16 can you try to implement pow this way?
I agree they are useful and I don't think it would be difficult to implement. Wouldn't round, floor, and ceil simply return the same dimensions as they accepted? |
The problem is that the rounding depends on the internal unit - round would work differently with degrees than radians for example, so we would need to specify what unit we want to round to. |
Oh, right. I forgot about that. I think it makes sense that we'd require the user to supply a specific unit to round in. For example, |
|
Finally, I think it would be a good idea to write some simple tests for the functionality of these new methods. Even if the highest chance of something breaking is within compile-time, there is always a possibility of having issues with certain types, or silly things like swapped parameters. Testing is definitely necessary for the rounding methods if you choose to implement them. Writing tests are fairly straightforward with googletest, but if you aren't comfortable with the testing system I'm happy to write them. |
|
I had a working implementation of Correct me if I'm wrong here, but it seems like the syntax for the rounding would be It seems like the other log and exponent functions wouldn't really work because of the way units are handled (for instance, I don't really have any experience writing tests, but I could give it a try. (It seems like the correct place to add them is in |
|
For Humm, you are correct. I'm not actually sure how you would go about this then - the unit parameter would be an RQuantity of the same dimension as the value parameter, but then the only thing that you can use to tell the units apart is the ratio to their underlying unit ( Yep, that's where you should put the unit tests. You just make a test case, and the testing framework will automagically add it to the tests which the CI will run. You can also run the tests locally by running the cmake commands outlined by the azure build file (in the project root). |
|
Cool, I'll get to work on the unit tests. With regards to |
|
Yeah, I'd keep them both as overloads. It might also be nice to have a |
Description of the Change
Add common math and trig functions (
sin(),cos(),sqrt(), etc.) to RQuantity (units API). This PR includes many of the functions present in the header (trigonometric and hyperbolic functions, a few basic root functions, modulus, etc). It does not include rounding functions or log/exponent functions.Motivation
Previously, using common math functions with the units API required converting a number from an RQuantity to a double, using the function, and then converting the number back to an RQuantity. This PR adds functions like
sin()that take RQuantity objects as arguments so users do not need to do this conversion.Possible Drawbacks
Because these functions are implemented as free functions as per this comment, they might clutter up the namespace a bit.
Verification Process
I tested each function I implemented on my desktop to ensure it gave valid results. (I haven't gotten the chance to test on a V5 brain, but compilation succeeded when I copied these changes into a new PROS project. Since most of the work done by the units API is at compile time, this seemed like a good sign.)
Applicable Issues
Closes #302.