chriskonnertz / string-calc Goto Github PK
View Code? Open in Web Editor NEWPHP calculator library for mathematical terms (expressions) passed as strings
License: MIT License
PHP calculator library for mathematical terms (expressions) passed as strings
License: MIT License
if i run a function and the arguments after the first start with a unary operator (for example min(5, -10)
or min(-5, -10)
don't work while min(-5, 10)
does ) i receive an error:
Notice: Undefined variable: leftOperand in /.../vendor/chriskonnertz/string-calc/src/ChrisKonnertz/StringCalc/Calculator/Calculator.php on line 117
If i wrap the arguments inside brackets it works min(5, (-10))
I have tested only with min and max. I'm gona do some more tests and report if i find something else
This task is related to #10
Hi,
I have a problem when calculate simple sum :
$stringCalc = new ChrisKonnertz\StringCalc\StringCalc();
echo $stringCalc->calculate('0.37+0.31-0.68');
The result must be "0", but it return : "-1.1102230246252E-16"
Have you an idea of why ?
Thanks
Hi,
I have a problem when calculate simple sum :
$stringCalc = new ChrisKonnertz\StringCalc\StringCalc();
echo $stringCalc->calculate('0.37+0.31-0.68');
The result must be "0", but it return : "-1.1102230246252E-16"
Have you an idea of why ?
Thanks
$calculator = new ChrisKonnertz\StringCalc\StringCalc();
echo $calculator->calculate('1-1-1') . PHP_EOL;
prints 1
$calculator = new ChrisKonnertz\StringCalc\StringCalc();
echo $calculator->calculate('1-1+1') . PHP_EOL;
prints -1
Hi Cris,
Congratulations for the calc tool, it is very useful and I hope this project helps many people in the community.
I have a questions about division by zero. I have a mathematic expression who converts many taxes in value to execute the algorithm, but one of this divisions have a 0 value, and in my console returns warning, and don't an Exception:
Warning: Division by zero in /var/www/controlzinco/vendor/chriskonnertz/string-calc/src/ChrisKonnertz/StringCalc/Symbols/Concrete/Operators/DivisionOperator.php on line 32
There's a way to return an Exception in this case, or I need check this condition previously?
Thanks!
( 293 == 561 || 293 == 562 || 293 == 563 || 293 == 564 ) ? 1.02 : 1
The result should be 1, but I'm getting the error instead.
Uncaught ChrisKonnertz\StringCalc\Exceptions\NotFoundException: Error: Detected unknown or invalid character identifier. in /home/dev5printaj/vendor/chriskonnertz/string-calc/src/ChrisKonnertz/StringCalc/Support/UtilityTrait.php:26.
I suppose logical operators are not covered.
Firstly, thanks for this package! Saved me a ton of time.
that allows users to enter mathematical terms in a text field
Stumbling point for me, I think most non programming users would use x
in place of *
Any chance that x
can be made an alias of *
?
I see that symbols are registered in SymbolRegistry
. Is it currently possible to register custom functions/symbols on runtime without altering the codebase of string-calc
?
First of all thanks for this nice and thoughtful piece of software! I'm about trying to use it for a new library I'm working on and it looks like it could serve me well for a particular use case. However, it looks like I discovered an issue with adding custom symbols (tl;dr: it doesn't work at the moment ...). Consider this test scenario (namespaces & comments omitted for the sake of brevity):
class Test extends AbstractFunction
{
protected $identifiers = ['test'];
public function execute(array $arguments)
{
return 1;
}
}
$stringCalc = new StringCalc();
$stringHelper = $stringCalc->getContainer()->get('stringcalc_stringhelper');
$stringCalc->addSymbol(new Test($stringHelper));
echo $stringCalc->calculate('test()');
I would expect this to yield just 1
. However, I get this exception (because the parser cannot find the test()
function):
ChrisKonnertz\StringCalc\Exceptions\NotFoundException : Error: Detected unknown or invalid string identifier.
Changing this line to the following fixes the problem:
// $parser = new Parser($symbolContainer);
$parser = new Parser($this->symbolContainer);
It looks like $this->container->get('stringcalc_symbolcontainer')
is different from $this->symbolContainer
despite this assignment.
I can avoid this bug temporarily by modifying the source as shown but it would be great to have the issue fixed upstream asap as automatic build tests are failing as well. (By the way you could also add to the documentation the fact that you need to pass a StringHelper
instance to the function object constructor โ just missing this in the docs).
Add way more (and more complex) test calculations to the test.
Thanks for your awesome project.
Can we improve performance by replacing tokenize
to token_get_all
?
tinpont@local:~/expression-test$ php -f compare.php
token_get_all: 0.077315092086792
StringCalc: 2.0649530887604
Source code:
<?php
require 'vendor/autoload.php';
$limit = 100000;
$expression = 'INT(W*0.013)*0.014';
$startTime = microtime(true);
for ($i = 0; $i < $limit; $i ++) {
$tokens = token_get_all("<?php $expression;");
}
echo 'token_get_all: ' . (microtime(true) - $startTime) . PHP_EOL;
$stringCalc = new ChrisKonnertz\StringCalc\StringCalc();
$startTime = microtime(true);
for ($i = 0; $i < $limit; $i ++) {
$tokens = $stringCalc->tokenize($expression);
}
echo 'StringCalc: ' . (microtime(true) - $startTime) . PHP_EOL;
Hy Chris,
I try to implement a percentage calculation in form of:
20 + 19% that should result in 23.8 or
45 * 10% that should result in 202.5
For the first one you could also state 20 * 1.19, but the users are more familiar to the first way.
As I can see, the calculator can be extended, but operators always follow the form x [operator] y.
Do you see a way how I can implement this with the AbstractSymbol API?
Greetings from Germany
Volker
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.