-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Comments
Noted ! May I ask why wpuld try calcupate the sqrt of a negative number knowing its mathematically impossible ? 🤔 |
It is mathematically impossible when we work with real numbers, since the function Although, since you've not yet implemented the advanced/scientific mode, you don't have to worry about it, just put a check for Keep up the good work! |
Oof, imma be honest I suck at maths, thanks ! :) |
@sosauce to solve this first create a custom exception like 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"
} |
You should update the default sqrt unary operator in keval to throw NegativeSquareRootException |
good catch |
Thanks ;) |
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):
The text was updated successfully, but these errors were encountered: