lets revert BigDecimal to Double, it was an overkill

This commit is contained in:
tibbi
2020-11-06 12:33:47 +01:00
parent 675901410b
commit f8f10c40ba
16 changed files with 54 additions and 81 deletions

View File

@ -7,11 +7,11 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.WindowManager import android.view.WindowManager
import com.simplemobiletools.calculator.BuildConfig
import com.simplemobiletools.calculator.R import com.simplemobiletools.calculator.R
import com.simplemobiletools.calculator.extensions.config import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.calculator.extensions.updateViewColors import com.simplemobiletools.calculator.extensions.updateViewColors
import com.simplemobiletools.calculator.helpers.* import com.simplemobiletools.calculator.helpers.*
import com.simplemobiletools.calculator.BuildConfig
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LICENSE_AUTOFITTEXTVIEW import com.simplemobiletools.commons.helpers.LICENSE_AUTOFITTEXTVIEW
import com.simplemobiletools.commons.helpers.LICENSE_ESPRESSO import com.simplemobiletools.commons.helpers.LICENSE_ESPRESSO
@ -20,7 +20,6 @@ import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.commons.models.Release import com.simplemobiletools.commons.models.Release
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import me.grantland.widget.AutofitHelper import me.grantland.widget.AutofitHelper
import java.math.BigDecimal
class MainActivity : SimpleActivity(), Calculator { class MainActivity : SimpleActivity(), Calculator {
private var storedTextColor = 0 private var storedTextColor = 0
@ -157,8 +156,8 @@ class MainActivity : SimpleActivity(), Calculator {
} }
// used only by Robolectric // used only by Robolectric
override fun setValueBigDecimal(d: BigDecimal) { override fun setValueDouble(d: Double) {
calc.setValue(Formatter.bigDecimalToString(d)) calc.setValue(Formatter.doubleToString(d))
calc.lastKey = DIGIT calc.lastKey = DIGIT
} }

View File

@ -1,12 +1,11 @@
package com.simplemobiletools.calculator.helpers package com.simplemobiletools.calculator.helpers
import android.content.Context import android.content.Context
import java.math.BigDecimal
interface Calculator { interface Calculator {
fun setValue(value: String, context: Context) fun setValue(value: String, context: Context)
fun setValueBigDecimal(d: BigDecimal) fun setValueDouble(d: Double)
fun setFormula(value: String, context: Context) fun setFormula(value: String, context: Context)
} }

View File

@ -6,7 +6,6 @@ import com.simplemobiletools.calculator.operation.OperationFactory
import com.simplemobiletools.calculator.operation.PercentOperation import com.simplemobiletools.calculator.operation.PercentOperation
import com.simplemobiletools.commons.extensions.areDigitsOnly import com.simplemobiletools.commons.extensions.areDigitsOnly
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import java.math.BigDecimal
class CalculatorImpl(calculator: Calculator, val context: Context) { class CalculatorImpl(calculator: Calculator, val context: Context) {
var displayedNumber: String? = null var displayedNumber: String? = null
@ -18,8 +17,8 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
private var isFirstOperation = false private var isFirstOperation = false
private var resetValue = false private var resetValue = false
private var baseValue: BigDecimal = BigDecimal.ZERO private var baseValue = 0.0
private var secondValue: BigDecimal = BigDecimal.ZERO private var secondValue = 0.0
private val operations = listOf("+", "-", "*", "/", "^", "%", "") private val operations = listOf("+", "-", "*", "/", "^", "%", "")
private var moreOperationsInRaw = false private var moreOperationsInRaw = false
@ -37,8 +36,8 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
} }
private fun resetValues() { private fun resetValues() {
baseValue = BigDecimal.ZERO baseValue = 0.0
secondValue = BigDecimal.ZERO secondValue = 0.0
resetValue = false resetValue = false
lastOperation = "" lastOperation = ""
displayedNumber = "" displayedNumber = ""
@ -63,7 +62,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
val sign = getSign(lastOperation) val sign = getSign(lastOperation)
if (sign.isNotEmpty()) { if (sign.isNotEmpty()) {
if (secondValue.compareTo(BigDecimal.ZERO) == 0 && sign == "/") { if (secondValue == 0.0 && sign == "/") {
context.toast(context.getString(R.string.formula_divide_by_zero_error)) context.toast(context.getString(R.string.formula_divide_by_zero_error))
} }
@ -102,16 +101,16 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
return str return str
} }
val doubleValue = Formatter.stringToBigDecimal(str) val doubleValue = Formatter.stringToDouble(str)
return doubleValue.format() return doubleValue.format()
} }
private fun updateResult(value: BigDecimal) { private fun updateResult(value: Double) {
setValue(value.format()) setValue(value.format())
baseValue = value baseValue = value
} }
private fun getDisplayedNumberAsDouble() = Formatter.stringToBigDecimal(displayedNumber!!) private fun getDisplayedNumberAsDouble() = Formatter.stringToDouble(displayedNumber!!)
fun handleResult() { fun handleResult() {
if (moreOperationsInRaw) { if (moreOperationsInRaw) {
@ -131,7 +130,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
} }
if (lastOperation == ROOT && inputDisplayedFormula.startsWith("")) { if (lastOperation == ROOT && inputDisplayedFormula.startsWith("")) {
baseValue = 1.toBigDecimal() baseValue = 1.0
} }
val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue) val operation = OperationFactory.forId(lastOperation!!, baseValue, secondValue)
@ -206,7 +205,7 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
private fun handlePercent() { private fun handlePercent() {
val operation = PercentOperation(baseValue, getSecondValue(), lastOperation ?: "") val operation = PercentOperation(baseValue, getSecondValue(), lastOperation ?: "")
val result = operation.getResult() val result = operation.getResult()
setFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue()}%") setFormula("${baseValue.format()}${getSign(lastOperation)}${getSecondValue().format()}%")
secondValue = result secondValue = result
updateResult(result) updateResult(result)
inputDisplayedFormula = displayedNumber ?: "" inputDisplayedFormula = displayedNumber ?: ""
@ -295,14 +294,14 @@ class CalculatorImpl(calculator: Calculator, val context: Context) {
setValue(inputDisplayedFormula) setValue(inputDisplayedFormula)
} }
private fun getSecondValue(): BigDecimal { private fun getSecondValue(): Double {
val valueToCheck = if (inputDisplayedFormula.startsWith("-")) { val valueToCheck = if (inputDisplayedFormula.startsWith("-")) {
inputDisplayedFormula.substring(1) inputDisplayedFormula.substring(1)
} else { } else {
inputDisplayedFormula inputDisplayedFormula
} }
return valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1).toBigDecimal() return valueToCheck.substring(valueToCheck.indexOfAny(operations, 0, false) + 1).toDouble()
} }
private fun zeroClicked() { private fun zeroClicked() {

View File

@ -1,24 +1,23 @@
package com.simplemobiletools.calculator.helpers package com.simplemobiletools.calculator.helpers
import java.math.BigDecimal
import java.text.DecimalFormat import java.text.DecimalFormat
import java.text.DecimalFormatSymbols import java.text.DecimalFormatSymbols
import java.util.* import java.util.*
object Formatter { object Formatter {
fun bigDecimalToString(d: BigDecimal): String { fun doubleToString(d: Double): String {
val symbols = DecimalFormatSymbols(Locale.US) val symbols = DecimalFormatSymbols(Locale.US)
symbols.decimalSeparator = '.' symbols.decimalSeparator = '.'
symbols.groupingSeparator = ',' symbols.groupingSeparator = ','
val formatter = DecimalFormat() val formatter = DecimalFormat()
formatter.maximumFractionDigits = 50 formatter.maximumFractionDigits = 12
formatter.decimalFormatSymbols = symbols formatter.decimalFormatSymbols = symbols
formatter.isGroupingUsed = true formatter.isGroupingUsed = true
return formatter.format(d) return formatter.format(d)
} }
fun stringToBigDecimal(str: String): BigDecimal = BigDecimal(str.replace(",", "")) fun stringToDouble(str: String) = str.replace(",", "").toDouble()
} }
fun BigDecimal.format(): String = Formatter.bigDecimalToString(this) fun Double.format(): String = Formatter.doubleToString(this)

View File

@ -13,7 +13,6 @@ import com.simplemobiletools.calculator.activities.MainActivity
import com.simplemobiletools.calculator.extensions.config import com.simplemobiletools.calculator.extensions.config
import com.simplemobiletools.commons.extensions.applyColorFilter import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.setText import com.simplemobiletools.commons.extensions.setText
import java.math.BigDecimal
class MyWidgetProvider : AppWidgetProvider(), Calculator { class MyWidgetProvider : AppWidgetProvider(), Calculator {
companion object { companion object {
@ -124,7 +123,7 @@ class MyWidgetProvider : AppWidgetProvider(), Calculator {
} }
} }
override fun setValueBigDecimal(d: BigDecimal) { override fun setValueDouble(d: Double) {
} }
override fun setFormula(value: String, context: Context) { override fun setFormula(value: String, context: Context) {

View File

@ -2,16 +2,8 @@ package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation import com.simplemobiletools.calculator.operation.base.Operation
import java.math.BigDecimal
import java.math.MathContext.DECIMAL128
class DivideOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation { class DivideOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult(): BigDecimal { override fun getResult() = baseValue / secondValue
return if (secondValue.compareTo(BigDecimal.ZERO) == 0) {
BigDecimal.ZERO
} else {
baseValue.divide(secondValue, DECIMAL128)
}
}
} }

View File

@ -2,9 +2,8 @@ package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation import com.simplemobiletools.calculator.operation.base.Operation
import java.math.BigDecimal
class MinusOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation { class MinusOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult(): BigDecimal = baseValue.subtract(secondValue) override fun getResult() = baseValue - secondValue
} }

View File

@ -2,9 +2,8 @@ package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation import com.simplemobiletools.calculator.operation.base.Operation
import java.math.BigDecimal
class MultiplyOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation { class MultiplyOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult(): BigDecimal = baseValue.multiply(secondValue) override fun getResult() = baseValue * secondValue
} }

View File

@ -2,11 +2,10 @@ package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.helpers.* import com.simplemobiletools.calculator.helpers.*
import com.simplemobiletools.calculator.operation.base.Operation import com.simplemobiletools.calculator.operation.base.Operation
import java.math.BigDecimal
object OperationFactory { object OperationFactory {
fun forId(id: String, baseValue: BigDecimal, secondValue: BigDecimal): Operation? { fun forId(id: String, baseValue: Double, secondValue: Double): Operation? {
return when (id) { return when (id) {
PLUS -> PlusOperation(baseValue, secondValue) PLUS -> PlusOperation(baseValue, secondValue)
MINUS -> MinusOperation(baseValue, secondValue) MINUS -> MinusOperation(baseValue, secondValue)

View File

@ -6,32 +6,29 @@ import com.simplemobiletools.calculator.helpers.MULTIPLY
import com.simplemobiletools.calculator.helpers.PLUS import com.simplemobiletools.calculator.helpers.PLUS
import com.simplemobiletools.calculator.operation.base.BinaryOperation import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation import com.simplemobiletools.calculator.operation.base.Operation
import java.math.BigDecimal
import java.math.MathContext
class PercentOperation(baseValue: BigDecimal, secondValue: BigDecimal, val sign: String) : BinaryOperation(baseValue, secondValue), Operation { class PercentOperation(baseValue: Double, secondValue: Double, val sign: String) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult(): BigDecimal { override fun getResult(): Double {
return when { return when (sign) {
secondValue.compareTo(BigDecimal.ZERO) == 0 -> BigDecimal.ZERO MULTIPLY -> {
sign == MULTIPLY -> { val partial = 100 / secondValue
val partial = BigDecimal(100).divide(secondValue, MathContext.DECIMAL32) baseValue / partial
baseValue.divide(partial, MathContext.DECIMAL32)
} }
sign == DIVIDE -> { DIVIDE -> {
val partial = BigDecimal(100).divide(secondValue, MathContext.DECIMAL32) val partial = 100 / secondValue
baseValue.multiply(partial, MathContext.DECIMAL32) baseValue * partial
} }
sign == PLUS -> { PLUS -> {
val partial = baseValue.divide(100.toBigDecimal().divide(secondValue, MathContext.DECIMAL32), MathContext.DECIMAL32) val partial = baseValue / (100 / secondValue)
baseValue.plus(partial) baseValue.plus(partial)
} }
sign == MINUS -> { MINUS -> {
val partial = baseValue.divide(100.toBigDecimal().divide(secondValue, MathContext.DECIMAL32), MathContext.DECIMAL32) val partial = baseValue / (100 / secondValue)
baseValue.minus(partial) baseValue.minus(partial)
} }
else -> { else -> {
baseValue.divide(BigDecimal(100)).multiply(secondValue) baseValue / (100 * secondValue)
} }
} }
} }

View File

@ -2,9 +2,8 @@ package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation import com.simplemobiletools.calculator.operation.base.Operation
import java.math.BigDecimal
class PlusOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation { class PlusOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult(): BigDecimal = baseValue.add(secondValue) override fun getResult() = baseValue + secondValue
} }

View File

@ -2,18 +2,17 @@ package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation import com.simplemobiletools.calculator.operation.base.Operation
import java.math.BigDecimal
import kotlin.math.pow import kotlin.math.pow
class PowerOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation { class PowerOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult(): BigDecimal { override fun getResult(): Double {
val result = baseValue.toDouble().pow(secondValue.toDouble()) val result = baseValue.pow(secondValue)
return if (java.lang.Double.isInfinite(result) || java.lang.Double.isNaN(result)) return if (java.lang.Double.isInfinite(result) || java.lang.Double.isNaN(result))
BigDecimal.ZERO 0.0
else { else {
BigDecimal(result) result
} }
} }
} }

View File

@ -2,10 +2,9 @@ package com.simplemobiletools.calculator.operation
import com.simplemobiletools.calculator.operation.base.BinaryOperation import com.simplemobiletools.calculator.operation.base.BinaryOperation
import com.simplemobiletools.calculator.operation.base.Operation import com.simplemobiletools.calculator.operation.base.Operation
import java.math.BigDecimal
import kotlin.math.sqrt import kotlin.math.sqrt
class RootOperation(baseValue: BigDecimal, secondValue: BigDecimal) : BinaryOperation(baseValue, secondValue), Operation { class RootOperation(baseValue: Double, secondValue: Double) : BinaryOperation(baseValue, secondValue), Operation {
override fun getResult() = BigDecimal(sqrt(secondValue.toDouble()).times(baseValue.toDouble())) override fun getResult() = sqrt(secondValue) * baseValue
} }

View File

@ -1,5 +1,3 @@
package com.simplemobiletools.calculator.operation.base package com.simplemobiletools.calculator.operation.base
import java.math.BigDecimal open class BinaryOperation protected constructor(protected val baseValue: Double, protected val secondValue: Double)
open class BinaryOperation protected constructor(protected val baseValue: BigDecimal, protected val secondValue: BigDecimal)

View File

@ -1,7 +1,5 @@
package com.simplemobiletools.calculator.operation.base package com.simplemobiletools.calculator.operation.base
import java.math.BigDecimal
interface Operation { interface Operation {
fun getResult(): BigDecimal fun getResult(): Double
} }

View File

@ -13,7 +13,6 @@ import org.robolectric.annotation.Config
import java.math.BigDecimal import java.math.BigDecimal
@RunWith(RobolectricTestRunner::class) @RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class, sdk = [21])
class MainActivityTest { class MainActivityTest {
private lateinit var activity: MainActivity private lateinit var activity: MainActivity
@ -164,7 +163,7 @@ class MainActivityTest {
} }
private fun setBigDecimal(d: BigDecimal) { private fun setBigDecimal(d: BigDecimal) {
activity.setValueBigDecimal(d)
} }
private fun handleOperation(operation: String) { private fun handleOperation(operation: String) {