From 9be97c4e2a27787ec02b47d18ae8946394c3832c Mon Sep 17 00:00:00 2001 From: Darkempire <50015928+Darkempire78@users.noreply.github.com> Date: Tue, 31 Jan 2023 15:18:11 +0100 Subject: [PATCH] fix result precision + null log --- .../com/darkempire78/opencalculator/Calculator.kt | 3 +++ .../darkempire78/opencalculator/MainActivity.kt | 15 +++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/darkempire78/opencalculator/Calculator.kt b/app/src/main/java/com/darkempire78/opencalculator/Calculator.kt index 9b7a740d..279d8322 100644 --- a/app/src/main/java/com/darkempire78/opencalculator/Calculator.kt +++ b/app/src/main/java/com/darkempire78/opencalculator/Calculator.kt @@ -10,6 +10,9 @@ class Calculator { if (number >= 171) { return Double.POSITIVE_INFINITY } + if (number <= 0) { + return Double.NaN + } val decimalPartOfNumber = number - number.toInt() return if (decimalPartOfNumber == 0.0) { var factorial = BigInteger("1") diff --git a/app/src/main/java/com/darkempire78/opencalculator/MainActivity.kt b/app/src/main/java/com/darkempire78/opencalculator/MainActivity.kt index e8267bf1..b0e769bd 100644 --- a/app/src/main/java/com/darkempire78/opencalculator/MainActivity.kt +++ b/app/src/main/java/com/darkempire78/opencalculator/MainActivity.kt @@ -273,6 +273,13 @@ class MainActivity : AppCompatActivity() { } } + private fun roundResult(result : Double): Double { + if (result.isNaN() || result == Double.POSITIVE_INFINITY || result == Double.NEGATIVE_INFINITY) { + return result + } + return BigDecimal(result).setScale(12, RoundingMode.HALF_EVEN).toDouble() + } + private fun updateResultDisplay() { lifecycleScope.launch(Dispatchers.Default) { val calculation = binding.input.text.toString() @@ -284,9 +291,9 @@ class MainActivity : AppCompatActivity() { var resultString = result.toString() var formattedResult = NumberFormatter.format(resultString.replace(".", NumberFormatter.decimalSeparatorSymbol)) - if (resultString != "NaN" && resultString != "Infinity" && resultString != getString(R.string.infinity)) { + if (resultString != "NaN" && resultString != "Infinity" && resultString != "-Infinity" && resultString != getString(R.string.infinity)) { // Round at 10^-12 - result = BigDecimal(result).setScale(12, RoundingMode.HALF_EVEN).toDouble() + result = roundResult(result) formattedResult = NumberFormatter.format(result.toString().replace(".", NumberFormatter.decimalSeparatorSymbol)) // If result = -0, change it to 0 @@ -478,7 +485,7 @@ class MainActivity : AppCompatActivity() { if (calculation != "") { val calculationTmp = Expression().getCleanExpression(binding.input.text.toString()) - val result = Calculator().evaluate(calculationTmp, isDegreeModeActivated) + val result = roundResult((Calculator().evaluate(calculationTmp, isDegreeModeActivated))) var resultString = result.toString() var formattedResult = NumberFormatter.format(resultString.replace(".", NumberFormatter.decimalSeparatorSymbol)) @@ -505,7 +512,7 @@ class MainActivity : AppCompatActivity() { binding.historyRecylcleView.scrollToPosition(historyAdapter.itemCount - 1) } - if (resultString != "NaN" && resultString != "Infinity" && resultString != getString(R.string.infinity)) { + if (resultString != "NaN" && resultString != "Infinity" && resultString != "-Infinity" && resultString != getString(R.string.infinity)) { if ((result * 10) % 10 == 0.0) { resultString = String.format("%.0f", result) formattedResult = NumberFormatter.format(resultString)