Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/chaiscript.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(CHAISCRIPT_VERSION 5.8.6)
set(CHAISCRIPT_VERSION 6.1.0)
find_package(chaiscript ${CHAISCRIPT_VERSION} QUIET)

if (NOT chaiscript_FOUND)
Expand Down
112 changes: 112 additions & 0 deletions include/chaiscript/extras/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,118 @@ namespace chaiscript {
isunordered<bool, float, float>(m);
isunordered<bool, long double, long double>(m);

// Create math namespace
m->eval("namespace(\"math\")");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be good to document the libraries available in the readme.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leftibot create/update readme to track the capabilities of this project


// TRIG FUNCTIONS
m->eval("math.cos = fun(x) { return cos(x) }");
Comment thread
RobLoach marked this conversation as resolved.
Outdated
m->eval("math.sin = fun(x) { return sin(x) }");
m->eval("math.tan = fun(x) { return tan(x) }");
m->eval("math.acos = fun(x) { return acos(x) }");
m->eval("math.asin = fun(x) { return asin(x) }");
m->eval("math.atan = fun(x) { return atan(x) }");
m->eval("math.atan2 = fun(x, y) { return atan2(x, y) }");

// HYPERBOLIC FUNCTIONS
m->eval("math.cosh = fun(x) { return cosh(x) }");
m->eval("math.sinh = fun(x) { return sinh(x) }");
m->eval("math.tanh = fun(x) { return tanh(x) }");

#ifndef CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED
m->eval("math.acosh = fun(x) { return acosh(x) }");
m->eval("math.asinh = fun(x) { return asinh(x) }");
m->eval("math.atanh = fun(x) { return atanh(x) }");
#endif

// EXPONENTIAL AND LOGARITHMIC FUNCTIONS
m->eval("math.exp = fun(x) { return exp(x) }");
m->eval("math.frexp = fun(x, y) { return frexp(x, y) }");
m->eval("math.ldexp = fun(x, y) { return ldexp(x, y) }");
m->eval("math.log = fun(x) { return log(x) }");
m->eval("math.log10 = fun(x) { return log10(x) }");
m->eval("math.modf = fun(x, y) { return modf(x, y) }");

#ifndef CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED
m->eval("math.exp2 = fun(x) { return exp2(x) }");
m->eval("math.expm1 = fun(x) { return expm1(x) }");
m->eval("math.ilogb = fun(x) { return ilogb(x) }");
m->eval("math.log1p = fun(x) { return log1p(x) }");
m->eval("math.log2 = fun(x) { return log2(x) }");
m->eval("math.logb = fun(x) { return logb(x) }");
m->eval("math.scalbn = fun(x, y) { return scalbn(x, y) }");
m->eval("math.scalbln = fun(x, y) { return scalbln(x, y) }");
#endif

// POWER FUNCTIONS
m->eval("math.pow = fun(x, y) { return pow(x, y) }");
m->eval("math.sqrt = fun(x) { return sqrt(x) }");

#ifndef CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED
m->eval("math.cbrt = fun(x) { return cbrt(x) }");
m->eval("math.hypot = fun(x, y) { return hypot(x, y) }");

// ERROR AND GAMMA FUNCTIONS
m->eval("math.erf = fun(x) { return erf(x) }");
m->eval("math.erfc = fun(x) { return erfc(x) }");
m->eval("math.tgamma = fun(x) { return tgamma(x) }");
m->eval("math.lgamma = fun(x) { return lgamma(x) }");
#endif

// ROUNDING AND REMAINDER FUNCTIONS
m->eval("math.ceil = fun(x) { return ceil(x) }");
m->eval("math.floor = fun(x) { return floor(x) }");
m->eval("math.fmod = fun(x, y) { return fmod(x, y) }");

#ifndef CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED
m->eval("math.trunc = fun(x) { return trunc(x) }");
m->eval("math.round = fun(x) { return round(x) }");
m->eval("math.lround = fun(x) { return lround(x) }");
m->eval("math.llround = fun(x) { return llround(x) }");
m->eval("math.rint = fun(x) { return rint(x) }");
m->eval("math.lrint = fun(x) { return lrint(x) }");
m->eval("math.llrint = fun(x) { return llrint(x) }");
m->eval("math.nearbyint = fun(x) { return nearbyint(x) }");
m->eval("math.remainder = fun(x, y) { return remainder(x, y) }");
m->eval("math.remquo = fun(x, y, z) { return remquo(x, y, z) }");

// FLOATING-POINT MANIPULATION FUNCTIONS
m->eval("math.copysign = fun(x, y) { return copysign(x, y) }");
m->eval("math.nan = fun(x) { return nan(x) }");
m->eval("math.nextafter = fun(x, y) { return nextafter(x, y) }");
m->eval("math.nexttoward = fun(x, y) { return nexttoward(x, y) }");

// MINIMUM, MAXIMUM, DIFFERENCE FUNCTIONS
m->eval("math.fdim = fun(x, y) { return fdim(x, y) }");
m->eval("math.fmax = fun(x, y) { return fmax(x, y) }");
m->eval("math.fmin = fun(x, y) { return fmin(x, y) }");

// OTHER FUNCTIONS
m->eval("math.fabs = fun(x) { return fabs(x) }");
#endif

m->eval("math.abs = fun(x) { return abs(x) }");

#ifndef CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED
m->eval("math.fma = fun(x, y, z) { return fma(x, y, z) }");

// CLASSIFICATION FUNCTIONS
m->eval("math.fpclassify = fun(x) { return fpclassify(x) }");
#endif

m->eval("math.isfinite = fun(x) { return isfinite(x) }");
m->eval("math.isinf = fun(x) { return isinf(x) }");
m->eval("math.isnan = fun(x) { return isnan(x) }");
m->eval("math.isnormal = fun(x) { return isnormal(x) }");
m->eval("math.signbit = fun(x) { return signbit(x) }");

// COMPARISON FUNCTIONS
m->eval("math.isgreater = fun(x, y) { return isgreater(x, y) }");
m->eval("math.isgreaterequal = fun(x, y) { return isgreaterequal(x, y) }");
m->eval("math.isless = fun(x, y) { return isless(x, y) }");
m->eval("math.islessequal = fun(x, y) { return islessequal(x, y) }");
m->eval("math.islessgreater = fun(x, y) { return islessgreater(x, y) }");
m->eval("math.isunordered = fun(x, y) { return isunordered(x, y) }");

return m;
}
}
Expand Down
7 changes: 4 additions & 3 deletions tests/catch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6462,7 +6462,8 @@ namespace Catch {
static bool isSet;
static struct sigaction oldSigActions[];// [sizeof(signalDefs) / sizeof(SignalDefs)];
static stack_t oldSigStack;
static char altStackMem[];
static constexpr std::size_t altStackSize = 32768;
static char altStackMem[altStackSize];

static void handleSignal( int sig );

Expand Down Expand Up @@ -6597,7 +6598,7 @@ namespace Catch {
isSet = true;
stack_t sigStack;
sigStack.ss_sp = altStackMem;
sigStack.ss_size = SIGSTKSZ;
sigStack.ss_size = altStackSize;
sigStack.ss_flags = 0;
sigaltstack(&sigStack, &oldSigStack);
struct sigaction sa = { };
Expand Down Expand Up @@ -6628,7 +6629,7 @@ namespace Catch {
bool FatalConditionHandler::isSet = false;
struct sigaction FatalConditionHandler::oldSigActions[sizeof(signalDefs)/sizeof(SignalDefs)] = {};
stack_t FatalConditionHandler::oldSigStack = {};
char FatalConditionHandler::altStackMem[SIGSTKSZ] = {};
char FatalConditionHandler::altStackMem[FatalConditionHandler::altStackSize] = {};

} // namespace Catch

Expand Down
33 changes: 31 additions & 2 deletions tests/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,40 @@

#include <iostream>

TEST_CASE( "Math namespace works", "[math]" ) {
auto mathlib = chaiscript::extras::math::bootstrap();

chaiscript::ChaiScript chai;
chai.add(mathlib);

// Trig
CHECK(chai.eval<double>("math.cos(0.5)") == std::cos(0.5));
CHECK(chai.eval<double>("math.sin(0.5)") == std::sin(0.5));
CHECK(chai.eval<double>("math.tan(0.5)") == std::tan(0.5));
CHECK(chai.eval<double>("math.acos(0.5)") == std::acos(0.5));
CHECK(chai.eval<double>("math.asin(0.5)") == std::asin(0.5));
CHECK(chai.eval<double>("math.atan(0.5)") == std::atan(0.5));
CHECK(chai.eval<double>("math.atan2(0.5, 0.5)") == std::atan2(0.5, 0.5));

// Power
CHECK(chai.eval<double>("math.pow(0.5, 3.0)") == std::pow(0.5, 3.0));
CHECK(chai.eval<double>("math.sqrt(0.5)") == std::sqrt(0.5));

// Rounding
CHECK(chai.eval<double>("math.ceil(0.5)") == std::ceil(0.5));
CHECK(chai.eval<double>("math.floor(0.5)") == std::floor(0.5));
CHECK(chai.eval<double>("math.abs(-0.5)") == std::abs(-0.5));

// Exponential
CHECK(chai.eval<double>("math.exp(0.5)") == std::exp(0.5));
CHECK(chai.eval<double>("math.log(0.5)") == std::log(0.5));
CHECK(chai.eval<double>("math.log10(0.5)") == std::log10(0.5));
}

TEST_CASE( "Math functions work", "[math]" ) {
auto mathlib = chaiscript::extras::math::bootstrap();

auto stdlib = chaiscript::Std_Lib::library();
chaiscript::ChaiScript chai(stdlib);
chaiscript::ChaiScript chai;
chai.add(mathlib);

// TRIG FUNCTIONS
Expand Down
3 changes: 1 addition & 2 deletions tests/string_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

TEST_CASE( "string_methods functions work", "[string_methods]" ) {
// Create the ChaiScript environment with stdlib available.
auto stdlib = chaiscript::Std_Lib::library();
chaiscript::ChaiScript chai(stdlib);
chaiscript::ChaiScript chai;

// Add the string_methods module.
auto stringmethods = chaiscript::extras::string_methods::bootstrap();
Expand Down