Exponentiation and modulo in the calculator (closes #3)
check / check (push) Successful in 1m7s
check / check (push) Successful in 1m7s
The calculator now takes `^` (or `**`) for powers and `%` for modulo. Powers bind tighter than a sign on their left and group to the right, so `-2^2` is `-4` and `2^3^2` is `512`; `%` sits with `*` and `/` and takes the sign of the divisor. A small parser of our own replaces `go/parser`, which cannot express `^`; `go/constant` still computes. A whole-number exponent is exact; a fractional one is computed in float64. Every number is held as a fraction under 4096 bits, and a float64 result must be a normal double, so one message cannot stall the bot; anything else gets "That needs a number too large or too small for me." Disclosure: tiny values below about 2.2e-308, which `next` answered, are now refused. Model: opus-5-5
This commit was merged in pull request #11.
This commit is contained in:
+7
-5
@@ -22,7 +22,7 @@ import (
|
||||
const DisplayName = "calc"
|
||||
|
||||
// Welcome is sent to everyone whose contact request the bot accepts.
|
||||
const Welcome = "Send me arithmetic, such as 2 + 2 or 5 * 5/2, " +
|
||||
const Welcome = "Send me arithmetic, such as 2 + 2, 5 * 5/2, 2^10 or 7 % 3, " +
|
||||
"and I will reply with the result."
|
||||
|
||||
const (
|
||||
@@ -221,10 +221,12 @@ func Reply(text string) string {
|
||||
calc.MaxInputLength)
|
||||
case errors.Is(err, calc.ErrDivisionByZero):
|
||||
return "I cannot divide by zero."
|
||||
case errors.Is(err, calc.ErrTooLarge):
|
||||
return "The result is too large for me."
|
||||
case errors.Is(err, calc.ErrOutOfRange):
|
||||
return "That needs a number too large or too small for me."
|
||||
case errors.Is(err, calc.ErrNoRealResult):
|
||||
return "A negative number to a fractional power has no real result."
|
||||
default:
|
||||
return "I only understand arithmetic: numbers, + - * / and " +
|
||||
"parentheses, such as 5 * 5/2."
|
||||
return "I only understand arithmetic: numbers, + - * /, ^ for a power, " +
|
||||
"% for a remainder, and parentheses, such as 5 * 5/2 or 2^10."
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ func TestReply(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
"2 + 2": "4",
|
||||
"5 * 5/2": "12.5",
|
||||
"2^10": "1024",
|
||||
"7 % 3": "1",
|
||||
} {
|
||||
if got := bot.Reply(in); got != want {
|
||||
t.Errorf("Reply(%q) = %q, want %q", in, got, want)
|
||||
@@ -23,9 +25,11 @@ func TestReply(t *testing.T) {
|
||||
}
|
||||
|
||||
for in, want := range map[string]string{
|
||||
"hello": "I only understand arithmetic",
|
||||
"1 / 0": "I cannot divide by zero.",
|
||||
"1e400": "The result is too large for me.",
|
||||
"hello": "I only understand arithmetic",
|
||||
"1 / 0": "I cannot divide by zero.",
|
||||
"1e400": "That needs a number too large or too small for me.",
|
||||
"1e-1300": "That needs a number too large or too small for me.",
|
||||
"(-8)^0.5": "A negative number to a fractional power has no real",
|
||||
strings.Repeat("1+", calc.MaxInputLength) + "1": "That is too long for me",
|
||||
} {
|
||||
if got := bot.Reply(in); !strings.HasPrefix(got, want) {
|
||||
|
||||
Reference in New Issue
Block a user