Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add a √ character to the pool of operators #767

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

bmansfie
Copy link

Not a character typically available from standard keyboards. I'm using this library as a backend for a calculator and it makes a lot more sense to use an operator here rather than function names like nthRoot() and sqrt(). Especially nthRoot is much less intuitive. My cursory testing has shown that the changes function as desired.

There is a precedence problem though between the unary square root operator and the n root operator.

√4√4 results in √(4√4) instead of the desired (√4)√4. It's a narrow case and it isn't obvious to me how to fix it inside of your precedence system so I'm ignoring it for the moment in favor of more pressing defects in my own code base.

@josdejong
Copy link
Owner

Thanks for your PR Brandon, nice idea to add support for to do things like √4.

I'm not sure about using for nthRoot too, it may be too ambiguous. I suppose we enter nthRoot(8,3) as 3√8 and not as 8√3?. √4√4 should indeed work like (√(4))*(√(4)). People may be surprised to find that 3√8 is not evaluated as a regular multiplication 3 * √(8) but as nthRoot.

An alternative solution would be to only support notations like √4 and √(4) for sqrt, and √(8,3) for nthRoot but I don't know whether that would be satisfying your application.

@bmansfie
Copy link
Author

bmansfie commented Jan 3, 2017 via email

@josdejong
Copy link
Owner

Shall we give this a bit more thought and see whether you can collect more feedback from your users on this subject?

@bmansfie
Copy link
Author

bmansfie commented Jan 3, 2017 via email

@josdejong
Copy link
Owner

Thanks for the feedback! No time pressure on my side - it's an open source project :D

@infusion
Copy link
Collaborator

I like the idea of adding unicode symbols as actual operators. But I'm against 3√8 being pow(8,1/3)! I would see it as syntactical sugar for a square-root with literal. Even √(123 + 888) looks pretty strange, since the top-line you normally would have is not there as an indicator.

@josdejong
Copy link
Owner

Thanks for sharing your thoughts @infusion , I agree with you, I think 3√8 would be confusing.

@josdejong
Copy link
Owner

Any news on this?

I'm still in for using as an alias for sqrt, but not for using it like 3√8.

@infusion
Copy link
Collaborator

infusion commented Jul 29, 2017

3√8 is clear in the way that it means 3*sqrt(8) I think, especially since it is a quite common form to write in math when factored out 3*3. I would prefer the following syntax:

  • Allow "√const", meaning sqrt(const)
  • Disallow "√expression"
  • Allow "expr√const", meaning expr*sqrt(const)
  • Parser SHOULD bind the const to the sqrt symbol. (√4)√4should be incorrect syntax, but √4√4 is ok.

@josdejong
Copy link
Owner

I agree with you @infusion .

I'm not sure what you mean with your fourth bullet, shouldn't (√4)√4 be interpreted as an implicit multiplication (√4) * √4?

@infusion
Copy link
Collaborator

"√4√4 results in √(4√4) instead of the desired (√4)√4." from the initial message of the PR. It was just an expression that I second that behavior.

@josdejong
Copy link
Owner

ehhh, so you also want to interpret √4√4 as (√4)*(√4) and not as √(4 * √4) too? In that case we're on the same page.

I would give a very high precedence, the same as factorial ! and transpose ', and higher than multiplication and power. That will result in the desired behavior.

(See reference docs on operator preference: http://mathjs.org/docs/expressions/syntax.html#precedence)

@FSMaxB
Copy link
Collaborator

FSMaxB commented Jul 30, 2017

Then what does √4! mean?

@josdejong
Copy link
Owner

We have to decide whether or ! has highest precedence. Both is fine with me, I have a slight preference for giving the highest precedence, i.e. evaluate it as (√4)!. We should look around and see what other calculators/math applications do in this case, see if there is already a standard behavior there.

@FSMaxB
Copy link
Collaborator

FSMaxB commented Jul 30, 2017

I personally would also give higher precedence.

List of other calculators I've tested:

  • gnome-calculator: ! has higher precedence.
  • google: ! has higher precedence (used sqrt instead of √)
  • wolfram-alpha: has higher precedence, but when typing (without typing enter) it randomly does one or the other
  • android calculator (LineageOS): has higher precedence

Needs more samples!

@josdejong
Copy link
Owner

Just to add to the list:

  • Qalculate! gives ! highest precedence

@harrysarson
Copy link
Collaborator

Could be treated as a function rather than an operator. Then parenthesis are required and no one has to worry about operator precidence? People would write √(9) rather than √9.

@infusion
Copy link
Collaborator

ehhh, so you also want to interpret √4√4 as (√4)*(√4) and not as √(4 * √4) too? In that case we're on the same page.
Right :)

Personally, I would bind only the first symbol after to it. That means √x! is either an error for real results or uses the gamma function (I'm not sure of the behavior of math.js there.

@harrysarson I like the idea of treating it as a function, even if it would be nice to be able to write √123 intuitively.

@josdejong
Copy link
Owner

Could √ be treated as a function rather than an operator. Then parenthesis are required and no one has to worry about operator precidence? People would write √(9) rather than √9.

I like that Idea @harrysarson . I think though that people really want to be able to just enter √9 too, and that it will feel really counter-intuitive if you can't. I like the option of implementing it as a unary operator most (having high precedence, higher than !).

@gwhitney
Copy link
Collaborator

I plan to update/reimplement this on current mathjs, according to the precedence and other details as discussed.

@josdejong
Copy link
Owner

👍 thanks Glen

@gwhitney
Copy link
Collaborator

gwhitney commented Oct 2, 2023

Best-laid plans and all that -- that was over a year ago. Can't say I know when/whether I'd get back to this, but it would still be a good change.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants