Skip to content
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

Possible factorial error. #75

Open
a35joshi opened this issue Dec 8, 2016 · 5 comments
Open

Possible factorial error. #75

a35joshi opened this issue Dec 8, 2016 · 5 comments

Comments

@a35joshi
Copy link

a35joshi commented Dec 8, 2016

Hi.
I am not sure about this being a bug or not but I will put forward some of my findings.
So I defined the factorial method as given on [(http://www.objecthunter.net/exp4j/)].
I get an exception for 3!-2! but not for 3-2!(I get 1, as expected). But rightfully, (3!)-(2!) gives me the correct answer(i.e, 4).
What can be done in order for 3!-2! to work without having to enter brackets?

@dktcoding
Copy link

Yes, this actually related to #64 see pull request #74, I just added a test for this and it seems to be resolved in that branch (https://travis-ci.org/dktcoding/exp4j/builds/182453474)

@a35joshi
Copy link
Author

a35joshi commented Dec 9, 2016 via email

@dktcoding
Copy link

That's the special casing, the way it works is:

  1. The ! is declared to have only one argument (as it should)
  2. Since we need ! to be always on the right we treated as a binary operator with a null second argument (like num!null)
  3. The tokenizer doesn't actually count the number of available tokens, it uses some clever assumptions to try and figure out the argument count.
  4. Based on (3) we could trick it to believe that it should have two arguments when there's only one is available, that way the expression is always valid (because it has only one argument), but the operator is always on the right.

The problem is that this can't be done using an external Operator (at least I was unable to figure out how), only using a built-in so it's possible to special case it

That code you posted isn't from the original exp4j it's from a fork I created, the proposed fix is detailed in PR #74

@fasseg
Copy link
Owner

fasseg commented Dec 9, 2016

Hey Guys,
I do have January off and will invest some time to get exp4j up to date. Unfortunately I do not have enough time to do this properly earlier...

@fasseg
Copy link
Owner

fasseg commented Jan 30, 2017

Hey Guys,

I added the tests from @dktcoding and it does produce the correct values. Am I missing something there or has this bug mysteriously disappeared?
Commmit: e977f19

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

No branches or pull requests

3 participants