Re: NSExpression is incorrect?
Re: NSExpression is incorrect?
- Subject: Re: NSExpression is incorrect?
- From: Ben Haller <email@hidden>
- Date: Sun, 21 Nov 2010 06:33:25 -0500
Another option would be to make your code mimic whatever NSExpression is doing on that machine, by evaluating 2 ** 3 ** 2 (once, and caching the result) and seeing whether it comes out as 64 or 512. That way if/when Apple fixes their bug, your code will seamlessly follow suit.
Which of these alternatives is best depends upon how your code is going to be used, of course. Roland's suggestion of a compatibility switch seems good; the behavior I suggest could be a third option for that switch. Then the user of the class can decide what they want to get. Probably the best default would be to use the correct (right associative) parsing, though, as it seems unlikely that a whole lot of code specifically depends upon this bug in NSExpression, so for most clients there is probably no need to propagate the buggy behavior...
Ben Haller
McGill University
On 2010-11-21, at 1:07 AM, Roland King wrote:
> I agree with you that exponentiation is usually defined as right-associative.
>
> How about a compatibility switch in your code which you can flip to make it work either way.
>
> On 21-Nov-2010, at 11:25 AM, Dave DeLong wrote:
>
>> Hi everyone,
>>
>> Let's say I have the string @"2 ** 3 ** 2". I can run this through NSPredicate to parse it into an NSExpression tree for me, like so:
>>
>> NSExpression * e = [(NSComparisonPredicate *)[NSPredicate predicateWithFormat:@"2 ** 3 ** 2 == 0"] leftExpression];
>>
>> When I log/evaluate this predicate, it gives me "(2 ** 3) ** 2", which evaluates to 64. In other words, NSExpression (or NSPredicate or whomever) has assumed that the power operator is a left-associative operator.
>>
>> According to Wikipedia (the source of all truth and knowledge), when no parentheses are present in the expression, "the order is usually understood to be top-down, not bottom-up." [1] In other words, "a ** b ** c" is understood to be "a ** (b ** c)", not "(a ** b) ** c". Put more formally, the power operator is supposed to be right associative. (rdar://problem/8692313)
>>
>> I'm currently working on some code that mimics much of the behavior of NSExpression. However, I'm now faced with a choice: do I make my code technically correct by making the power operator right associative, or do I maintain parity with NSExpression and make it left associative?
>>
>> Any suggestions you have would be welcome.
>>
>> Thanks,
>>
>> Dave DeLong
>>
>> [1]: http://en.wikipedia.org/wiki/Exponentiation#Identities_and_properties
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden