Calculator: exponentiation and modulo #3
Notifications
Due Date
No due date set.
Blocks
#2 Exponentiation, modulo, HTTP API for chats, per-chat webhooks
sneak/simplexcalc
Reference: sneak/simplexcalc#3
Reference in New Issue
Block a user
Part of #2. Independent of the API units.
What. The calculator accepts
^(also written**) for exponentiation and%for modulo, with the usual precedence, alongside what it does now.Decisions (reversible, taken by the repo-manager):
^and**are one operator. It binds tighter than*,/,%and than a sign on its left, and groups to the right:2^3^2is512,-2^2is-4,(-2)^2is4,2^-1is0.5.%sits with*and/, left to right. The result takes the sign of the divisor, as in Python and most calculators:7 % 3is1,-7 % 3is2,7 % -3is-2. It works on decimals, exactly:7.5 % 2is1.5,0.3 % 0.1is0.x % 0is division by zero.0.1^2is0.01,2^100 - 2^100 + 1is1. A fractional exponent is computed in float64:2^0.5is1.4142135623730951. A negative base with a fractional exponent has no real result and gets its own reply.0^0is1; zero to a negative power is division by zero.9^9^9^9^9,((9^999)^999)^999,1.0000001^99999) answer promptly.go/parsercannot express this (^is XOR at the precedence of+, and**does not parse), and neither the standard library nor the default package list parses exact arithmetic with a power operator. Sointernal/calcgets a small tokenizer and precedence-climbing parser of its own, still computing withgo/constant. Keep the decimal-only number rule (010is ten; no hex,_ori), the 256-byte limit, and refusal of everything else. Small and plain: a newcomer follows it in one reading.Done when:
internal/calctests cover precedence, grouping, signs, every example above, the refusals and the bounded-work cases; the existing tests still pass, changed only where the parser change must change them.bot.Reply's "I only understand arithmetic" sentence and the welcome text mention^and%; the no-real-result case has its own sentence.docs/TODO.mdCompleted Steps gets a line.make checkis green.Model: opus-5-5