-
Notifications
You must be signed in to change notification settings - Fork 10
Fix #13: math: Use a math.* namespace #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leftibot
wants to merge
5
commits into
ChaiScript:master
Choose a base branch
from
leftibot:fix/issue-13-math-use-a-math-namespace
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6016cb4
Fix #13: Add math.* namespace for Lua-style access to math functions
leftibot 23008f2
Address review: Use ChaiScript namespaces instead of global math object
leftibot 022c611
Address review: Use register_namespace for real math namespace
leftibot aa93761
Document capabilities in README
leftibot cbc737f
Merge remote-tracking branch 'origin/master' into fix/issue-13-math-u…
leftibot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,80 +1,170 @@ | ||
| # ChaiScript Extras | ||
|
|
||
| User contributed wrappers and modules for ChaiScript. | ||
| User contributed wrappers and modules for [ChaiScript](https://github.com/ChaiScript/ChaiScript). | ||
|
|
||
| All modules are header-only and live under `chaiscript::extras`. To use a module, | ||
| include its header and add the returned `Module` to your `ChaiScript` engine (or, | ||
| for math, register the namespace directly on the engine). | ||
|
|
||
| ## Modules | ||
|
|
||
| - [Math](#math): Adds common math methods to ChaiScript. | ||
| - [String ID](#string-id): String hashing with [string_id](https://github.com/foonathan/string_id) | ||
| - [String](#string): Adds some extra string methods to ChaiScript strings | ||
| | Module | Header | Description | | ||
| |---|---|---| | ||
| | [Math](#math) | `chaiscript/extras/math.hpp` | Standard C++ `<cmath>` functions, as flat globals or as a `math.*` namespace. | | ||
| | [String Methods](#string-methods) | `chaiscript/extras/string_methods.hpp` | Extra string utilities (`replace`, `trim`, `split`, `toLowerCase`, ...). | | ||
| | [String ID](#string-id) | `chaiscript/extras/string_id.hpp` | Bindings for [foonathan/string_id](https://github.com/foonathan/string_id) string hashing. | | ||
|
|
||
| --- | ||
|
|
||
| ## Math | ||
|
|
||
| The Math module adds some standard math functions to ChaiScript. | ||
| Wraps the standard C++ `<cmath>` functions for ChaiScript. Two APIs are provided: | ||
|
|
||
| 1. **`bootstrap()`** — registers the functions as free (global) names such as `cos(x)`. | ||
| Includes `float`, `double`, and `long double` overloads for every function. | ||
| 2. **`bootstrap_namespace()`** — registers a real `math` namespace on the engine so | ||
| functions can be called as `math.cos(x)`, similar to Lua's `math` library. | ||
| Double-precision only. | ||
|
|
||
| Both APIs may be used together; they do not conflict. | ||
|
|
||
| ### Install | ||
| ``` cpp | ||
|
|
||
| ```cpp | ||
| #include "chaiscript/extras/math.hpp" | ||
| ``` | ||
| ``` cpp | ||
|
|
||
| ### Flat / global usage — `bootstrap()` | ||
|
|
||
| ```cpp | ||
| chaiscript::ChaiScript chai; | ||
| auto mathlib = chaiscript::extras::math::bootstrap(); | ||
| chai.add(mathlib); | ||
| ``` | ||
|
|
||
| ### Usage | ||
| ```chaiscript | ||
| var result = cos(0.5) | ||
| ``` | ||
|
|
||
| ``` chaiscript | ||
| var result = cos(0.5f) | ||
| ### Namespace usage — `bootstrap_namespace()` | ||
|
|
||
| ```cpp | ||
| chaiscript::ChaiScript chai; | ||
| chaiscript::extras::math::bootstrap_namespace(chai); | ||
| ``` | ||
|
|
||
| ### Options | ||
| ```chaiscript | ||
| import("math") | ||
| var result = math.cos(0.5) | ||
| ``` | ||
|
|
||
| Compile with one of the following flags to enable or disable features... | ||
| - `CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED` When enabled, will skip some of the advanced math functions. | ||
| ### Available functions | ||
|
|
||
| ## String ID | ||
| All names below are available in both APIs unless noted. Functions marked **(advanced)** | ||
| are compiled in only when `CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED` is **not** defined. | ||
|
|
||
| Adds [String ID](https://github.com/foonathan/string_id) support to ChaiScript. | ||
| | Category | Functions | | ||
| |---|---| | ||
| | Trigonometric | `cos`, `sin`, `tan`, `acos`, `asin`, `atan`, `atan2` | | ||
| | Hyperbolic | `cosh`, `sinh`, `tanh`, `acosh` *(advanced)*, `asinh` *(advanced)*, `atanh` *(advanced)* | | ||
| | Exponential / logarithmic | `exp`, `log`, `log10`, `exp2` *(advanced)*, `expm1` *(advanced)*, `ilogb` *(advanced)*, `log1p` *(advanced)*, `log2` *(advanced)*, `logb` *(advanced)* | | ||
| | Exponential (flat-only) | `frexp`, `ldexp`, `modf`, `scalbn` *(advanced)*, `scalbln` *(advanced)* | | ||
| | Power | `pow`, `sqrt`, `cbrt` *(advanced)*, `hypot` *(advanced)* | | ||
| | Error / gamma *(advanced)* | `erf`, `erfc`, `tgamma`, `lgamma` | | ||
| | Rounding / remainder | `ceil`, `floor`, `fmod`, `trunc` *(advanced)*, `round` *(advanced)*, `lround` *(advanced)*, `llround` *(advanced)*, `rint` *(advanced)*, `lrint` *(advanced)*, `llrint` *(advanced)*, `nearbyint` *(advanced)*, `remainder` *(advanced)* | | ||
| | Rounding (flat-only, advanced) | `remquo` | | ||
| | Floating-point manipulation *(advanced)* | `copysign`, `nextafter`, `nexttoward`, `nan` *(flat-only)* | | ||
| | Min / max / difference *(advanced)* | `fdim`, `fmax`, `fmin` | | ||
| | Absolute value | `abs`, `fabs` *(advanced)*, `fma` *(advanced)* | | ||
| | Classification | `isfinite`, `isinf`, `isnan`, `isnormal`, `signbit`, `fpclassify` *(advanced)* | | ||
| | Comparison | `isgreater`, `isgreaterequal`, `isless`, `islessequal`, `islessgreater`, `isunordered` | | ||
|
|
||
| ### Install | ||
| ### Compile options | ||
|
|
||
| ``` cpp | ||
| #include "chaiscript/extras/string_id.hpp" | ||
| ``` | ||
| - `CHAISCRIPT_EXTRAS_MATH_SKIP_ADVANCED` — if defined, skips the functions marked *(advanced)* | ||
| above. Useful for reducing compile time and binary size when only the basics are needed. | ||
|
|
||
| ``` cpp | ||
| auto string_idlib = chaiscript::extras::string_id::bootstrap(); | ||
| chai.add(string_idlib); | ||
| ``` | ||
| --- | ||
|
|
||
| ## String | ||
| ## String Methods | ||
|
|
||
| Adds various string methods to extend how strings can be used in ChaiScript: | ||
| - `string::replace(string, string)` | ||
| - `string::trim()` | ||
| - `string::trimStart()` | ||
| - `string::trimEnd()` | ||
| - `string::split(string)` | ||
| - `string::toLowerCase()` | ||
| - `string::toUpperCase()` | ||
| - `string::includes()` | ||
| Adds method-style string utilities to ChaiScript strings. | ||
|
|
||
| ### Install | ||
|
|
||
| ``` cpp | ||
| ```cpp | ||
| #include "chaiscript/extras/string_methods.hpp" | ||
| ``` | ||
|
|
||
| ``` cpp | ||
| ```cpp | ||
| chaiscript::ChaiScript chai; | ||
| // split() returns a vector of strings, so register the vector type if you plan to index into it: | ||
| chai.add(chaiscript::bootstrap::standard_library::vector_type<std::vector<std::string>>("StringVector")); | ||
| auto stringmethods = chaiscript::extras::string_methods::bootstrap(); | ||
| chai.add(stringmethods); | ||
| ``` | ||
|
|
||
| ### Methods | ||
|
|
||
| | Method | Description | | ||
| |---|---| | ||
| | `string::replace(string search, string replace)` | Replace all occurrences of `search` with `replace`. | | ||
| | `string::replace(char search, char replace)` | Replace all occurrences of character `search` with `replace`. | | ||
| | `string::trim()` | Remove leading and trailing whitespace. | | ||
| | `string::trimStart()` | Remove leading whitespace. | | ||
| | `string::trimEnd()` | Remove trailing whitespace. | | ||
| | `string::split(string token)` | Split on `token`, returning a vector of strings. | | ||
| | `string::toLowerCase()` | Return a lowercase copy of the string. | | ||
| | `string::toUpperCase()` | Return an uppercase copy of the string. | | ||
| | `string::includes(string search)` | Return `true` if `search` occurs in the string. | | ||
| | `string::includes(char search)` | Return `true` if character `search` occurs in the string. | | ||
|
|
||
| ### Usage | ||
|
|
||
| ``` chaiscript | ||
| ```chaiscript | ||
| var input = "Hello, World!" | ||
| var output = input.replace("Hello", "Goodbye") | ||
| // => "Goodbye, World!" | ||
|
|
||
| " padded ".trim() // => "padded" | ||
| "a,b,c".split(",")[1] // => "b" | ||
| "Hello".toUpperCase() // => "HELLO" | ||
| "Hello World".includes("orld") // => true | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## String ID | ||
|
|
||
| Adds [foonathan/string_id](https://github.com/foonathan/string_id) support to | ||
| ChaiScript, exposing `string_id`, `string_info`, `default_database`, and | ||
| `basic_database` (when compiled with `FOONATHAN_STRING_ID_DATABASE`). | ||
|
|
||
| ### Install | ||
|
|
||
| ```cpp | ||
| #include "chaiscript/extras/string_id.hpp" | ||
| ``` | ||
|
|
||
| ```cpp | ||
| chaiscript::ChaiScript chai; | ||
| auto string_idlib = chaiscript::extras::string_id::bootstrap(); | ||
| chai.add(string_idlib); | ||
| ``` | ||
|
|
||
| Types registered: `default_database`, `basic_database`, `string_id`, `string_info`. | ||
| Operators `==` and `!=` are provided between `string_id` and `hash_type`. | ||
|
|
||
| --- | ||
|
|
||
| ## Building and testing | ||
|
|
||
| This is a header-only library, so no build is required to use it. To build and run the tests: | ||
|
|
||
| ```sh | ||
| cmake -B build -S . | ||
| cmake --build build -j | ||
| ctest --test-dir build --output-on-failure | ||
| ``` | ||
|
|
||
| Tests live in `tests/` and use [Catch2](https://github.com/catchorg/Catch2). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since these now live in the math namespace, would it make sense to remove the original ones.