nhanaz-pm-pl / Calculator

Execute math expressions in the server or console on PocketMine-MP servers.
GNU General Public License v3.0
5 stars 1 forks source link

Setting value of variable #3

Open Jack10270 opened 1 year ago

Jack10270 commented 1 year ago

Hi this request for adding variable support to plugin

When user types expression sender can set value of variable:

(Sender) /calc x + 1
(Server) Enter value for variable x:
(Sender) 3
(Server) x + 1 = 4 where x = 3

(Sender) /calc x + 1
(Server) Enter value for variable x:
(Sender) pi / 4
(Server) x + 1 = 1.7 where x = 0.7

For this cyou can use await-std to be done easy (code adapted from https://github.com/SOF3/await-std-examples/blob/master/BanHammer/src/SOFe/BanHammer/Main.php)

$expr = implode(" ", $args);
Await::f2c(function() use($sender, $expr) : Generator{
    try{
        $values = [];
        foreach(expr variables as variable){
            $sender->sendMessage("Enter value for variable "  + $variable);
            $value = $std->consumeNextChat($sender);
            $values[variable] = $value;
        }

        evaluate expression using $values
    }catch(QuitException){
        // do nothing if $sender quit
    }
});
NhanAZ commented 1 year ago

This issue is related to the math expression parsing library I am using on this plugin I think you should open an issue at https://github.com/Muqsit/arithmexp Their library doesn't seem to support solving equations yet

Jack10270 commented 1 year ago

Hi sir,,in the repository arithm exp in readme there is this code I think for allow to add variables

$expression = $parser->parse("x + y");
var_dump($expression->evaluate(["x" => 2, "y" => 3])); // int(5)
var_dump($expression->evaluate(["x" => 1.5, "y" => 1.5])); // float(3)
NhanAZ commented 1 year ago

It doesn't work as you think. I opened an issue on his library You can see here: https://github.com/Muqsit/arithmexp/issues/12

Jack10270 commented 1 year ago

@NhanAZ I mean equation substitution, equation solver will different thing yes

If I will give equation a + b and also give value for a and b, this is variable substitution To do this the plugin will replace a with 3 and b with 4 and do calculation

If I will give equation a + bx and ask to find value of x, this is equation solving (solve for x) To do this the plugin will do like "Let y = a + bx" or if its quadratic a(x^2)+bx+c the plugin will do like "Let a(x^2)+bx+c=0"

But it will also be good to have equation solver but my intend here was variable substitution

NhanAZ commented 1 year ago
(Sender) /calc x + 1
(Server) Enter value for variable x:
(Sender) 3
(Server) x + 1 = 4 where x = 3

Why don't you use something like this?

(Sender) /calc 3 + 1
(Server) Result: 4
NhanAZ commented 2 months ago
(Sender) /calc x + 1
(Server) Enter value for variable x:
(Sender) 3
(Server) x + 1 = 4 where x = 3

Why don't you use something like this?

(Sender) /calc 3 + 1
(Server) Result: 4

After about 2 years I realized my question was stupid :D