tinyexpr icon indicating copy to clipboard operation
tinyexpr copied to clipboard

Comparisons and logical operators

Open cschreib opened this issue 7 years ago • 10 comments

I've been using TinyExpr in my project and recently needed standard comparison operators (<, <=, >, >=) and boolean logic (!, &&, ||). I've just implemented this in my fork with this commit https://github.com/cschreib/tinyexpr/commit/c1b01d879ea5bb7d14a6529988327bb407641a9b and this one https://github.com/cschreib/tinyexpr/commit/4cdeb9150e3579349d4513795dfadd41a7dcb45a (adding == and !=).

Booleans are simply 0.0 for false and 1.0 (or any non-zero value) for true.

I've written a test suite to make sure that the precedence with respect to other binary operators is correct, and it all seems to work fine. There is one little thing that I do not understand though, is that I was expecting !!1 to produce 1, since --1 produces 1 in TinyExpr. I do not understand what the difference is, but !!1 currently produces 0 regardless of how many negation operators are used. It's no big deal for me, but it would be good if this worked to be consistent with the other unary operators.

Let me know if you're interested in merging this feature, then I'll prepare a pull request.

cschreib avatar Jun 30 '17 10:06 cschreib

Commit https://github.com/cschreib/tinyexpr/commit/9de3ed238a2a2d4e669b50b1f6dc159b15e0e951 fixes the issue with !!1 != 1. Thanks to @codeplea for spotting the mistake.

This commit also fixes the right-to-left version of TinyExpr (which I had broken badly by mistake), and will correctly optimize stuff like -!--!-a into -!!a (note: !! is not a no-op for numbers which are neither 0 nor 1: !!2 == 1).

cschreib avatar Jul 01 '17 16:07 cschreib

This seems like a useful feature. Have you considered making a pull request?

godlikepanos avatar Jun 04 '18 11:06 godlikepanos

I have considered it, but as per the guidelines set in https://github.com/codeplea/tinyexpr/blob/master/CONTRIBUTING I have opened an issue first to gauge interest. I interpret the subsequent lack of reply as a hint that this is not a desired feature, but I'm happy to be wrong :)

Also, my fork was based on another fork which included CMake build scripts. Adding these CMake scripts has been proposed here and rejected, so perhaps this is also a source of restraint.

cschreib avatar Jun 04 '18 18:06 cschreib

Sorry for the very late reply.

I will likely merge this into a branch, spend some time tweaking and adding documentation, and then merge into master. I've already created a pull request here.

Most of my hesitation was exactly as you expected. However, I've had people ask for both CMake support and logical operators recently, so I'm giving in. It only took a year. lol

Nice work!

codeplea avatar Jul 10 '18 06:07 codeplea

Excellent news! Glad you found it to be an interesting addition.

cschreib avatar Jul 12 '18 11:07 cschreib

It would be nice to see this branch get merged into master now. Are there any plans?

tanis2000 avatar Mar 01 '19 15:03 tanis2000

It's merged into the "logic" branch. It needs documentation before going to master. It also needs test cases added.

I won't have time to work on it for a while. Feel free to help.

codeplea avatar Mar 01 '19 15:03 codeplea

If I'm not wrong, the Operator Precedence, in the logical branch is wrong. In the branch the precedence is this: [1] > | < | >= | <= | == | != [2] AND | OR

it should be: [1] > | < | >= | <= [2] == | != [3] AND [4] OR

https://en.cppreference.com/w/c/language/operator_precedence

gfmartins avatar Aug 26 '19 20:08 gfmartins

I forked a C++ version of this library that includes logic and comparison operators in case that is useful to anyone:

https://github.com/Blake-Madden/tinyexpr-plusplus

Blake-Madden avatar Jan 31 '21 21:01 Blake-Madden

It's merged into the "logic" branch. It needs documentation before going to master. It also needs test cases added.

@codeplea - There seem to be a reasonable number of test cases, were there others you had in mind for rounding out what is already there?

I'll work on adding some documentation.

kdunn926 avatar Sep 08 '22 17:09 kdunn926