Skip to content

Commit 881b518

Browse files
authored
Merge pull request #84 from SuhasDissa/graphing
Graphing Improvements
2 parents 3a0dab7 + 85a0cec commit 881b518

File tree

9 files changed

+528
-20
lines changed

9 files changed

+528
-20
lines changed

app/src/main/java/net/youapps/calcyou/data/evaluator/Defaults.kt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ import kotlin.math.atan
1010
import kotlin.math.atan2
1111
import kotlin.math.atanh
1212
import kotlin.math.ceil
13+
import kotlin.math.cos
1314
import kotlin.math.cosh
1415
import kotlin.math.exp
1516
import kotlin.math.floor
17+
import kotlin.math.hypot
1618
import kotlin.math.ln
1719
import kotlin.math.log
1820
import kotlin.math.log10
1921
import kotlin.math.log2
2022
import kotlin.math.pow
2123
import kotlin.math.round
24+
import kotlin.math.sin
2225
import kotlin.math.sinh
2326
import kotlin.math.sqrt
27+
import kotlin.math.tan
2428
import kotlin.math.tanh
2529
import kotlin.math.truncate
2630

@@ -184,6 +188,63 @@ object Defaults {
184188
"ATANH" to { args, mode ->
185189
atanh(trigonometricModeToRadian(args.first(), mode))
186190
},
191+
192+
// Additional trigonometric functions
193+
"COT" to { args, mode ->
194+
1.0 / tan(trigonometricModeToRadian(args.first(), mode))
195+
},
196+
"SEC" to { args, mode ->
197+
1.0 / cos(trigonometricModeToRadian(args.first(), mode))
198+
},
199+
"CSC" to { args, mode ->
200+
1.0 / sin(trigonometricModeToRadian(args.first(), mode))
201+
},
202+
203+
// Additional mathematical functions
204+
"CBRT" to { args, _ ->
205+
args.first().pow(1.0 / 3.0)
206+
},
207+
"HYPOT" to fn@{ args, _ ->
208+
if (args.size == 2) {
209+
return@fn hypot(args[0], args[1])
210+
}
211+
throw InvalidParameterException("hypot requires two arguments: x, y")
212+
},
213+
"GCD" to fn@{ args, _ ->
214+
if (args.size == 2) {
215+
val a = args[0].toLong()
216+
val b = args[1].toLong()
217+
return@fn MathUtil.gcd(a, b).toDouble()
218+
}
219+
throw InvalidParameterException("gcd requires two arguments")
220+
},
221+
"LCM" to fn@{ args, _ ->
222+
if (args.size == 2) {
223+
val a = args[0].toLong()
224+
val b = args[1].toLong()
225+
return@fn MathUtil.lcm(a, b).toDouble()
226+
}
227+
throw InvalidParameterException("lcm requires two arguments")
228+
},
229+
"CLAMP" to fn@{ args, _ ->
230+
if (args.size == 3) {
231+
val value = args[0]
232+
val min = args[1]
233+
val max = args[2]
234+
return@fn when {
235+
value < min -> min
236+
value > max -> max
237+
else -> value
238+
}
239+
}
240+
throw InvalidParameterException("clamp requires three arguments: value, min, max")
241+
},
242+
"RADIANS" to { args, _ ->
243+
args.first() * Math.PI / 180.0
244+
},
245+
"DEGREES" to { args, _ ->
246+
args.first() * 180.0 / Math.PI
247+
},
187248
)
188249
}
189250
}

app/src/main/java/net/youapps/calcyou/data/evaluator/MathUtil.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,11 @@ object MathUtil {
175175
* @param maxLen the maximum total length of the resulting string
176176
* @param rounding the number of final digits to round
177177
*/
178-
fun doubleToString(x: Double, maxLen: Int = MAX_DIGITS, rounding: Int = ROUNDING_DIGITS): String {
178+
fun doubleToString(
179+
x: Double,
180+
maxLen: Int = MAX_DIGITS,
181+
rounding: Int = ROUNDING_DIGITS
182+
): String {
179183
return sizeTruncate(doubleToString(x, rounding), maxLen)
180184
}
181185

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

224+
225+
// Helper functions for GCD and LCM
226+
fun gcd(a: Long, b: Long): Long {
227+
var x = abs(a)
228+
var y = abs(b)
229+
while (y != 0L) {
230+
val temp = y
231+
y = x % y
232+
x = temp
233+
}
234+
return x
235+
}
236+
237+
fun lcm(a: Long, b: Long): Long {
238+
return if (a == 0L || b == 0L) 0L else abs(a * b) / gcd(a, b)
239+
}
240+
220241
private const val MAX_DIGITS = 12
221242
private const val ROUNDING_DIGITS = 5
222243
}

app/src/main/java/net/youapps/calcyou/data/graphing/Function.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ data class Function(
1111
val expression: String,
1212
val color: Color,
1313
val name: String,
14-
val compiled: CompiledExpression
14+
val compiled: CompiledExpression,
15+
val isVisible: Boolean = true
1516
) {
1617
fun execute(variableX: Float, mode: TrigonometricMode, constants: List<Constant>): Float? {
1718
val variables = listOf(

0 commit comments

Comments
 (0)