Skip to content

Commit

Permalink
fix result precision + null log
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkempire78 committed Jan 31, 2023
1 parent 4d9d4ad commit 9be97c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/com/darkempire78/opencalculator/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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))

Expand All @@ -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)
Expand Down

0 comments on commit 9be97c4

Please sign in to comment.