Skip to content

Commit

Permalink
Show using te_type in example code instead of doubles
Browse files Browse the repository at this point in the history
TE_FLOAT would change these to float, so te_type should be used instead
  • Loading branch information
Blake-Madden committed Nov 18, 2023
1 parent 182ba0d commit bd7c830
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
16 changes: 8 additions & 8 deletions docs/manual/custom-extensions.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char* argv[])
}
const char* expression = argv[1];
double x{ 0 }, y{ 0 }; // x and y are bound at eval-time.
te_type x{ 0 }, y{ 0 }; // x and y are bound at eval-time.
// Store variable names and pointers.
te_parser tep;
tep.set_variables_and_functions({ {"x", &x}, {"y", &y} });
Expand Down Expand Up @@ -48,7 +48,7 @@ int main(int argc, char* argv[])
*TinyExpr++* can call custom functions\index{functions!binding to custom} also. Here is a short example:

```{.cpp}
double my_sum(double a, double b)
te_type my_sum(te_type a, te_type b)
{
/* Example function that adds two numbers together. */
return a + b;
Expand All @@ -70,7 +70,7 @@ Here is an example of using a lambda:
te_parser tep;
tep.set_variables_and_functions({
{ "mysum",
[](double a, double b) noexcept
[](te_type a, te_type b) noexcept
{ return a + b; } }
});
Expand All @@ -93,7 +93,7 @@ class te_expr_array : public te_expr
public:
explicit te_expr_array(const te_variable_flags type) noexcept :
te_expr(type) {}
std::array<double, 5> m_data = { 5, 6, 7, 8, 9 };
std::array<te_type, 5> m_data = { 5, 6, 7, 8, 9 };
};
```

Expand All @@ -102,17 +102,17 @@ actions on it. (Note that proper error handling is not included for brevity.):

```{.cpp}
// Returns the value of a cell from the object's data.
double cell(const te_expr* context, double a)
te_type cell(const te_expr* context, te_type a)
{
auto* c = dynamic_cast<const te_expr_array*>(context);
return static_cast<double>(c->m_data[static_cast<size_t>(a)]);
return static_cast<te_type>(c->m_data[static_cast<size_t>(a)]);
}
// Returns the max value of the object's data.
double cell_max(const te_expr* context)
te_type cell_max(const te_expr* context)
{
auto* c = dynamic_cast<const te_expr_array*>(context);
return static_cast<double>(
return static_cast<te_type>(
*std::max_element(c->m_data.cbegin(), c->m_data.cend()));
}
```
Expand Down
14 changes: 7 additions & 7 deletions docs/manual/unknown-symbol-resolution.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ take one of the following signatures:

::: {.minipage data-latex="{\textwidth}"}
```
double callback(std::string_view);
double callback(std::string_view, std::string&);
te_type callback(std::string_view);
te_type callback(std::string_view, std::string&);
```
:::

Expand All @@ -55,7 +55,7 @@ will also be available through `te_parser::get_last_error_message()`.
Here is a simple example using a function:

```{.cpp}
double ResolveResolutionSymbols(std::string_view str)
te_type ResolveResolutionSymbols(std::string_view str)
{
// Note that this is case sensitive for brevity.
return (str == "RES" || str == "RESOLUTION") ?
Expand Down Expand Up @@ -97,8 +97,8 @@ te_parser tep;
// dynamic strings like "FY2004" or "FY1997" and convert them to 2004 and 1997.
tep.set_unknown_symbol_resolver(
// Handler should except a string (which will be the unrecognized token)
// and return a double.
[](std::string_view str) -> double
// and return a te_type.
[](std::string_view str) -> te_type
{
const std::regex re{ "FY([0-9]{4})",
std::regex_constants::icase | std::regex_constants::ECMAScript };
Expand Down Expand Up @@ -146,9 +146,9 @@ Additionally, you want to increase the values of these variables with every eval
te_parser tep;
tep.set_unknown_symbol_resolver(
[](std::string_view str) -> double
[](std::string_view str) -> te_type
{
static double stressLevel{ 3 };
static te_type stressLevel{ 3 };
if (std::strncmp(str.data(), "STRESS", 6) == 0)
{ return stressLevel++; }
else
Expand Down
8 changes: 4 additions & 4 deletions docs/manual/usage.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*TinyExpr++*'s `te_parser` class defines these functions:

```{.cpp}
double evaluate(const std::string_view expression);
double get_result();
te_type evaluate(const std::string_view expression);
te_type get_result();
bool success();
int64_t get_last_error_position();
std::string get_last_error_message();
Expand Down Expand Up @@ -78,7 +78,7 @@ Example:
#include "tinyexpr.h"
#include <iostream>
double x{ 0 }, y{ 0 };
te_type x{ 0 }, y{ 0 };
// Store variable names and pointers.
te_parser tep;
tep.set_variables_and_functions({{"x", &x}, {"y", &y}});
Expand All @@ -105,7 +105,7 @@ else
:::

Along with positional and message information, the return value\index{results!evaluating failed} of a parse can also indicate failure.
When an evaluation fails, the parser will return NaN (i.e., `std::numeric_limits<double>::quiet_NaN()`) as the result.
When an evaluation fails, the parser will return NaN (i.e., `std::numeric_limits<te_type>::quiet_NaN()`) as the result.
NaN values can be verified using the standard function `std::isnan()`.

## Example {-}
Expand Down

0 comments on commit bd7c830

Please sign in to comment.