sosauce / CuteCalc

CuteCalc is a simple,lightweight and open-source calculator app for Android.
GNU General Public License v3.0
121 stars 9 forks source link

Operator implementations #53

Closed notKamui closed 7 months ago

notKamui commented 8 months ago

Hey, Keval's creator here again. I suggest you use Keval's full ability to be customizable to only implement the operators you need instead of loading the full default resources.

This will also allow you to make the × symbol to the multiplication instead of *, which goes more in line with how you buttons work.

Moreover, this will allow you to add other operators that users might want. For example, I saw some people had issues with the modulo operator, thinking it is the percentage unary postfix operator. I don't agree that such an operator should be implement, I find it dumb to have a button to do basically /100. However, this could allow you to implement an unambiguous mod(n, c) function for example, and others as need, to make you calculator a scientific calculator in landscape mode.

Even if you don't plan on adding the features mentioned in the previous paragraph, you should still instanciate a custom Keval instance with only what you need.

I could make a PR for you if you don't have time.

realeatham commented 8 months ago

The multication symbol should still be * even if you do this

notKamui commented 7 months ago

@realeatham I vehemently disagree.

If the button is ×, then pressing it should show × and not *.

Keval was made to be this customizable, there is absolutely no valid reason to have a discrepancy between what's written and what is evaluated. This will also greatly simplify the code.

sosauce commented 7 months ago

I'd like to do that yeah ! How do I create for example a "+" ? (maybe I shouldnt ask that lol) I don't plan to add any more operators yes

sosauce commented 7 months ago

@realeatham

Changing * to x is simpler since i wont have to use a .replace() method

sosauce commented 7 months ago

I think its with this :

Keval.create { // builder instance includeDefault() // this function includes the built-in resources

binaryOperator { // this function adds a binary operator ; you can call it several times
    symbol = ';'
    precedence = 3
    isLeftAssociative = true
    implementation = { a, b -> a.pow(2) + b.pow(2) }
}

unaryOperator { // this function adds a unary operator ; you can call it several times
    symbol = '#'
    isPrefix = false
    implementation = { arg -> (1..arg.toInt()).fold(0.0) { acc, i -> acc + i } }
}

function { // this function adds a function ; you can call it several times
    name = "max"
    arity = 2
    implementation = { args -> max(args[0], args[1]) }
}

constant { // this function adds a constant ; you can call it several times
    name = "PHI"
    value = 1.618
}

}

But there are some Math things I dont understand 🥲

notKamui commented 7 months ago

@sosauce

what do you not understand ?

notKamui commented 7 months ago

I'll make a PR for you btw

notKamui commented 7 months ago

@sosauce I did the thing: #55

sosauce commented 7 months ago

Thank you so much ahhhh alos, the opt in are becaise my AS tweaks and forgets sometimes 😅