Giter Site home page Giter Site logo

Add Exponent operator about expressions HOT 7 CLOSED

malown avatar malown commented on August 19, 2024 1
Add Exponent operator

from expressions.

Comments (7)

bbjay avatar bbjay commented on August 19, 2024 1

Just came across the same issue.
The error message I got was
"NoSuchMethodError: Class 'double' has no instance method '^'."

I first tried to define a '^' method with pow in the context, but it looks like the context is just for variables and functions, not for infix/binary operators.

In the end I got it working with a custom evaluator:

import 'dart:math';

class MyEvaluator extends ExpressionEvaluator {
	const MyEvaluator();

	@override
	evalBinaryExpression(BinaryExpression expression, Map<String, dynamic> context) {
		if (expression.operator == '^') {
			final left = eval(expression.left, context);
			final right = () => eval(expression.right, context);
			return pow(left, right());
		}
		return super.evalBinaryExpression(expression, context);
	}
}

from expressions.

apgapg avatar apgapg commented on August 19, 2024

Its already there i guess
case '^': return left ^ right();

Try a test case of 2^2 and see if it evaluates correctly

from expressions.

malown avatar malown commented on August 19, 2024

That's the bitwise XOR operator
https://en.wikipedia.org/wiki/Bitwise_operations_in_C#Bitwise_XOR_^

from expressions.

malown avatar malown commented on August 19, 2024

That will interfere with the bitwise XOR mentioned above. Also, you didn't set the precedence for the operator so the expression 5 * 3^2 gets evaluated as (5 * 3)^2

Check out my repo for a solution. I Used the ° sign as the exponent operator as i couldn't get ** to get parsed properly

https://github.com/malown/expressions

from expressions.

apgapg avatar apgapg commented on August 19, 2024

You can simply define a callMemberExpression like 'pow(2,2)'

from expressions.

malown avatar malown commented on August 19, 2024

For my usecase i need the expression to be easily readable by humans without using regex

from expressions.

Throvn avatar Throvn commented on August 19, 2024

Does someone have a solution for this?
Unfortunately @malown took all of their repositories down
and I have no clue on how to extend the code snippet above to make it work properly :(

from expressions.

Related Issues (20)

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.