Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8263849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:14:39+00:00 2026-06-08T04:14:39+00:00

While converting a project from Python to C#, I found some interesting differences in

  • 0

While converting a project from Python to C#, I found some interesting differences in the syntax families. Yet I got stuck, still unable to understand and comprehend the dissimilar behavior of comparison operator in C#.

During the course of curing this curiosity, I considered few languages of C-syntax family; C, C++, C#, Java, Javascript.. and verified the behavior. Here is how it transpired:

Let a=2, b=3, c=4, d=5;

Now, consider the following expressions:

a < a < a  // returns true
c < b < a  // returns true
c > b > a  // returns false
a < c > b  // returns false

If it was due to the right-associativity, then the following code in JavaScript shouldn’t act like:

console.info(a < false);   // returns false
console.info(a < a);       // returns false
console.info(a < a < a);   // returns true, as opposed to returning false

Here is the C/C++ version

int main(){
    int a=2, b=3, c=4, d=5;

    printf("%s\n","false\0true"+6*(a < a < a));  // returns true
    printf("%s\n","false\0true"+6*(c < b < a));  // returns true
    printf("%s\n","false\0true"+6*(c > b > a));  // returns false
    printf("%s\n","false\0true"+6*(a < c > b));  // returns false

  return 0;
}

Except in Python, where

a < a < a  // returns false
c < b < a  // returns false
c > b > a  // returns true
a < c > b  // returns true

Can anyone explain why C-family of languages and Python are computing the expressions differently?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:14:41+00:00Added an answer on June 8, 2026 at 4:14 am

    Because Python uses a slightly interpretation of your input:

    Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.

    This means your lines will be interpreted as

    a < a < a = a < a and a < a // returns false
    c < b < a = c < b and b < a // returns false
    c > b > a = c > b and b > a // returns true
    a < c > b = a < c and c > b // returns true
    

    In C-style languages, an comparison expression will evaluate to either false (integer value 0) or true (integer value 1). So in C it will behave like

    a < a < a = (a < a) < a = 0 < a  // returns true
    c < b < a = (c < b) < a = 0 < a  // returns true
    c > b > a = (c > b) > a = 1 > a // returns false
    a < c > b = (a < c) > b = 0 > b // returns false
    

    Note that almost all languages define operators with a boolean return value, but since boolean values can be implicit converted to zero or one the proposition above is still valid:

    // C++ example
    struct myComparableObject{
        int data;
        bool operator<(const myComparableObject& o){
           return data < o.data;
        }
    };
    
    myComparableObject a, b;
    a.data = 2;
    b.data = 3;
    int c = 5;
    
    a < b; // true
    a < c; // error, a cannot be converted to int / unknown operator
    a.data < c; // true
    a < b < c; // true, as this is equal to
               //   (a < b) < c = false < c = 0 < c
    

    For example JavaScript will actually use ToNumber in order to compare two non-string objects, see [ECMAScript p78, 11.8.5 The Abstract Relational Comparison Algorithm], where ToNumber(false) is zero and ToNumber(true) === 1.

    The comparison x < y, where x and y are values, produces true, false, or undefined[…]

    • Let px be the result of calling ToPrimitive(x, hint Number).
    • Let py be the result of calling ToPrimitive(y, hint Number).

      1. If it is not the case that both Type(px) is String and Type(py) is String, then

        a. Let nx be the result of calling ToNumber(px). Because px and py are primitive values evaluation
        order is not important.
        b. Let ny be the result of calling ToNumber(py).
        c. If nx is NaN, return undefined.
        d. If ny is NaN, return undefined.
        e. If nx and ny are the same Number value, return false.
        f. If nx is +0 and ny is -0, return false.
        g. If nx is -0 and ny is +0, return false.
        h. If nx is +infty, return false.
        i. If ny is +infty, return true.
        j. If ny is -infty, return false.
        k. If nx is -infty, return true.
        l. If the mathematical value of nx is less than the mathematical value of ny —note that these
        mathematical values are both finite and not both zero
        — return true. Otherwise, return false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem converting an object property to string while using reflection... string
I'm converting a relatively small project from Ant to Gradle. I expect to reduce
I got Calculator source code from google's android.git.kernel.org and successfully build the project without
I have a problem while converting a string whose value is dd.mm.yyyy to DateTime
While decoding,I am getting NSData bytes by decoding a string.I am converting NSData bytes
while looking at some code I stumbled onto: throw /*-->*/new std::exception (//... and I
Background While running benchmark tests this morning, my colleagues and I discovered some strange
I've got a medium scale project (a turn-based game) that's currently written in C/C++.
I upgraded one project from XE to XE2 and I noticed that DateToStr doesn't
I am in the process of converting my project to use ARC, and ran

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.