Exposing <cmath> to ChaiScript


I’ve used the basic arithmetic operations in ChaiScript. However, I would like to expose the more complex functions available in cmath, such as sqrt, sin, cos, etc. Is there a simple way to bind all the functions available in this header? Does ChaiScript already offer some math library? If I am potentially running hundreds of ChaiScript instances simultaneously, will having many bindings per ChaiScript cause memory or performance issues?


I started a math library here: https://github.com/ChaiScript/ChaiScript_extras. It would be great if you wanted to contribute to it.

I don’t recommend performing lots of heavy math inside of ChaiScript, because you could get much better performance by implementing your math-heavy code in C++ and then calling it from ChaiScript, this way you get the best of both worlds, performance from C++ and flexibility from scripting.

Re: running “hundreds” of instances simultaneously: they will not interact with each other, so you will not have performance issues. Memory will grow, certainly. You can reduce the per-instance overhead by instantiating each library only once. Like:

auto stdlib = chaiscript::Std_Lib::library();
auto mathlib = chaiscript::extras::math::bootstrap();

chaiscript::ChaiScript chai(stdlib);
chaiscript::ChaiScript chai2(stdlib);

Hopefully that helps. Running 100’s of instances at once has not been tested as far as I know, but I have tried to reduce the overhead (CPU time) of creating each instance, which should help.


I’ll give it a shot and do a few tests to see how many scripts it can handle. I will also definitely have a look at contributing to this math library. Thanks for your help!


To all it may concern, all the cmath function bindings are now available here under /include/ChaiScript/extras/math.hpp.