• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 14823373998

04 May 2025 05:03PM UTC coverage: 86.442% (+0.03%) from 86.409%
14823373998

push

github

SuperFola
fix: change the color of the function name inside runtime typechecking errors from blue to cyan to be easier to read inside dark terminals

0 of 1 new or added line in 1 file covered. (0.0%)

195 existing lines in 22 files now uncovered.

6835 of 7907 relevant lines covered (86.44%)

79668.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.43
/src/arkreactor/Builtins/Mathematics.cpp
1
#define _USE_MATH_DEFINES
2
#include <cmath>
3
#include <fmt/core.h>
4
#include <random>
5

6
#include <Ark/Builtins/Builtins.hpp>
7

8
#include <Ark/TypeChecker.hpp>
9
#include <Ark/VM/VM.hpp>
10

11
namespace Ark::internal::Builtins::Mathematics
12
{
13
    /**
14
     * @name math:exp
15
     * @brief Calculate e^number
16
     * @param value the Number
17
     * =begin
18
     * (math:exp 1)  # 2.7182...
19
     * =end
20
     * @author https://github.com/SuperFola
21
     */
22
    // cppcheck-suppress constParameterReference
23
    Value exponential(std::vector<Value>& n, VM* vm [[maybe_unused]])
8✔
24
    {
8✔
25
        if (!types::check(n, ValueType::Number))
8✔
26
            throw types::TypeCheckingError(
2✔
27
                "math:exp",
1✔
28
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
29
                n);
1✔
30

31
        return Value(std::exp(n[0].number()));
7✔
32
    }
1✔
33

34
    /**
35
     * @name math:ln
36
     * @brief Calculate the logarithm of a number
37
     * @param value the Number
38
     * =begin
39
     * (math:ln 1)  # 0
40
     * =end
41
     * @author https://github.com/SuperFola
42
     */
43
    // cppcheck-suppress constParameterReference
44
    Value logarithm(std::vector<Value>& n, VM* vm [[maybe_unused]])
19✔
45
    {
19✔
46
        if (!types::check(n, ValueType::Number))
19✔
47
            throw types::TypeCheckingError(
3✔
48
                "math:ln",
1✔
49
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
50
                n);
1✔
51

52
        if (n[0].number() <= 0.0)
18✔
53
            throw std::runtime_error(fmt::format("math:ln: value {} must be greater than 0", n[0].number()));
1✔
54

55
        return Value(std::log(n[0].number()));
17✔
56
    }
2✔
57

58
    /**
59
     * @name math:ceil
60
     * @brief Get the smallest possible integer greater than the number
61
     * @param value the Number
62
     * =begin
63
     * (math:ceil 0.2)  # 1
64
     * =end
65
     * @author https://github.com/SuperFola
66
     */
67
    // cppcheck-suppress constParameterReference
68
    Value ceil_(std::vector<Value>& n, VM* vm [[maybe_unused]])
4✔
69
    {
4✔
70
        if (!types::check(n, ValueType::Number))
4✔
71
            throw types::TypeCheckingError(
2✔
72
                "math:ceil",
1✔
73
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
74
                n);
1✔
75

76
        return Value(std::ceil(n[0].number()));
3✔
77
    }
1✔
78

79
    /**
80
     * @name math:floor
81
     * @brief Get the smallest possible integer equal to the given number
82
     * @param value the Number
83
     * =begin
84
     * (math:floor 1.7)  # 1
85
     * =end
86
     * @author https://github.com/SuperFola
87
     */
88
    // cppcheck-suppress constParameterReference
89
    Value floor_(std::vector<Value>& n, VM* vm [[maybe_unused]])
15✔
90
    {
15✔
91
        if (!types::check(n, ValueType::Number))
15✔
92
            throw types::TypeCheckingError(
2✔
93
                "math:floor",
1✔
94
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
95
                n);
1✔
96

97
        return Value(std::floor(n[0].number()));
14✔
98
    }
1✔
99

100
    /**
101
     * @name math:round
102
     * @brief Get the smallest possible integer equal to or greater than the given number
103
     * @param value the Number
104
     * =begin
105
     * (math:round 0.2)  # 0
106
     * (math:round 0.6)  # 1
107
     * =end
108
     * @author https://github.com/SuperFola
109
     */
110
    // cppcheck-suppress constParameterReference
111
    Value round_(std::vector<Value>& n, VM* vm [[maybe_unused]])
6✔
112
    {
6✔
113
        if (!types::check(n, ValueType::Number))
6✔
114
            throw types::TypeCheckingError(
2✔
115
                "math:round",
1✔
116
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
117
                n);
1✔
118

119
        return Value(std::round(n[0].number()));
5✔
120
    }
1✔
121

122
    /**
123
     * @name math:NaN?
124
     * @brief Check if a Number is NaN
125
     * @param value the Number
126
     * =begin
127
     * (math:NaN? 2)  # false
128
     * (math:NaN? nan)  # true
129
     * =end
130
     * @author https://github.com/SuperFola
131
     */
132
    // cppcheck-suppress constParameterReference
133
    Value isnan_(std::vector<Value>& n, VM* vm [[maybe_unused]])
2✔
134
    {
2✔
135
        if (n[0].valueType() != ValueType::Number)
2✔
UNCOV
136
            return falseSym;
×
137

138
        return std::isnan(n[0].number()) ? trueSym : falseSym;
2✔
139
    }
2✔
140

141
    /**
142
     * @name math:Inf?
143
     * @brief Check if a Number if Inf
144
     * @param value the Number
145
     * =begin
146
     * (math:Inf? 1)  # false
147
     * (math:Inf? nan)  # false
148
     * =end
149
     * @author https://github.com/SuperFola
150
     */
151
    // cppcheck-suppress constParameterReference
152
    Value isinf_(std::vector<Value>& n, VM* vm [[maybe_unused]])
5✔
153
    {
5✔
154
        if (n[0].valueType() != ValueType::Number)
5✔
UNCOV
155
            return falseSym;
×
156

157
        return std::isinf(n[0].number()) ? trueSym : falseSym;
5✔
158
    }
5✔
159

160
    /**
161
     * @name math:cos
162
     * @brief Calculate the cosinus of a number
163
     * @param value the Number (radians)
164
     * =begin
165
     * (math:cos 0)  # 1
166
     * (math:cos math:pi)  # -1
167
     * =end
168
     * @author https://github.com/SuperFola
169
     */
170
    // cppcheck-suppress constParameterReference
171
    Value cos_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
172
    {
1✔
173
        if (!types::check(n, ValueType::Number))
1✔
174
            throw types::TypeCheckingError(
2✔
175
                "math:cos",
1✔
176
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
177
                n);
1✔
178

UNCOV
179
        return Value(std::cos(n[0].number()));
×
180
    }
1✔
181

182
    /**
183
     * @name math:sin
184
     * @brief Calculate the sinus of a number
185
     * @param value the Number (radians)
186
     * =begin
187
     * (math:sin 0)  # 0
188
     * (math:cos (/ math:pi 2))  # 1
189
     * =end
190
     * @author https://github.com/SuperFola
191
     */
192
    // cppcheck-suppress constParameterReference
193
    Value sin_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
194
    {
1✔
195
        if (!types::check(n, ValueType::Number))
1✔
196
            throw types::TypeCheckingError(
2✔
197
                "math:sin",
1✔
198
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
199
                n);
1✔
200

UNCOV
201
        return Value(std::sin(n[0].number()));
×
202
    }
1✔
203

204
    /**
205
     * @name math:tan
206
     * @brief Calculate the tangent of a number
207
     * @param value the Number (radians)
208
     * =begin
209
     * (math:tan 0)  # 0
210
     * (math:cos (/ math:pi 4))  # 1
211
     * =end
212
     * @author https://github.com/SuperFola
213
     */
214
    // cppcheck-suppress constParameterReference
215
    Value tan_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
216
    {
1✔
217
        if (!types::check(n, ValueType::Number))
1✔
218
            throw types::TypeCheckingError(
2✔
219
                "math:tan",
1✔
220
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
221
                n);
1✔
222

UNCOV
223
        return Value(std::tan(n[0].number()));
×
224
    }
1✔
225

226
    /**
227
     * @name math:arccos
228
     * @brief Calculate the arc cosinus of a number
229
     * @param value the Number
230
     * =begin
231
     * (math:arccos 1)  # 0
232
     * =end
233
     * @author https://github.com/SuperFola
234
     */
235
    // cppcheck-suppress constParameterReference
236
    Value acos_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
237
    {
1✔
238
        if (!types::check(n, ValueType::Number))
1✔
239
            throw types::TypeCheckingError(
2✔
240
                "math:arccos",
1✔
241
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
242
                n);
1✔
243

UNCOV
244
        return Value(std::acos(n[0].number()));
×
245
    }
1✔
246

247
    /**
248
     * @name math:arcsin
249
     * @brief Calculate the arc sinus of a number
250
     * @param value the Number
251
     * =begin
252
     * (math:arcsin 1)  # 1.570796326794897 (/ math:pi 2)
253
     * =end
254
     * @author https://github.com/SuperFola
255
     */
256
    // cppcheck-suppress constParameterReference
257
    Value asin_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
258
    {
1✔
259
        if (!types::check(n, ValueType::Number))
1✔
260
            throw types::TypeCheckingError(
2✔
261
                "math:arcsin",
1✔
262
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
263
                n);
1✔
264

UNCOV
265
        return Value(std::asin(n[0].number()));
×
266
    }
1✔
267

268
    /**
269
     * @name math:arctan
270
     * @brief Calculate the arc tangent of a number
271
     * @param value the Number
272
     * =begin
273
     * (math:arctan 0)  # 0
274
     * =end
275
     * @author https://github.com/SuperFola
276
     */
277
    // cppcheck-suppress constParameterReference
278
    Value atan_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
279
    {
1✔
280
        if (!types::check(n, ValueType::Number))
1✔
281
            throw types::TypeCheckingError(
2✔
282
                "math:arctan",
1✔
283
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
284
                n);
1✔
285

UNCOV
286
        return Value(std::atan(n[0].number()));
×
287
    }
1✔
288

289
    /**
290
     * @name math:cosh
291
     * @brief Calculate the hyperbolic cosinus of a number
292
     * @param value the Number
293
     * @author https://github.com/Gryfenfer97
294
     */
295
    // cppcheck-suppress constParameterReference
296
    Value cosh_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
297
    {
1✔
298
        if (!types::check(n, ValueType::Number))
1✔
299
            throw types::TypeCheckingError(
2✔
300
                "math:cosh",
1✔
301
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
302
                n);
1✔
303

UNCOV
304
        return Value(std::cosh(n[0].number()));
×
305
    }
1✔
306

307
    /**
308
     * @name math:sinh
309
     * @brief Calculate the hyperbolic sinus of a number
310
     * @param value the Number
311
     * @author https://github.com/Gryfenfer97
312
     */
313
    // cppcheck-suppress constParameterReference
314
    Value sinh_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
315
    {
1✔
316
        if (!types::check(n, ValueType::Number))
1✔
317
            throw types::TypeCheckingError(
2✔
318
                "math:sinh",
1✔
319
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
320
                n);
1✔
321

UNCOV
322
        return Value(std::sinh(n[0].number()));
×
323
    }
1✔
324

325
    /**
326
     * @name math:tanh
327
     * @brief Calculate the hyperbolic tangent of a number
328
     * @param value the Number
329
     * @author https://github.com/Gryfenfer97
330
     */
331
    // cppcheck-suppress constParameterReference
332
    Value tanh_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
333
    {
1✔
334
        if (!types::check(n, ValueType::Number))
1✔
335
            throw types::TypeCheckingError(
2✔
336
                "math:tanh",
1✔
337
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
338
                n);
1✔
339

UNCOV
340
        return Value(std::tanh(n[0].number()));
×
341
    }
1✔
342

343
    /**
344
     * @name math:acosh
345
     * @brief Calculate the hyperbolic arc cosinus of a number
346
     * @param value the Number
347
     * @author https://github.com/Gryfenfer97
348
     */
349
    // cppcheck-suppress constParameterReference
350
    Value acosh_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
351
    {
1✔
352
        if (!types::check(n, ValueType::Number))
1✔
353
            throw types::TypeCheckingError(
2✔
354
                "math:acosh",
1✔
355
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
356
                n);
1✔
357

358
        return Value(std::acosh(n[0].number()));
×
359
    }
1✔
360

361
    /**
362
     * @name math:asinh
363
     * @brief Calculate the hyperbolic arc sinus of a number
364
     * @param value the Number
365
     * @author https://github.com/Gryfenfer97
366
     */
367
    // cppcheck-suppress constParameterReference
368
    Value asinh_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
369
    {
1✔
370
        if (!types::check(n, ValueType::Number))
1✔
371
            throw types::TypeCheckingError(
2✔
372
                "math:asinh",
1✔
373
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
374
                n);
1✔
375

UNCOV
376
        return Value(std::asinh(n[0].number()));
×
377
    }
1✔
378

379
    /**
380
     * @name math:atanh
381
     * @brief Calculate the hyperbolic arc tangent of a number
382
     * @param value the Number
383
     * @author https://github.com/Gryfenfer97
384
     */
385
    // cppcheck-suppress constParameterReference
386
    Value atanh_(std::vector<Value>& n, VM* vm [[maybe_unused]])
1✔
387
    {
1✔
388
        if (!types::check(n, ValueType::Number))
1✔
389
            throw types::TypeCheckingError(
2✔
390
                "math:atanh",
1✔
391
                { { types::Contract { { types::Typedef("value", ValueType::Number) } } } },
1✔
392
                n);
1✔
393

UNCOV
394
        return Value(std::atanh(n[0].number()));
×
395
    }
1✔
396

397
    /**
398
     * @name random
399
     * @brief Compute a random number in [-2147483648, 2147483647] or in a custom range passed to the function
400
     * @param min optional inclusive lower bound
401
     * @param max optional inclusive upper bound. Must be present if `min` is passed
402
     * =begin
403
     * (print (random))  # a number in [-2147483648, 2147483647]
404
     * (print (random 0 10))  # a number between 0 and 10
405
     * =end
406
     * @author https://github.com/SuperFola
407
     */
408
    // cppcheck-suppress constParameterReference
409
    Value random(std::vector<Value>& n, VM* vm [[maybe_unused]])
102✔
410
    {
102✔
411
        static std::mt19937 gen { std::random_device()() };
103✔
412

413
        if (n.size() == 2 && !types::check(n, ValueType::Number, ValueType::Number))
102✔
414
            throw types::TypeCheckingError(
2✔
415
                "random",
1✔
416
                { { types::Contract {
2✔
417
                    { types::Typedef("min", ValueType::Number), types::Typedef("max", ValueType::Number) } } } },
1✔
418
                n);
1✔
419

420
        if (n.size() == 2)
101✔
421
        {
422
            const auto inclusive_min = static_cast<int>(n[0].number()),
100✔
423
                       inclusive_max = static_cast<int>(n[1].number());
100✔
424

425
            std::uniform_int_distribution<> distrib(inclusive_min, inclusive_max);
100✔
426
            return Value(distrib(gen));
100✔
427
        }
100✔
428

429
        const auto x = static_cast<int>(gen());
1✔
430
        return Value(x);
1✔
431
    }
102✔
432
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc