Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
math.cpp
Go to the documentation of this file.
1#include "StdLib.hpp"
2#include <phsint.hpp>
3
4namespace Phasor
5{
6
23
24f64 StdLib::math_sqrt(const std::vector<Value> &args, VM *)
25{
26 checkArgCount(args, 1, "math_sqrt");
27 return asm_sqrt(args[0].asFloat());
28}
29
30f64 StdLib::math_pow(const std::vector<Value> &args, VM *)
31{
32 checkArgCount(args, 2, "math_pow");
33 f64 base = args[0].asFloat();
34 f64 expv = args[1].asFloat();
35 return asm_pow(base, expv);
36}
37
38Value StdLib::math_abs(const std::vector<Value> &args, VM *)
39{
41 checkArgCount(args, 1, "math_abs");
42 if (args[0].isInt())
43 return std::abs(args[0].asInt());
44 return std::abs(args[0].asFloat());
45}
46
47f64 StdLib::math_floor(const std::vector<Value> &args, VM *)
48{
50 checkArgCount(args, 1, "math_floor");
51 return std::floor(args[0].asFloat());
52}
53
54f64 StdLib::math_ceil(const std::vector<Value> &args, VM *)
55{
57 checkArgCount(args, 1, "math_ceil");
58 return std::ceil(args[0].asFloat());
59}
60
61f64 StdLib::math_round(const std::vector<Value> &args, VM *)
62{
64 checkArgCount(args, 1, "math_round");
65 return std::round(args[0].asFloat());
66}
67
68Value StdLib::math_min(const std::vector<Value> &args, VM *)
69{
70 checkArgCount(args, 2, "math_min");
71 const Value &a = args[0];
72 const Value &b = args[1];
73 if (a.isInt() && b.isInt())
74 {
75 i64 ai = a.asInt();
76 i64 bi = b.asInt();
77 return Value(asm_iless_than(ai, bi) ? ai : bi);
78 }
79 if (a.isNumber() && b.isNumber())
80 {
81 f64 af = a.asFloat();
82 f64 bf = b.asFloat();
83 return Value(asm_flless_than(af, bf) ? af : bf);
84 }
85 return a < b ? a : b;
86}
87
88Value StdLib::math_max(const std::vector<Value> &args, VM *)
89{
90 checkArgCount(args, 2, "math_max");
91 const Value &a = args[0];
92 const Value &b = args[1];
93 if (a.isInt() && b.isInt())
94 {
95 i64 ai = a.asInt();
96 i64 bi = b.asInt();
97 return Value(ai > bi ? ai : bi);
98 }
99 if (a.isNumber() && b.isNumber())
100 {
101 f64 af = a.asFloat();
102 f64 bf = b.asFloat();
103 return Value(af > bf ? af : bf);
104 }
105 return a > b ? a : b;
106}
107
108f64 StdLib::math_log(const std::vector<Value> &args, VM *)
109{
110 checkArgCount(args, 1, "math_log");
111 return asm_log(args[0].asFloat());
112}
113
114f64 StdLib::math_exp(const std::vector<Value> &args, VM *)
115{
116 checkArgCount(args, 1, "math_exp");
117 return asm_exp(args[0].asFloat());
118}
119
120f64 StdLib::math_sin(const std::vector<Value> &args, VM *)
121{
122 checkArgCount(args, 1, "math_sin");
123 return asm_sin(args[0].asFloat());
124}
125
126f64 StdLib::math_cos(const std::vector<Value> &args, VM *)
127{
128 checkArgCount(args, 1, "math_cos");
129 return asm_cos(args[0].asFloat());
130}
131
132f64 StdLib::math_tan(const std::vector<Value> &args, VM *)
133{
134 checkArgCount(args, 1, "math_tan");
135 return asm_tan(args[0].asFloat());
136}
137} // namespace Phasor
double asm_tan(double a)
Native tangent.
Definition arithmetic.c:93
double asm_log(double a)
Native natural logarithm.
Definition arithmetic.c:73
double asm_sqrt(double a)
Native square root.
Definition arithmetic.c:63
double asm_sin(double a)
Native sine.
Definition arithmetic.c:83
double asm_exp(double a)
Native exponential.
Definition arithmetic.c:78
double asm_cos(double a)
Native cosine.
Definition arithmetic.c:88
double asm_pow(double a, double b)
Native power.
Definition arithmetic.c:68
static Value math_max(const std::vector< Value > &args, VM *vm)
Maximum.
Definition math.cpp:88
static f64 math_exp(const std::vector< Value > &args, VM *vm)
Exponential.
Definition math.cpp:114
static f64 math_cos(const std::vector< Value > &args, VM *vm)
Cosine.
Definition math.cpp:126
static f64 math_sin(const std::vector< Value > &args, VM *vm)
Sine.
Definition math.cpp:120
static f64 math_log(const std::vector< Value > &args, VM *vm)
Natural logarithm.
Definition math.cpp:108
static f64 math_sqrt(const std::vector< Value > &args, VM *vm)
Square root.
Definition math.cpp:24
static f64 math_pow(const std::vector< Value > &args, VM *vm)
Power.
Definition math.cpp:30
static void checkArgCount(const std::vector< Value > &args, size_t minimumArguments, const std::string &name, bool allowMoreArguments=false)
Definition StdLib.cpp:44
static void registerMathFunctions(VM *vm)
Definition math.cpp:7
static f64 math_round(const std::vector< Value > &args, VM *vm)
Round.
Definition math.cpp:61
static Value math_abs(const std::vector< Value > &args, VM *vm)
Absolute value.
Definition math.cpp:38
static f64 math_tan(const std::vector< Value > &args, VM *vm)
Tangent.
Definition math.cpp:132
static f64 math_ceil(const std::vector< Value > &args, VM *vm)
Ceiling.
Definition math.cpp:54
static f64 math_floor(const std::vector< Value > &args, VM *vm)
Floor.
Definition math.cpp:47
static Value math_min(const std::vector< Value > &args, VM *vm)
Minimum.
Definition math.cpp:68
Virtual Machine.
Definition VM.hpp:36
void registerNativeFunction(const std::string &name, NativeFunction fn)
Register a native function.
Definition Native.cpp:5
A value in the Phasor VM.
Definition Value.hpp:59
bool isInt() const noexcept
Definition Value.hpp:148
f64 asFloat() const noexcept
Get the value as a f64.
Definition Value.hpp:177
i64 asInt() const noexcept
Get the value as an integer.
Definition Value.hpp:164
bool isNumber() const noexcept
Definition Value.hpp:151
int64_t asm_iless_than(int64_t a, int64_t b)
Native Less than comparison.
Definition logical.c:35
int64_t asm_flless_than(double a, double b)
Definition logical.c:87
The Phasor Programming Language and Runtime.
Definition AST.hpp:13
int64_t i64
Definition phsint.hpp:16
double f64
Definition phsint.hpp:7