Giter VIP home page Giter VIP logo

matex's Issues

Can't handle negative numbers

Try to use a negative number in the equation.

5 + (-0.5) produces an error when it should result to 4.5.
5 + -0.5 produces an error when it should result to 4.5.

No License

It would be fantastic if the project will be released under a license. I would like to use the well written code.
More Info

Exception: Empty argument in Evaluator->addArgument() when custom function without arguments

I have and EvalMathFunctions class, holding a 'random' function without arguments.

class EvalMathFunctions {
  static public function random() {
    return rand();
  }
}

I register this in matex:

$this->evaluator->functions = [
  'random' => ['ref' => '\\EvalMathFunctions::random', 'arc' => 0]
];

I then call the function:

$this->evaluator->execute('random()');

And get the error.

Assign a value to a variable inside an expression

Not entirely sure whether this is a bug or a feature request.

In my code I have:

$this->matex->execute('a=random()');

Unfortunately, this throws an exception: Exception: Unknown variable: a in Evaluator->getVariable(). Am I correct that this is not supposed to work in the current version?

Problem calcul simple addition

Hi,

I have a problem when calculate simple sum :
$evaluator = new \Matex\Evaluator();
$evaluator->execute('0.37+0.31-0.68');

The result must be "0", but it return : "-1.1102230246252E-16"

Have you an idea of why ?

Thanks

Make variables case sensitive (not only Lower Case)

Is your feature request related to a problem? Please describe.
I have a use case where my variables are UCased (i.e. A, B, ...). I was surprised to have to use strict Lcase variables because your Evaluator is LCasing the formula passed to it.

Describe the solution you'd like
Let the user choose the case sensitivity via an option and by default use the exact formula that the user provides (no LCasing).
OR
LCase all variables passed to the evaluator (this one is not my favorite).

Describe alternatives you've considered
N/A

Additional context
N/A.

Rounding

Is your feature request related to a problem? Please describe.
Currently its not possible to round

Describe the solution you'd like
Will be awesome if there is a way to use it that way for example i want to do round(0.7/100) or something else..

Describe alternatives you've considered
Currently im using plain php, will like to switch to a lib like this so i can create dynamic calculations via user inputs for example.

Additional context
Is there any alternative package to this that supports round? I couldn't find any maybe im searching the wrong keywrods.

Support comparison operators (>, <, =, >=,<=) to get a true / false evaluation

Is your feature request related to a problem? Please describe.
This feature request is related to a use case. I need to implement price calculation depending on some variables. A price can be obtained through various formula. Each formula has a condition of application (e.g a+b+c > 200 then use formula 1 - I need to be able to evaluate the formula a+b+c > 200)

Describe the solution you'd like
Take the comparison operators into account.

Describe alternatives you've considered
N/A

Additional context
N/A.

Could not evaluate "2*0*0.45*0.3*7.4999999999999997E-2"

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Order of operations between powers and multiplications

Describe the bug
The order of operations between powers and multiplications is not what I expect

To Reproduce
Steps to reproduce the behavior:

$evaluator = new \Matex\Evaluator();
$evaluator->execute(4*3^2); // returns 144
$evaluator->execute(45/3^2); // returns 225

Expected behavior
Following usual PEDMAS order of operations, powers should be computed before multiplications and/or divisions.
The above code should then return 36 on line 2 and 5 on line 3

Code can't handle round brackets in strings

Describe the bug
Unmatched brackets exception for a simple string concatenation.

To Reproduce
Steps to reproduce the behavior:
execute('"(" + "xyz"')

Expected behavior
execute function tests for unmatched brackets but does not ignore brackets within a "string" - bracket checking should occur per expression parsed.

Screenshots
n/a

Desktop (please complete the following information):
n/a

Smartphone (please complete the following information):
n/a

Additional context
Add any other context about the problem here.

Examples difficult to understand

This is only a suggestion, but I needed time to understand examples. I suggest you to remove the object requirement. For function example :

function sum() {
$arguments = func_get_args();
$result = 0;
foreach ($arguments as $argument)
$result += $argument;
return $result;
}
$evaluator = new \Matex\Evaluator();
$evaluator->functions = [
'sum' => ['ref' => 'sum', 'arc' => null]
];
echo $evaluator->execute('sum(1, 2, 3)');

Handle invalid operations

Adding a number and a string now produces a php warning.

e.g.:

$evaluator = new \Matex\Evaluator();

try {
    $evaluator->execute('1 + "x"');
} catch(Exception $exception) {
    // ....
}

It would be nice to have a catchable exception when the terms do not match and the calculation is invalid.

( 101.25 + -202.44 ) results in "syntax error"

Describe the bug
( 101.25 + -202.44 ) = syntax error

To Reproduce


$evaluator = new \Matex\Evaluator();
$error = FALSE;
try {
	$val = $evaluator->execute("( 101.25 + -202.44 )");
} catch ( \Exception $e ) {
	$error = $e->getMessage();
	$val = "__";
}
echo $val;
echo $error;

Expected behavior
return -101.19 and not error out

Desktop (please complete the following information):

  • OS: windows 10. php 7.2

Not working with multiples of 10

So every time it executes an equation with zeros mostly, it doesn't work. If I execute just '10' it will give me just the multiplication of 10 ( so 1 for 10, 2 for 20 ) and that's all. Also, getting floats like 0.5 into the equation will give an error. So there's a problem whenever 0 is present.

Custom basic operators

I'd like to have a modulo operator, but I would generalise and say it would be nice if we could add custom functions which behave like the basic operators, that being that you call them like a op b instead of op(a, b).

Example given does't work

static function sum($arguments) {
	$result = 0;
	foreach ($arguments as $argument)
		$result += $argument;
	return $result;
}

$evaluator = new \Matex\Evaluator();
$evaluator->functions = [
	'sum' => ['ref' => '\\Space\\Class::sum', 'arc' => null]
];
echo $evaluator->execute('sum(1, 2, 3)');

In the function sum, when I output $arguments, I only get the first value (1, in this case).
How do I get array of all the arguments?

Add composer.json and add this to https://packagist.org/

Is your feature request related to a problem? Please describe.
This project looks really handy, but without having it on https://packagist.org/ it makes installing and maintenance very old school. E.g. my current laravel project I have to download the files and manually autoload them.

Describe the solution you'd like
Please can you add a composer.json file to the project and submit the project to https://packagist.org/ This would allow for easier installation and maintenance, when used inside other projects.

Describe alternatives you've considered
There ain't one

Additional context

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.