-
Notifications
You must be signed in to change notification settings - Fork 544
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
[Proposal] Add Num.cbrt #890
Comments
Can be trivially added in core or as a foreign helper. The only thing that
annoy me is the lack of reusablility that lots of `Num` methods signature
has. Because of implementation details it can become an issue.
One side note, maybe you can speed up your implementation a bit by doing
something like: `num.sign * num.abs.pow(1/3)`
|
Thanks for that suggestion. I just tried it and it's definitely faster for a small number of calls but gets slower as the number of calls increases, possibly because internally there's always three method calls instead of one. Incidentally, does Wren ever inline anything such as simple property getters? I was wondering how expensive it is to use |
If you need the patch, it can be done in less than 5 minutes.
No, inlining is an advanced feature that the wren trivial compiler will
never be able to do.
It is not hard to read it just a matter of getting used to.
|
Yeah, that's what I suspected given that the compiler is only single pass. At least though method calls are fast relative to comparable languages. Whilst personally I'd like to see Num.cbrt, I don't want to clutter up the Num class unless others would find it useful too. |
|
Hey, thanks for doing that :) Looks good to go. Think I can manage the docs myself: cbrtThe cube root of the number. |
fwiw I don't think |
Well, I don't think you could use |
I just had a quick look around to see what other languages use and every one I looked at which has one - C++, Java, C#, Rust, Javascript, Ruby and Python(numpy) - use the name So, elegant or not, at least it would be familiar to most people. I suppose you could use |
Honestly I've never in my life seen the cube root function named differently than |
Yea it's reasonable as convention similar to sqrt, apparently I've never used this function so wasn't familiar with it's presence in other languages, my comment stemmed from that. |
Well all these names comes from a time completion was not really a thing,
space was costly, and utf-8 was a dream. These days, we could also have
used the utf-8 character to have an square/cube root unary operators, same
thing with greek symbols.
|
Yeah however:
|
Sure, what I meant was that all in all `cbrt`, `cubic_root`, `3√`, proper
unicode symbol, ... It does not matter, as long as it follows some
logic/convention/... to help memories it. Human is the most adaptable
machine ever, but if we can help it a little it is better.
|
Thought I'd just add that Gravity also has a They also have |
|
Precisely :) |
Actually, that wouldn't work if However, it's still not worth adding IMO as roots above the third don't crop up very often in my experience. |
Just noticed there is a typo in my comment for the patch, will issue a PR. |
The lack of a built-in cube root function has been causing me a problem lately.
I had been using
x.pow(1/3)
until I realized that this doesn't work whenx
is negative becauseNum.pow
then returns NaN. So I'm now using (in my Math library):which works fine even for infinities or NaN.
However, it would be nice if this function were 'on tap' given that it could be implemented by simply calling the C99 library function,
cbrt(x)
, which should execute much more quickly than a Wren implementation. It may also use a better algorithm than my 'naive' version above.Although cube roots don't come up as often as square roots, they're still quite common in mathematical work which is probably why they're in the C99 (and also Go) standard libraries in the first place.
A worthwhile addition? Comments welcome.
The text was updated successfully, but these errors were encountered: