nicklockwood / Expression

A cross-platform Swift library for evaluating mathematical expressions at runtime
MIT License
829 stars 51 forks source link

Complex expressions are not working #51

Closed faizal-karim closed 9 months ago

faizal-karim commented 10 months ago

I have an expression like this -> *"(0.341 8000.0) / (1 - (1 + 0.341) ^ -84)"** , I used below code but giving wrong answer.

Expected result - "109.71908469357446" Actual result - "1.538906384258725e-36"

Can someone please correct me?

do {
            let result = try Expression(
                "(0.341 * 8000.0) / (1 - (1 + 0.341) ^ -84)",
                symbols: [
                    .infix("^"): { pow($0[0], $0[1]) }
                ]
            ).evaluate()

            print(result) 

        } catch {

        }
nicklockwood commented 9 months ago

@faizal-karim I think the issue here is that ^ has a the same precedence as - so the expression is evaluated as:

(0.341 * 8000.0) / ((1 - (1 + 0.341)) ^ -84)

I can fix this quickly by hard-coding a more appropriate precedence. In the longer term I really need to expose a way to set precedence for custom operators.

nicklockwood commented 9 months ago

@faizal-karim this is fixed in the latest version (0.13.8)

jaydeepbhayani commented 9 months ago

@nicklockwood what about android?

nicklockwood commented 9 months ago

@jaydeepbhayani what about it?

jaydeepbhayani commented 9 months ago

@nicklockwood do you have same library in android?

nicklockwood commented 9 months ago

@jaydeepbhayani no. I'm not sure what the state of Swift on Android is at the moment. In principle it might work, but it's not something I can help with.