Brings in the calculator's powers and remainders. docs/TODO.md keeps both Completed Steps lines, the API one first. Model: opus-5-5
This commit is contained in:
@@ -5,9 +5,10 @@ SimpleX Chat network: it accepts every contact request and answers
|
||||
arithmetic such as `2 + 2` with the result.
|
||||
|
||||
Send it `2 + 2` and it replies `4`; send `5 * 5/2` and it replies
|
||||
`12.5`. It understands decimal numbers, `+ - * /`, unary minus and
|
||||
parentheses, and computes exactly, so `0.1 + 0.2` is `0.3`. Anything
|
||||
else gets a short explanation instead of a result.
|
||||
`12.5`. It understands decimal numbers, `+ - * /`, powers written `2^10`
|
||||
or `2**10`, remainders written `7 % 3`, signs and parentheses, and
|
||||
computes exactly, so `0.1 + 0.2` is `0.3`. Anything else gets a short
|
||||
explanation instead of a result.
|
||||
|
||||
## Getting Started
|
||||
|
||||
@@ -232,16 +233,33 @@ container.
|
||||
- **Replies**: for each text message a contact sends in a direct chat,
|
||||
the bot sends back the result, as a reply quoting the message. Group
|
||||
messages, files and the bot's own messages are ignored.
|
||||
- **Arithmetic** (`internal/calc`): the text is parsed as a Go
|
||||
expression with `go/parser`, and only numbers, `+ - * /`, unary signs
|
||||
and parentheses are evaluated; anything else in the syntax tree is
|
||||
refused. `go/constant` computes with exact rationals. Numbers are read
|
||||
as decimal, so `010` is ten. Input over 256 bytes is refused, so a
|
||||
- **Arithmetic** (`internal/calc`): a small parser of its own reads
|
||||
numbers, `+ - * / % ^`, signs and parentheses, and refuses anything
|
||||
else. `go/constant` computes with exact rationals. `^`, also written
|
||||
`**`, is a power: it binds tighter than `*`, `/`, `%` and a sign on
|
||||
its left, and groups to the right, so `2^3^2` is `512`, `-2^2` is
|
||||
`-4`, `(-2)^2` is `4` and `2^-1` is `0.5`. `%` is the remainder and
|
||||
ranks with `*` and `/`; its result takes the sign of the divisor, as
|
||||
in Python, so `7 % 3` is `1`, `-7 % 3` is `2` and `7.5 % 2` is `1.5`.
|
||||
A power with a whole exponent is exact, so `0.1^2` is `0.01` and
|
||||
`2^-1400 * 2^1400` is `1`, unless `go/constant` could hold the result
|
||||
only rounded; that power, and one with a fractional exponent, is
|
||||
computed as a double, so `2^0.5` is `1.4142135623730951`. A negative
|
||||
number to a fractional power is refused, as having no real result.
|
||||
Numbers are read as decimal, so `010` is ten. Input over 256 bytes is
|
||||
refused, exact powers are capped, and every number is held as a
|
||||
fraction, whole numbers too, under the 4096-bit limit below, so a
|
||||
message cannot make the bot do unbounded work. Whole numbers below
|
||||
10<sup>21</sup> are written exactly; other results in the shortest
|
||||
form that reads back as the same double, in exponent notation from
|
||||
10<sup>21</sup> up and below 10<sup>-6</sup>. A result beyond the
|
||||
range of a double is refused as too large.
|
||||
10<sup>21</sup> up and below 10<sup>-6</sup>. Refused as too large or
|
||||
too small: any number whose numerator or denominator reaches 4096
|
||||
bits, wherever it appears, as `go/constant` rounds a fraction that
|
||||
grows that large (`1e-1300 + 1`); a power computed as a double whose
|
||||
base or result is outside the normal range of a double, about 2.2e-308
|
||||
to 1.8e308 in magnitude, where a double keeps all its digits
|
||||
(`1e-400^0.5`); and a result other than zero outside that range, as it
|
||||
is written through a double (`1e400`, `2^-1400`).
|
||||
- **Failure is an exit.** If the chat client exits or the connection to
|
||||
it drops, the bot exits with an error and the container's restart
|
||||
policy starts both again. `SIGTERM` stops the bot, which stops the
|
||||
|
||||
Reference in New Issue
Block a user