Code Monkey home page Code Monkey logo

Comments (5)

nicklockwood avatar nicklockwood commented on July 30, 2024

@graycampbell it's not really possible at the moment I'm afraid. Two consecutive values are treated as an error unless one of them is registered as an operator. It might be possible to fudge it, e.g. by overriding pi with a postfix operator that multiplies the previous value by pi, but I'm not sure if that would work for all of your use cases.

from expression.

vanniktech avatar vanniktech commented on July 30, 2024

I also would like to have this. I've got the constants defined for Euler's number & PI:

        constants: [
            "π": Double.pi,
            "": Darwin.M_E,
        ],

and would like to have support for 3π.

For Android I use the following library and they support it: https://github.com/ezylang/EvalEx

from expression.

nicklockwood avatar nicklockwood commented on July 30, 2024

@vanniktech for that particular use-case, you can define them as postfix operators.

EDIT: I tested and can confirm that this works:

let expression = ""

let result = try Expression(
    expression,
    constants: ["π": .pi],
    symbols: [.postfix("π"): { $0[0] * .pi }]
).evaluate()

print(result) // 6.28319

from expression.

nicklockwood avatar nicklockwood commented on July 30, 2024

@graycampbell apologies, it seems I misled you! implicit multiplication is possible, at least for some cases. In addition to the solution for implicit multiplication of constants mentioned above (by using a postfix operator), you can also use the "()" operator to implement implicit multiplication of values in parenthesis, like this:

let expression = "4(5)"

let result = try Expression(
    expression,
    symbols: [
        .infix("()"): { $0[0] * $0[1] }
    ]
).evaluate()

print(result) // 20

from expression.

vanniktech avatar vanniktech commented on July 30, 2024

This is amazing! Can be closed from my side.

from expression.

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.