Skip to content

Commit a1acddd

Browse files
authored
Update README.md
1 parent 497f9bc commit a1acddd

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

README.md

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,110 +3,113 @@
33

44
Mathos Parser
55
=============
6-
**Mathos Parser** is a mathematical expression parser targeting the .NET Framework and .NET Standard that parses all kinds of mathematical expressions with the ability to use custom functions, operators, and variables.
6+
**Mathos Parser** is a mathematical expression parser for the .NET Framework and .NET Standard. It can parse all kinds of mathematical expressions with the ability to use custom functions, operators, and variables.
77

88
* The CIL version (compiles expressions into IL code): https://github.com/MathosProject/Mathos-Parser-CIL
99

1010
You can find documentation and examples on the [wiki](https://github.com/MathosProject/Mathos-Parser/wiki).
1111

1212
## Features
1313

14-
* Parse mathematical expressions.
15-
* Add conditional statements.
14+
* Parse and execute mathematical expressions.
1615
* Customize and override existing operators, functions, and variables.
1716
* Supports common mathematical functions, such as pow, round, sqrt, rem, and abs.
1817
* Culture independent.
1918
* And much more!
2019

2120
## Introduction
2221

23-
Mathos Parser is a part of Mathos Project, a project that aims to provide useful methods, structures, and utilities, to make life easier! This math parser is fully independent of the Mathos Core Library, so you can use this library to achieve powerful math parsing without external dependencies.
22+
Mathos Parser is a part of the Mathos Project, a project that aims to provide useful methods, structures, and utilities to make life easier! This math parser is fully independent of the Mathos Core Library, so you can use this library to achieve powerful math parsing without external dependencies.
2423

2524
## How to use
2625

2726
It's really easy to use and understand. In this topic I will try to show you some key features of the library.
2827

28+
* Custom variables
2929
* Custom operators
3030
* Custom functions
31-
* Multi-argument functions
32-
* Custom variables
3331
* Variables through parsing
32+
* Multi-argument functions
3433

35-
### Custom Operators
36-
34+
### Custom Variables
3735
```csharp
3836
// Create a parser.
3937
MathParser parser = new MathParser();
4038

41-
// Add the operator to the operator list.
42-
parser.OperatorList.Add("^");
39+
// Add a variable.
40+
parser.LocalVariables.Add("a", 25);
4341

44-
// Add an action for the newly added operator.
45-
parser.OperatorAction.Add("^", delegate(double numA, double numB)
46-
{
47-
return (decimal) Math.Pow((double) numA, (double) numB);
48-
});
42+
// How about another.
43+
parser.LocalVariables.Add("", 5);
4944

5045
// Parsing
51-
Assert.IsTrue(parser.Parse("3^2") == Math.Pow(3,2));
46+
Assert.AreEqual(30, parser.Parse("a + 猫"));
5247
```
5348

54-
### Custom Functions
49+
### Custom Operators
5550
```csharp
5651
// Create a parser.
5752
MathParser parser = new MathParser();
5853

59-
// Add the function and its contents.
60-
parser.LocalFunctions.Add("timesTwo", inputs => inputs[0] * 2);
54+
// Add a custom operator
55+
parser.Operators.Add("λ", (left, right) => Math.Pow(left, right));
6156

6257
// Parsing
63-
Assert.IsTrue(parser.Parse("timesTwo(4)") == 8);
58+
Assert.AreEqual(Math.Pow(3, 2), parser.Parse("3 λ 2"));
6459
```
6560

66-
### Multi-Argument Functions
61+
### Custom Functions
6762
```csharp
6863
// Create a parser.
6964
MathParser parser = new MathParser();
7065

71-
// Add the function and its contents.
72-
parser.LocalFunctions.Add("log", delegate(double[] inputs)
73-
{
74-
// inputs[0] is the number.
75-
// inputs[1] is the base (optional).
76-
77-
if(inputs.Length == 1)
78-
return Math.Log(inputs[0]);
79-
else if(inputs.Length == 2)
80-
return Math.Log(inputs[0], inputs[1]);
81-
else
82-
return 0; // Error.
83-
});
66+
// Add the function and its implementation.
67+
parser.LocalFunctions.Add("timesTwo", inputs => inputs[0] * 2);
8468

8569
// Parsing
86-
Assert.IsTrue(parser.Parse("log(100)") == 2);
87-
Assert.IsTrue(parser.Parse("log(8, 2)") == 3);
70+
Assert.AreEqual(8, parser.Parse("timesTwo(4)"));
8871
```
8972

90-
### Custom Variables
73+
### Variables Through Parsing
9174
```csharp
9275
// Create a parser.
9376
MathParser parser = new MathParser();
9477

95-
// Add the variable.
96-
parser.LocalVariables.Add("a", 25);
78+
// Define the variable
79+
parser.ProgrammaticallyParse("let a = 25");
9780

9881
// Parsing
99-
Assert.IsTrue(parser.Parse("a+5") == 30);
82+
Assert.AreEqual(30, parser.Parse("a + 5"));
10083
```
10184

102-
### Variables Through Parsing
85+
### Multi-Argument Functions
10386
```csharp
10487
// Create a parser.
10588
MathParser parser = new MathParser();
10689

107-
// Add the variable
108-
parser.ProgrammaticallyParse("let a = 25");
90+
// Add the function and its implementation.
91+
parser.LocalFunctions.Add("clamp", delegate (double[] inputs)
92+
{
93+
// The value.
94+
var value = inputs[0];
95+
96+
// The maximum value.
97+
var min = inputs[1];
98+
99+
// The minimum value.
100+
var max = inputs[2];
101+
102+
if (value > max)
103+
return max;
104+
105+
if (value < min)
106+
return min;
107+
108+
return value;
109+
});
109110

110111
// Parsing
111-
Assert.IsTrue(parser.Parse("a+5") == 30);
112+
Assert.AreEqual(3, parser.Parse("clamp(3,-1,5)"));
113+
Assert.AreEqual(-1, parser.Parse("clamp(-5,-1,5)"));
114+
Assert.AreEqual(5, parser.Parse("clamp(8,-1,5)"));
112115
```

0 commit comments

Comments
 (0)