BASIC: scientific functions and OPTION ANGLE DEGREES

ASIN, ACOS, ANGLE, SEC, CSC, COT; SINH, COSH, TANH and their inverses;
LOG10, LOG2; IP, FP, CEIL, ROUND, TRUNCATE; MOD; MIN and MAX of any number of
values; FACT; PI, DEG, RAD; OPTION ANGLE DEGREES|RADIANS. Out-of-domain values
give ILLEGAL FUNCTION CALL. basic/test-math.py checks each against Python's
math (within 3 ulp); the manual and README cover them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-12-01 16:39:29 -08:00
1 parent 79aef8ed38
commit 25c8286cf2
8 files changed
+2985 -2375

No files matched your search

+72 -4
View File
@@ -419,6 +419,16 @@ in its formula are the program's ordinary variables.
the program. A `DATA` statement takes up the rest of its line. `RESTORE` starts
again from the first item.
### OPTION ANGLE
```
OPTION ANGLE DEGREES
OPTION ANGLE RADIANS
```
Chooses the units of the trigonometric functions (section 6). Radians are
the default, and `RUN` goes back to them.
### END, STOP, REM
- `END` stops the program quietly.
@@ -460,16 +470,74 @@ and `INK` are described in sections 7 and 8.
| `ABS(X)` | Absolute value |
| `SGN(X)` | -1, 0 or 1 |
| `INT(X)` | Largest whole number not above *X*: `INT(-2.5)` is -3 |
| `IP(X)` | The whole-number part, towards 0: `IP(-2.5)` is -2 |
| `FP(X)` | The fraction part, `X - IP(X)`: `FP(-2.5)` is -.5 |
| `CEIL(X)` | Smallest whole number not below *X*: `CEIL(2.1)` is 3 |
| `ROUND(X [, N])` | *X* rounded to *N* decimal places (0 if left out); a negative *N* rounds to tens, hundreds…: `ROUND(2.567, 2)` is 2.57, `ROUND(1250, -2)` is 1300. Halves go up, as `INT(X + .5)` does: `ROUND(-2.5)` is -2 |
| `TRUNCATE(X, N)` | *X* cut short after *N* decimal places: `TRUNCATE(-2.567, 1)` is -2.5 |
| `MOD(A, B)` | Remainder of *A* divided by *B*, with the sign of *B*: `A - B * INT(A / B)`, but exact. `MOD(-7, 3)` is 2 |
| `MIN(A, B, …)`, `MAX(A, B, …)` | Smallest or largest of any number of values |
| `FACT(N)` | *N* factorial, for whole numbers 0 to 34 |
| `SQR(X)` | Square root |
| `EXP(X)` | e to the power *X* |
| `LOG(X)` | Natural logarithm |
| `SIN(X)`, `COS(X)`, `TAN(X)` | Trigonometry, in radians |
| `ATN(X)` | Arctangent, in radians; `4*ATN(1)` is pi |
| `LOG10(X)`, `LOG2(X)` | Logarithm to base 10 or 2 (`LOG2` is exact for powers of 2); powers of 10 are `10 ^ X` |
| `PI` | 3.14159265 (no brackets) |
| `RND` or `RND(X)` | Random number from 0 up to (not including) 1; *X* is ignored |
| `FNx(X)` | A function made with `DEF` |
A whole number from 1 to 6: `1 + INT(RND * 6)`.
### Trigonometry
| Function | Result |
|---|---|
| `SIN(X)`, `COS(X)`, `TAN(X)` | Sine, cosine, tangent |
| `SEC(X)`, `CSC(X)`, `COT(X)` | Secant, cosecant, cotangent: `1/COS(X)`, `1/SIN(X)`, `1/TAN(X)` |
| `ATN(X)` | Arctangent, from -90° to 90° (-π/2 to π/2) |
| `ASIN(X)`, `ACOS(X)` | Arcsine (-90° to 90°) and arccosine (0° to 180°), for *X* from -1 to 1 |
| `ANGLE(X, Y)` | The direction of the point (*X*, *Y*) from the origin, from -180° to 180°, measured anticlockwise from the X axis; it gets the quarter right, as `ATN(Y / X)` can't (other languages call it `ATAN2(Y, X)`) |
| `DEG(X)`, `RAD(X)` | Radians to degrees, degrees to radians |
| `SINH(X)`, `COSH(X)`, `TANH(X)` | Hyperbolic sine, cosine, tangent |
| `ASINH(X)`, `ACOSH(X)`, `ATANH(X)` | Their inverses: `ACOSH` for *X* ≥ 1, `ATANH` for *X* between -1 and 1 |
Angles are in radians until a program says `OPTION ANGLE DEGREES`; from
then on the trigonometric functions take degrees and the inverse ones
(`ATN`, `ASIN`, `ACOS`, `ANGLE`) give degrees. `OPTION ANGLE RADIANS` goes
back, and so does `RUN`. In degrees, `SIN`, `COS` and the rest are exact at
multiples of 90, so `SIN(180)` is 0, not a tiny remainder.
A value outside a function's range, such as `ASIN(2)`, `LOG10(0)`,
`ACOSH(0)`, `FACT(2.5)`, `ANGLE(0, 0)` or, in degrees, `TAN(90)`, stops
the program with `ILLEGAL FUNCTION CALL`; `MOD(A, 0)` is
`DIVISION BY ZERO`. Every function is correct to within 3 units in the last
place of single precision (about 1 part in 3 million), checked against a
PC's double-precision maths by `basic/test-math.py`.
```
10 OPTION ANGLE DEGREES
20 FOR A = 0 TO 75 STEP 15
30 PRINT A, ROUND(SIN(A), 4), ROUND(COS(A), 4), ROUND(TAN(A), 4)
40 NEXT A
50 PRINT "BEARING OF (3, 4):"; ROUND(ANGLE(3, 4), 2); "DEGREES"
60 PRINT "ITS HYPOTENUSE:"; SQR(3^2 + 4^2)
70 PRINT "ACOS(-1) ="; ACOS(-1); " 20! ="; FACT(20)
```
prints
```
0 0 1 0
15 .2588 .9659 .2679
30 .5 .866 .5774
45 .7071 .7071 1
60 .866 .5 1.7321
75 .9659 .2588 3.7321
BEARING OF (3, 4): 53.13 DEGREES
ITS HYPOTENUSE: 5
ACOS(-1) = 180 20! = 2.4329E+18
```
### Strings
| Function | Result |
@@ -1303,11 +1371,11 @@ An error stops the program and reports the line, for example
| `NEXT WITHOUT FOR` | `NEXT` with no loop open (or the wrong variable) |
| `OUT OF DATA` | `READ` after the last `DATA` item |
| `SUBSCRIPT OUT OF RANGE` | An array subscript below 0 or beyond its `DIM` |
| `DIVISION BY ZERO` | Dividing by 0 |
| `DIVISION BY ZERO` | Dividing by 0, or `MOD(A, 0)` |
| `OUT OF MEMORY` | The program and arrays don't fit |
| `TYPE MISMATCH` | A string where a number belongs, or the other way round |
| `STRING TOO LONG` | A string over 79 characters, or a program line too long to store (also from `RENUM`) |
| `ILLEGAL FUNCTION CALL` | A bad argument: `SQR(-1)`, `LOG(0)`, a colour over 15, `AND` on a number beyond ±32767, a file number other than 1–3, an impossible `DATE`, a `RENUM` past 65535 or with a step of 0 |
| `ILLEGAL FUNCTION CALL` | A bad argument: `SQR(-1)`, `LOG(0)`, `ASIN(2)`, `FACT(-1)` (section 6), a colour over 15, `AND` on a number beyond ±32767, a file number other than 1–3, an impossible `DATE`, a `RENUM` past 65535 or with a step of 0 |
| `UNDEFINED FUNCTION` | `FNx` used before its `DEF` |
| `FOR NESTED TOO DEEPLY` | More than 16 `FOR` loops open |
| `DO NESTED TOO DEEPLY` | More than 16 `DO` loops open |