-
Notifications
You must be signed in to change notification settings - Fork 245
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
C11 and C++20 #71
Open
tylov
wants to merge
4
commits into
codeplea:master
Choose a base branch
from
tylov-fork:c11_and_c++20
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
C11 and C++20 #71
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2ed8c05
This PR addresses the following issues:
tylov 191cc0c
type-safe function assignment.
tylov cfa448b
Updated fac() to use tgamma() so that it works for real number as well.
tylov 5211d6e
Replaced ^ with ** operator: always right-associative. Added gamma(),…
tylov 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 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 |
---|---|---|
|
@@ -15,10 +15,11 @@ the standard C math functions and runtime binding of variables. | |
## Features | ||
|
||
- **C99 with no dependencies**. | ||
- Can also be compiled with C++20. | ||
- Single source file and header file. | ||
- Simple and fast. | ||
- Implements standard operators precedence. | ||
- Exposes standard C math functions (sin, sqrt, ln, etc.). | ||
- Exposes standard C math functions (sin, sqrt, log, etc.). | ||
- Can add custom functions and variables easily. | ||
- Can bind variables at eval-time. | ||
- Released under the zlib license - free for nearly any use. | ||
|
@@ -101,7 +102,7 @@ After you're finished, make sure to call `te_free()`. | |
|
||
int err; | ||
/* Compile the expression with variables. */ | ||
te_expr *expr = te_compile("sqrt(x^2+y^2)", vars, 2, &err); | ||
te_expr *expr = te_compile("sqrt(x**2+y**2)", vars, 2, &err); | ||
|
||
if (expr) { | ||
x = 3; y = 4; | ||
|
@@ -163,20 +164,20 @@ line. It also does error checking and binds the variables `x` and `y` to *3* and | |
|
||
|
||
This produces the output: | ||
|
||
$ example2 "sqrt(x^2+y2)" | ||
``` | ||
$ example2 "sqrt(x**2+y2)" | ||
Evaluating: | ||
sqrt(x^2+y2) | ||
sqrt(x**2+y2) | ||
^ | ||
Error near here | ||
|
||
|
||
$ example2 "sqrt(x^2+y^2)" | ||
$ example2 "sqrt(x**2+y**2)" | ||
Evaluating: | ||
sqrt(x^2+y^2) | ||
sqrt(x**2+y**2) | ||
Result: | ||
5.000000 | ||
|
||
``` | ||
|
||
## Binding to Custom Functions | ||
|
||
|
@@ -228,12 +229,12 @@ Here is some example performance numbers taken from the included | |
**benchmark.c** program: | ||
|
||
| Expression | te_eval time | native C time | slowdown | | ||
| :------------- |-------------:| -----:|----:| | ||
| sqrt(a^1.5+a^2.5) | 15,641 ms | 14,478 ms | 8% slower | | ||
| a+5 | 765 ms | 563 ms | 36% slower | | ||
| a+(5*2) | 765 ms | 563 ms | 36% slower | | ||
| (a+5)*2 | 1422 ms | 563 ms | 153% slower | | ||
| (1/(a+1)+2/(a+2)+3/(a+3)) | 5,516 ms | 1,266 ms | 336% slower | | ||
| :---------------------------|----------:|----------:|------------:| | ||
| `sqrt(a**1.5+a**2.5)` | 15,641 ms | 14,478 ms | 8% slower | | ||
| `a+5` | 765 ms | 563 ms | 36% slower | | ||
| `a+(5*2)` | 765 ms | 563 ms | 36% slower | | ||
| `(a+5)*2` | 1422 ms | 563 ms | 153% slower | | ||
| `(1/(a+1)+2/(a+2)+3/(a+3))` | 5,516 ms | 1,266 ms | 336% slower | | ||
|
||
|
||
|
||
|
@@ -244,7 +245,7 @@ TinyExpr parses the following grammar: | |
<list> = <expr> {"," <expr>} | ||
<expr> = <term> {("+" | "-") <term>} | ||
<term> = <factor> {("*" | "/" | "%") <factor>} | ||
<factor> = <power> {"^" <power>} | ||
<factor> = <power> {"*\*" <power>} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than having |
||
<power> = {("-" | "+")} <base> | ||
<base> = <constant> | ||
| <variable> | ||
|
@@ -265,47 +266,26 @@ for *0.5*) | |
## Functions supported | ||
|
||
TinyExpr supports addition (+), subtraction/negation (-), multiplication (\*), | ||
division (/), exponentiation (^) and modulus (%) with the normal operator | ||
division (/), exponentiation (*\*) and modulus (%) with the normal operator | ||
precedence (the one exception being that exponentiation is evaluated | ||
left-to-right, but this can be changed - see below). | ||
|
||
The following C math functions are also supported: | ||
|
||
- abs (calls to *fabs*), acos, asin, atan, atan2, ceil, cos, cosh, exp, floor, ln (calls to *log*), log (calls to *log10* by default, see below), log10, pow, sin, sinh, sqrt, tan, tanh | ||
- abs, acos, asin, atan, atan2, cbrt, ceil, cos, cosh, exp, floor, gamma, log, log2, log10, pow, sin, sinh, sqrt, tan, tanh | ||
|
||
The following functions are also built-in and provided by TinyExpr: | ||
|
||
- fac (factorials e.g. `fac 5` == 120) | ||
- ncr (combinations e.g. `ncr(6,2)` == 15) | ||
- npr (permutations e.g. `npr(6,2)` == 30) | ||
- gcd (common denominator e.g. `gcd(30, 50)` == 10) | ||
|
||
Also, the following constants are available: | ||
|
||
- `pi`, `e` | ||
|
||
|
||
## Compile-time options | ||
|
||
|
||
By default, TinyExpr does exponentiation from left to right. For example: | ||
|
||
`a^b^c == (a^b)^c` and `-a^b == (-a)^b` | ||
|
||
This is by design. It's the way that spreadsheets do it (e.g. Excel, Google Sheets). | ||
|
||
|
||
If you would rather have exponentiation work from right to left, you need to | ||
define `TE_POW_FROM_RIGHT` when compiling `tinyexpr.c`. There is a | ||
commented-out define near the top of that file. With this option enabled, the | ||
behaviour is: | ||
|
||
`a^b^c == a^(b^c)` and `-a^b == -(a^b)` | ||
|
||
That will match how many scripting languages do it (e.g. Python, Ruby). | ||
|
||
Also, if you'd like `log` to default to the natural log instead of `log10`, | ||
then you can define `TE_NAT_LOG`. | ||
|
||
## Hints | ||
|
||
- All functions/types start with the letters *te*. | ||
|
@@ -316,4 +296,3 @@ then you can define `TE_NAT_LOG`. | |
parentheses are important, because TinyExpr will not change the order of | ||
evaluation. If you instead compiled "x+1+5" TinyExpr will insist that "1" is | ||
added to "x" first, and "5" is added the result second. | ||
|
This file contains 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 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
Oops, something went wrong.
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.
Why the change to
**
from^
? I don't necessarily oppose it, using Python frequently myself, but it's certainly not a mathematical standard practice (Mathematica, Matlab).