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

Root of a negative number causes app crash #61

Closed
Ransomware3301 opened this issue Mar 4, 2024 · 7 comments
Closed

Root of a negative number causes app crash #61

Ransomware3301 opened this issue Mar 4, 2024 · 7 comments

Comments

@Ransomware3301
Copy link

Describe the bug
Taking the square root of a negative number causes an app crash

To Reproduce
Choose a negative number and take the square root of that

Expected behavior
Print "Error" instead of causing an app crash

Smartphone (please complete the following information):

  • Device: Pixel 6
  • OS: PixelOS
  • Version: 14
@sosauce
Copy link
Owner

sosauce commented Mar 4, 2024

Noted ! May I ask why wpuld try calcupate the sqrt of a negative number knowing its mathematically impossible ? 🤔

@Ransomware3301
Copy link
Author

Ransomware3301 commented Mar 4, 2024

It is mathematically impossible when we work with real numbers, since the function $f(x) = \sqrt{x}\space$ isn't defined for negative numbers, but if you take into account complex numbers, then you have the imaginary unit, defined as $i = \sqrt{-1}$ and this enables you to calculate for example: $\sqrt{-3}$ as $i\sqrt{3}$, thus eliminating the negative value in the radicand.

Although, since you've not yet implemented the advanced/scientific mode, you don't have to worry about it, just put a check for $x \geq 0$ and you're good to go.

Keep up the good work!

@sosauce
Copy link
Owner

sosauce commented Mar 4, 2024

Oof, imma be honest I suck at maths, thanks ! :)

@notKamui
Copy link
Contributor

notKamui commented Mar 7, 2024

@sosauce to solve this

first create a custom exception like class NegativeSquareRootException : RuntimeException("negative square root")

then in the definition of the operator you can do

unaryOperator {
    symbol = ''
    isPrefix = true
    implementation = { arg -> if (arg < 0) throw NegativeSquareRootException() else sqrt(arg) }
}

And finally you can do something like this in the eval function

    fun eval(formula: String): String = try {
        KEVAL.eval(formula).toBigDecimal().stripTrailingZeros().toPlainString()
    } catch (e: KevalZeroDivisionException) {
        "Undefined (zero division)"
    } catch (e: NegativeSquareRootException) {
        "Undefined in Reals (negative square root)"
    } catch (e: KevalException) {
        "Error"
    }

@realeatham
Copy link
Contributor

You should update the default sqrt unary operator in keval to throw NegativeSquareRootException
@notKamui

@notKamui
Copy link
Contributor

notKamui commented Mar 7, 2024

You should update the default sqrt unary operator in keval to throw NegativeSquareRootException @notKamui

good catch

@sosauce
Copy link
Owner

sosauce commented Mar 7, 2024

Thanks ;)

@sosauce sosauce closed this as completed Mar 7, 2024
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

4 participants