Added math constants for pi and e

This commit is contained in:
Add00
2023-07-31 13:56:06 -04:00
parent 2eaf7fc71a
commit 027d093e21
2 changed files with 54 additions and 11 deletions

View File

@@ -7,6 +7,9 @@
#include <time.h> #include <time.h>
#include <ctype.h> #include <ctype.h>
#define STD_MATH_PI 3.14159265358979323846f
#define STD_MATH_E 2.71828182845904523536f
static int nativeClock(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { static int nativeClock(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
//no arguments //no arguments
if (arguments->count != 0) { if (arguments->count != 0) {
@@ -608,9 +611,7 @@ static int nativeToRad(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments
// cast int to float to handle all types of numbers // cast int to float to handle all types of numbers
float degrees = TOY_IS_INTEGER(degreesLiteral)? TOY_AS_INTEGER(degreesLiteral) : TOY_AS_FLOAT(degreesLiteral); float degrees = TOY_IS_INTEGER(degreesLiteral)? TOY_AS_INTEGER(degreesLiteral) : TOY_AS_FLOAT(degreesLiteral);
const float PI = 3.14159265358979323846f; float result = degrees * (STD_MATH_PI / 180.0);
float result = degrees * (PI / 180.0);
//return the result //return the result
Toy_Literal resultLiteral = TOY_TO_FLOAT_LITERAL(result); Toy_Literal resultLiteral = TOY_TO_FLOAT_LITERAL(result);
@@ -648,9 +649,7 @@ static int nativeToDeg(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments
// cast int to float to handle all types of numbers // cast int to float to handle all types of numbers
float radians = TOY_IS_INTEGER(radiansLiteral)? TOY_AS_INTEGER(radiansLiteral) : TOY_AS_FLOAT(radiansLiteral); float radians = TOY_IS_INTEGER(radiansLiteral)? TOY_AS_INTEGER(radiansLiteral) : TOY_AS_FLOAT(radiansLiteral);
const float PI = 3.14159265358979323846f; float result = radians * (180.0 / STD_MATH_PI);
float result = radians * (180.0 / PI);
//return the result //return the result
Toy_Literal resultLiteral = TOY_TO_FLOAT_LITERAL(result); Toy_Literal resultLiteral = TOY_TO_FLOAT_LITERAL(result);
@@ -2352,6 +2351,16 @@ int Toy_hookStandard(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_L
{NULL, NULL} {NULL, NULL}
}; };
// math constants
Toy_Literal piKeyLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("PI"));
Toy_Literal piIdentifierLiteral = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString("PI"));
Toy_Literal piLiteral = TOY_TO_FLOAT_LITERAL(STD_MATH_PI);
Toy_Literal eKeyLiteral = TOY_TO_STRING_LITERAL(Toy_createRefString("E"));
Toy_Literal eIdentifierLiteral = TOY_TO_IDENTIFIER_LITERAL(Toy_createRefString("E"));
Toy_Literal eLiteral = TOY_TO_FLOAT_LITERAL(STD_MATH_PI);
//store the library in an aliased dictionary //store the library in an aliased dictionary
if (!TOY_IS_NULL(alias)) { if (!TOY_IS_NULL(alias)) {
//make sure the name isn't taken //make sure the name isn't taken
@@ -2376,6 +2385,9 @@ int Toy_hookStandard(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_L
Toy_freeLiteral(func); Toy_freeLiteral(func);
} }
Toy_setLiteralDictionary(dictionary, piKeyLiteral, piLiteral);
Toy_setLiteralDictionary(dictionary, eKeyLiteral, eLiteral);
//build the type //build the type
Toy_Literal type = TOY_TO_TYPE_LITERAL(TOY_LITERAL_DICTIONARY, true); Toy_Literal type = TOY_TO_TYPE_LITERAL(TOY_LITERAL_DICTIONARY, true);
Toy_Literal strType = TOY_TO_TYPE_LITERAL(TOY_LITERAL_STRING, true); Toy_Literal strType = TOY_TO_TYPE_LITERAL(TOY_LITERAL_STRING, true);
@@ -2391,6 +2403,13 @@ int Toy_hookStandard(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_L
//cleanup //cleanup
Toy_freeLiteral(dict); Toy_freeLiteral(dict);
Toy_freeLiteral(type); Toy_freeLiteral(type);
Toy_freeLiteral(piKeyLiteral);
Toy_freeLiteral(piIdentifierLiteral);
Toy_freeLiteral(piLiteral);
Toy_freeLiteral(eKeyLiteral);
Toy_freeLiteral(eIdentifierLiteral);
Toy_freeLiteral(eLiteral);
return 0; return 0;
} }
@@ -2399,5 +2418,34 @@ int Toy_hookStandard(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_L
Toy_injectNativeFn(interpreter, natives[i].name, natives[i].fn); Toy_injectNativeFn(interpreter, natives[i].name, natives[i].fn);
} }
if (Toy_isDeclaredScopeVariable(interpreter->scope, piKeyLiteral)) {
interpreter->errorOutput("Can't override an existing variable\n");
// cleanup
Toy_freeLiteral(alias);
Toy_freeLiteral(piKeyLiteral);
return -1;
}
Toy_Literal floatType = TOY_TO_TYPE_LITERAL(TOY_LITERAL_FLOAT, false);
// pi
Toy_declareScopeVariable(interpreter->scope, piIdentifierLiteral, floatType);
Toy_setScopeVariable(interpreter->scope, piIdentifierLiteral, piLiteral, false);
// e
Toy_declareScopeVariable(interpreter->scope, eIdentifierLiteral, floatType);
Toy_setScopeVariable(interpreter->scope, eIdentifierLiteral, eLiteral, false);
// cleanup
Toy_freeLiteral(floatType);
Toy_freeLiteral(piKeyLiteral);
Toy_freeLiteral(piIdentifierLiteral);
Toy_freeLiteral(piLiteral);
Toy_freeLiteral(eKeyLiteral);
Toy_freeLiteral(eIdentifierLiteral);
Toy_freeLiteral(eLiteral);
return 0; return 0;
} }

View File

@@ -157,11 +157,8 @@ import standard;
assert typeof lerp(0, 10, 0) == float, "typeof lerp result failed"; assert typeof lerp(0, 10, 0) == float, "typeof lerp result failed";
} }
// test toRad // test toRad
{ {
var PI: float const = 3.14159265358979323846;
assert toRad(0) == 0, "toRad 0° failed"; assert toRad(0) == 0, "toRad 0° failed";
assert toRad(180) == PI, "toRad 180° failed"; assert toRad(180) == PI, "toRad 180° failed";
assert toRad(360) == 2 * PI, "toRad 360° failed"; assert toRad(360) == 2 * PI, "toRad 360° failed";
@@ -169,8 +166,6 @@ import standard;
// test toDeg // test toDeg
{ {
var PI: float const = 3.14159265358979323846;
assert toDeg(0) == 0, "toDeg 0 failed"; assert toDeg(0) == 0, "toDeg 0 failed";
assert toDeg(PI) == 180, "toDeg π failed"; assert toDeg(PI) == 180, "toDeg π failed";
assert toDeg(2 * PI) == 360, "toDeg 2π failed"; assert toDeg(2 * PI) == 360, "toDeg 2π failed";