Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions app/src/main/java/net/youapps/calcyou/data/evaluator/Defaults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ import kotlin.math.atan
import kotlin.math.atan2
import kotlin.math.atanh
import kotlin.math.ceil
import kotlin.math.cos
import kotlin.math.cosh
import kotlin.math.exp
import kotlin.math.floor
import kotlin.math.hypot
import kotlin.math.ln
import kotlin.math.log
import kotlin.math.log10
import kotlin.math.log2
import kotlin.math.pow
import kotlin.math.round
import kotlin.math.sin
import kotlin.math.sinh
import kotlin.math.sqrt
import kotlin.math.tan
import kotlin.math.tanh
import kotlin.math.truncate

Expand Down Expand Up @@ -184,6 +188,63 @@ object Defaults {
"ATANH" to { args, mode ->
atanh(trigonometricModeToRadian(args.first(), mode))
},

// Additional trigonometric functions
"COT" to { args, mode ->
1.0 / tan(trigonometricModeToRadian(args.first(), mode))
},
"SEC" to { args, mode ->
1.0 / cos(trigonometricModeToRadian(args.first(), mode))
},
"CSC" to { args, mode ->
1.0 / sin(trigonometricModeToRadian(args.first(), mode))
},

// Additional mathematical functions
"CBRT" to { args, _ ->
args.first().pow(1.0 / 3.0)
},
"HYPOT" to fn@{ args, _ ->
if (args.size == 2) {
return@fn hypot(args[0], args[1])
}
throw InvalidParameterException("hypot requires two arguments: x, y")
},
"GCD" to fn@{ args, _ ->
if (args.size == 2) {
val a = args[0].toLong()
val b = args[1].toLong()
return@fn MathUtil.gcd(a, b).toDouble()
}
throw InvalidParameterException("gcd requires two arguments")
},
"LCM" to fn@{ args, _ ->
if (args.size == 2) {
val a = args[0].toLong()
val b = args[1].toLong()
return@fn MathUtil.lcm(a, b).toDouble()
}
throw InvalidParameterException("lcm requires two arguments")
},
"CLAMP" to fn@{ args, _ ->
if (args.size == 3) {
val value = args[0]
val min = args[1]
val max = args[2]
return@fn when {
value < min -> min
value > max -> max
else -> value
}
}
throw InvalidParameterException("clamp requires three arguments: value, min, max")
},
"RADIANS" to { args, _ ->
args.first() * Math.PI / 180.0
},
"DEGREES" to { args, _ ->
args.first() * 180.0 / Math.PI
},
)
}
}
23 changes: 22 additions & 1 deletion app/src/main/java/net/youapps/calcyou/data/evaluator/MathUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ object MathUtil {
* @param maxLen the maximum total length of the resulting string
* @param rounding the number of final digits to round
*/
fun doubleToString(x: Double, maxLen: Int = MAX_DIGITS, rounding: Int = ROUNDING_DIGITS): String {
fun doubleToString(
x: Double,
maxLen: Int = MAX_DIGITS,
rounding: Int = ROUNDING_DIGITS
): String {
return sizeTruncate(doubleToString(x, rounding), maxLen)
}

Expand Down Expand Up @@ -217,6 +221,23 @@ object MathUtil {
return (sqrt((2 * x + 1 / 3) * Math.PI) * x.pow(x) * exp((-x)))
}


// Helper functions for GCD and LCM
fun gcd(a: Long, b: Long): Long {
var x = abs(a)
var y = abs(b)
while (y != 0L) {
val temp = y
y = x % y
x = temp
}
return x
}

fun lcm(a: Long, b: Long): Long {
return if (a == 0L || b == 0L) 0L else abs(a * b) / gcd(a, b)
}

private const val MAX_DIGITS = 12
private const val ROUNDING_DIGITS = 5
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ data class Function(
val expression: String,
val color: Color,
val name: String,
val compiled: CompiledExpression
val compiled: CompiledExpression,
val isVisible: Boolean = true
) {
fun execute(variableX: Float, mode: TrigonometricMode, constants: List<Constant>): Float? {
val variables = listOf(
Expand Down
Loading