Skip to content

Commit 18b8711

Browse files
add SGN, INT, DATA, RESTORE, ATAN, ATAN2, CEIL, ROUND, FMOD, ASN, ACS, EXP, LOG, DEG, RAD to bring REAL functions up to BASIC IV standard
1 parent 6b89aef commit 18b8711

9 files changed

Lines changed: 438 additions & 11 deletions

File tree

include/basic.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,8 @@ int64_t basic_cpuid(struct basic_ctx* ctx);
599599
int64_t basic_atoi(struct basic_ctx* ctx);
600600
int64_t basic_shl(struct basic_ctx* ctx);
601601
int64_t basic_shr(struct basic_ctx* ctx);
602+
int64_t basic_sgn(struct basic_ctx* ctx);
603+
int64_t basic_int(struct basic_ctx* ctx);
602604

603605
/*
604606
* Builtin string functions
@@ -649,6 +651,8 @@ void rmdir_statement(struct basic_ctx* ctx);
649651
void write_statement(struct basic_ctx* ctx);
650652
void chdir_statement(struct basic_ctx* ctx);
651653
char* basic_filetype(struct basic_ctx* ctx);
654+
void data_statement(struct basic_ctx* ctx);
655+
void restore_statement(struct basic_ctx* ctx);
652656

653657
/*
654658
* Builtin real (double) functions
@@ -658,7 +662,17 @@ void basic_cos(struct basic_ctx* ctx, double* res);
658662
void basic_tan(struct basic_ctx* ctx, double* res);
659663
void basic_pow(struct basic_ctx* ctx, double* res);
660664
void basic_sqrt(struct basic_ctx* ctx, double* res);
661-
665+
void basic_atan(struct basic_ctx* ctx, double* res);
666+
void basic_atan2(struct basic_ctx* ctx, double* res);
667+
void basic_ceil(struct basic_ctx* ctx, double* res);
668+
void basic_round(struct basic_ctx* ctx, double* res);
669+
void basic_fmod(struct basic_ctx* ctx, double* res);
670+
void basic_asn(struct basic_ctx* ctx, double* res);
671+
void basic_acs(struct basic_ctx* ctx, double* res);
672+
void basic_exp(struct basic_ctx* ctx, double* res);
673+
void basic_log(struct basic_ctx* ctx, double* res);
674+
void basic_deg(struct basic_ctx* ctx, double* res);
675+
void basic_rad(struct basic_ctx* ctx, double* res);
662676

663677
/*
664678
* Context control functions

include/basic_tokenizer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
T(POINT) \
113113
T(OPENOUT) \
114114
T(OPENUP) \
115+
T(DATA) \
116+
T(RESTORE) \
115117
T(WRITE) \
116118
T(MKDIR) \
117119
T(RMDIR) \

include/maths.h

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
#include "kernel.h"
99

10+
static inline double rr_nan(void) {
11+
union { uint64_t u; double d; } v = { 0x7ff8000000000000ULL }; // NaN
12+
return v.d;
13+
}
14+
1015
/**
1116
* @brief Raise base to the power of exp
1217
*
@@ -58,3 +63,111 @@ double tan(double rads);
5863
* @return double calculated square root of `x`
5964
*/
6065
double sqrt(double x);
66+
67+
/**
68+
* @brief Returns the absolute value of a floating point number.
69+
*
70+
* @param x The input value.
71+
* @return The absolute value of x (|x|).
72+
*/
73+
double fabs(double x);
74+
75+
/**
76+
* @brief Rounds a floating point number down to the nearest integer value.
77+
*
78+
* @param x The input value.
79+
* @return The largest integer value less than or equal to x, as a double.
80+
*/
81+
double floor(double x);
82+
83+
/**
84+
* @brief Computes the floating-point remainder of x / y.
85+
*
86+
* The result is x - n * y, where n is the integer quotient of x / y,
87+
* truncated toward zero.
88+
*
89+
* @param x Dividend.
90+
* @param y Divisor (must not be zero).
91+
* @return The remainder of x divided by y.
92+
*/
93+
double fmod(double x, double y);
94+
95+
/**
96+
* @brief Rounds a floating-point value up to the nearest integer.
97+
*
98+
* @param x Input value.
99+
* @return The smallest integer value not less than x.
100+
*/
101+
double ceil(double x);
102+
103+
/**
104+
* @brief Rounds a floating-point value to the nearest integer.
105+
*
106+
* Values halfway between two integers are rounded away from zero.
107+
*
108+
* @param x Input value.
109+
* @return The nearest integer as a double.
110+
*/
111+
double round(double x);
112+
113+
/**
114+
* @brief Computes the arc tangent of y/x using the signs of both arguments
115+
* to determine the correct quadrant of the result.
116+
*
117+
* @param y y-coordinate (numerator).
118+
* @param x x-coordinate (denominator).
119+
* @return The angle in radians between -PI and PI.
120+
*/
121+
double atan2(double y, double x);
122+
123+
/**
124+
* @brief Computes the arc tangent of x (inverse tangent).
125+
*
126+
* The result is in radians, between -PI/2 and PI/2.
127+
*
128+
* @param x The input value.
129+
* @return The arc tangent of x.
130+
*/
131+
double atan(double x);
132+
133+
/**
134+
* @brief Returns the arc sine (inverse sine) of x.
135+
* @param x Input value (must be between -1 and 1).
136+
* @return The arcsin of x in radians.
137+
*/
138+
double asin(double x);
139+
140+
/**
141+
* @brief Returns the arc cosine (inverse cosine) of x.
142+
* @param x Input value (must be between -1 and 1).
143+
* @return The arccos of x in radians.
144+
*/
145+
double acos(double x);
146+
147+
/**
148+
* @brief Calculates e raised to the power of x.
149+
* @param x Exponent.
150+
* @return e^x.
151+
*/
152+
double exp(double x);
153+
154+
/**
155+
* @brief Computes the natural logarithm (ln) of x.
156+
* @param x Input value (must be > 0).
157+
* @return ln(x).
158+
*/
159+
double log(double x);
160+
161+
/**
162+
* @brief Converts radians to degrees.
163+
* @param radians Value in radians.
164+
* @return Value in degrees.
165+
*/
166+
double deg(double radians);
167+
168+
/**
169+
* @brief Converts degrees to radians.
170+
* @param degrees Value in degrees.
171+
* @return Value in radians.
172+
*/
173+
double rad(double degrees);

src/basic/file_io.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ void read_statement(struct basic_ctx* ctx)
8383
tokenizer_error_print(ctx, "READ is a function");
8484
}
8585

86+
void data_statement(struct basic_ctx* ctx)
87+
{
88+
tokenizer_error_print(ctx, "DATA statements are not supported in Retro Rocket BASIC. Use files instead.");
89+
}
90+
91+
void restore_statement(struct basic_ctx* ctx)
92+
{
93+
tokenizer_error_print(ctx, "Nothing to RESTORE. DATA statements are not supported.");
94+
}
95+
8696
void close_statement(struct basic_ctx* ctx)
8797
{
8898
accept_or_return(CLOSE, ctx);

src/basic/function.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ struct basic_int_fn builtin_int[] =
4848
{ basic_get_text_max_y, "TERMHEIGHT" },
4949
{ basic_get_text_max_x, "TERMWIDTH" },
5050
{ basic_val, "VAL" },
51+
{ basic_sgn, "SGN" },
52+
{ basic_int, "INT" },
5153
{ NULL, NULL }
5254
};
5355

@@ -59,6 +61,18 @@ struct basic_double_fn builtin_double[] = {
5961
{ basic_sin, "SIN" },
6062
{ basic_tan, "TAN" },
6163
{ basic_sqrt, "SQRT" },
64+
{ basic_sqrt, "SQR" },
65+
{ basic_atan, "ATAN" },
66+
{ basic_atan2, "ATAN2" },
67+
{ basic_ceil, "CEIL" },
68+
{ basic_round, "ROUND" },
69+
{ basic_fmod, "FMOD" },
70+
{ basic_asn, "ASN" },
71+
{ basic_acs, "ACS" },
72+
{ basic_exp, "EXP" },
73+
{ basic_log, "LOG" },
74+
{ basic_deg, "DEG" },
75+
{ basic_rad, "RAD" },
6276
{ NULL, NULL },
6377
};
6478

src/basic/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,10 @@ void statement(struct basic_ctx* ctx)
685685
return openup_statement(ctx);
686686
case READ:
687687
return read_statement(ctx);
688+
case DATA:
689+
return data_statement(ctx);
690+
case RESTORE:
691+
return restore_statement(ctx);
688692
case CLOSE:
689693
return close_statement(ctx);
690694
case EOF:

0 commit comments

Comments
 (0)