GithubHelp home page GithubHelp logo

chriskonnertz / string-calc Goto Github PK

View Code? Open in Web Editor NEW
98.0 8.0 17.0 314 KB

PHP calculator library for mathematical terms (expressions) passed as strings

License: MIT License

PHP 100.00%
php php-calculator math mathematics term calculate string calculator calc tokenizer

string-calc's People

Contributors

chriskonnertz avatar jkphl avatar nickurt avatar prohalexey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

string-calc's Issues

unary operators inside functions don't work

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

Problem calcul simple sum

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

Problem calcul simple sum

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

Wrong calculations

$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

Division by 0

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!

Logical operators

( 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.

Using x as * (multiplication)

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 *?

Is it possible to add custom functions?

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?

Adding custom symbols doesn't work (internal symbol container issue)

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).

[Improve] use token_get_all instead of tokenize method

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;

Percentage calculations

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

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.