Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Range notation

Range notation

Scheduled Pinned Locked Moved The Lounge
databasediscussiongraphicsquestionlearning
46 Posts 20 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Klaus Werner Konrad

    Well - had a (very short) discussion with Bjarne Stroustrup this day ... In a draft of his new book he explained a code fragment as

    double& Vector::operator[](int i)
    {
    if (i<0 || size()<=i) throw out_of_range{"Vector::operator[]"};
    retur n elem[i];
    }

    where size() returns the number of elements in the vector collection. But in the text he wrote: (can't find any link to 'quote' a clipboarded text) "Had we formally specified V ector’s subscript operator, we would have said something like ‘‘the index must be in the [0 : size()) range,’’ and that was in fact what we tested in our operator[]()." His reply to my question about this discrepance was (as I asked for) that I'm missing that it is 'common (at least in US) a Notation for 'open range''; what's quiet not definite - it's a 'right open range'. But that's not the Point; my query is to find out, if this Notation is (seen worlswide) really common. For me, this Notation is just confusing; I prefer the inbound (FROM incl. x TO incl. Y) notation. What are your thoughts about it (please with Nation code, to weight the arguments)

    E Offline
    E Offline
    englebart
    wrote on last edited by
    #28

    I think the [0, size()) makes sense. What I want to know is why he wrote size() <= i I would have written i >= size() was he trying to show that i was to the right of the valid range? Or maybe he reads right to left from the close parenthesis? Reading right to left on size() <= i makes more sense to me. This looks like it is talking about C++. Many loops are written with an "open interval" sequence that terminates when you pass the end of the valid interval and reach the "undefined/inaccessible" end() marker. for (itr = ..initialize..; itr != the.end(); ..advance..) { .. do the work .. } I always thought this looping structure was kind of like these driving directions: "Drive straight until you see empty air in front of you, then take a left at the top of the cliff" but it is an easy metaphor to implement for almost any container.

    K 1 Reply Last reply
    0
    • K Klaus Werner Konrad

      Well - had a (very short) discussion with Bjarne Stroustrup this day ... In a draft of his new book he explained a code fragment as

      double& Vector::operator[](int i)
      {
      if (i<0 || size()<=i) throw out_of_range{"Vector::operator[]"};
      retur n elem[i];
      }

      where size() returns the number of elements in the vector collection. But in the text he wrote: (can't find any link to 'quote' a clipboarded text) "Had we formally specified V ector’s subscript operator, we would have said something like ‘‘the index must be in the [0 : size()) range,’’ and that was in fact what we tested in our operator[]()." His reply to my question about this discrepance was (as I asked for) that I'm missing that it is 'common (at least in US) a Notation for 'open range''; what's quiet not definite - it's a 'right open range'. But that's not the Point; my query is to find out, if this Notation is (seen worlswide) really common. For me, this Notation is just confusing; I prefer the inbound (FROM incl. x TO incl. Y) notation. What are your thoughts about it (please with Nation code, to weight the arguments)

      C Offline
      C Offline
      cmmello
      wrote on last edited by
      #29

      I was taught that notation in school here in Brazil too. Best regards Mello

      K 1 Reply Last reply
      0
      • K Klaus Werner Konrad

        Well - had a (very short) discussion with Bjarne Stroustrup this day ... In a draft of his new book he explained a code fragment as

        double& Vector::operator[](int i)
        {
        if (i<0 || size()<=i) throw out_of_range{"Vector::operator[]"};
        retur n elem[i];
        }

        where size() returns the number of elements in the vector collection. But in the text he wrote: (can't find any link to 'quote' a clipboarded text) "Had we formally specified V ector’s subscript operator, we would have said something like ‘‘the index must be in the [0 : size()) range,’’ and that was in fact what we tested in our operator[]()." His reply to my question about this discrepance was (as I asked for) that I'm missing that it is 'common (at least in US) a Notation for 'open range''; what's quiet not definite - it's a 'right open range'. But that's not the Point; my query is to find out, if this Notation is (seen worlswide) really common. For me, this Notation is just confusing; I prefer the inbound (FROM incl. x TO incl. Y) notation. What are your thoughts about it (please with Nation code, to weight the arguments)

        K Offline
        K Offline
        Keith Tyra
        wrote on last edited by
        #30

        I know the notation, and learned it in math classes required for a computer science degree at a US university. But I wouldn't expect most US programmers to know it, since most of the people in my major did not like math. On hearing "Big O" these days, some of them would probably be thinking "giant robot". For my own use I sometimes use mathematical notations because they are concise and precise.

        1 Reply Last reply
        0
        • K Klaus Werner Konrad

          Well - had a (very short) discussion with Bjarne Stroustrup this day ... In a draft of his new book he explained a code fragment as

          double& Vector::operator[](int i)
          {
          if (i<0 || size()<=i) throw out_of_range{"Vector::operator[]"};
          retur n elem[i];
          }

          where size() returns the number of elements in the vector collection. But in the text he wrote: (can't find any link to 'quote' a clipboarded text) "Had we formally specified V ector’s subscript operator, we would have said something like ‘‘the index must be in the [0 : size()) range,’’ and that was in fact what we tested in our operator[]()." His reply to my question about this discrepance was (as I asked for) that I'm missing that it is 'common (at least in US) a Notation for 'open range''; what's quiet not definite - it's a 'right open range'. But that's not the Point; my query is to find out, if this Notation is (seen worlswide) really common. For me, this Notation is just confusing; I prefer the inbound (FROM incl. x TO incl. Y) notation. What are your thoughts about it (please with Nation code, to weight the arguments)

          R Offline
          R Offline
          RafagaX
          wrote on last edited by
          #31

          As several other have pointed out this is standard mathematical notation, I remember seeing it in my mathematics courses for my degree, so at least on this side of the river (Mexico) it exists, however as a programmer I have never used it to describe anything, and i'm pretty sure that my ex classmates don't even remember what this is for. So answering your question in programming is not very common, but in mathematics it's.

          CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

          K 1 Reply Last reply
          0
          • K Klaus Werner Konrad

            Well - had a (very short) discussion with Bjarne Stroustrup this day ... In a draft of his new book he explained a code fragment as

            double& Vector::operator[](int i)
            {
            if (i<0 || size()<=i) throw out_of_range{"Vector::operator[]"};
            retur n elem[i];
            }

            where size() returns the number of elements in the vector collection. But in the text he wrote: (can't find any link to 'quote' a clipboarded text) "Had we formally specified V ector’s subscript operator, we would have said something like ‘‘the index must be in the [0 : size()) range,’’ and that was in fact what we tested in our operator[]()." His reply to my question about this discrepance was (as I asked for) that I'm missing that it is 'common (at least in US) a Notation for 'open range''; what's quiet not definite - it's a 'right open range'. But that's not the Point; my query is to find out, if this Notation is (seen worlswide) really common. For me, this Notation is just confusing; I prefer the inbound (FROM incl. x TO incl. Y) notation. What are your thoughts about it (please with Nation code, to weight the arguments)

            P Offline
            P Offline
            patbob
            wrote on last edited by
            #32

            Klaus-Werner Konrad wrote:

            ‘‘the index must be in the [0 : size()) range,’’

            His reply to my question about this discrepance was (as I asked for) that I'm missing that it is 'common (at least in US) a Notation for 'open range''; what's quiet not definite - it's a 'right open range'.

            Ah, but its not an open range. Stroustrup expressed it exactly correctly -- its a range inclusive of the value 0, and exclusive of the value "size()" . If you're bounds checking access to an array, that's exactly the correct range check to make on the index. The parenthesis/square-bracket notation is indeed a standard mathematical notation for expressing the inclusive/exclusiveness of the range endpoints. Like you, I find the notation confusing. Despite reading it and using it myself countless times, I still have to look it up to make sure I use the correct bracketing. I do it because the notation is compact enough to include in the description of a function parameter, where popup documentation tips (a la intellisense) can present it to the next programmer while they're writing their code.

            We can program with only 1's, but if all you've got are zeros, you've got nothing.

            K 1 Reply Last reply
            0
            • K Klaus Werner Konrad

              Ahem ... It's one of the (Standard) notations,but not the Standard Notation ! Remember PASCAL ? And - especially for a novice programmer - this Notation can lead to the 'on off' bug 'cause of misinterpretation (I thought it was a typo, 'cause a ')' is a SHIFT+9, whereas a ']' is a ALTGR+9' (on a german keyboard) ... But my real question is, if you provide an API, in wich notation would you explain a range to your customer (app dev), and wich notation would you (as an app dev) expect (in cross-reading the doc.'cause your'e in a hurry)

              J Offline
              J Offline
              jibalt
              wrote on last edited by
              #33

              It's the standard mathematical notation for an interval ... Pascal is irrelevant.

              K 1 Reply Last reply
              0
              • K Klaus Werner Konrad

                You got me, thanks. May be I'm just ignorant, but I've never come across this notation, and it's (from a Logical thought) very uncomfortable, and also it's not common in the common sense ... Imagine: "Select - on a Scala from 1 to 10 =[1:10]" against "Select - on a Scala fron 1 to 10 =[1:11) What is intuitive ? What can you get only by cross-reading (the FM for the API)? What is the natural thinking (against that indices 'MUST start with 0') ?

                J Offline
                J Offline
                jibalt
                wrote on last edited by
                #34

                "May be I'm just ignorant" There's no maybe about it.

                K 1 Reply Last reply
                0
                • J jibalt

                  It's the standard mathematical notation for an interval ... Pascal is irrelevant.

                  K Offline
                  K Offline
                  Klaus Werner Konrad
                  wrote on last edited by
                  #35

                  As I replied before, it's the standard notation for an right-open range, but not the standard notation for an interval ...

                  1 Reply Last reply
                  0
                  • J jibalt

                    "May be I'm just ignorant" There's no maybe about it.

                    K Offline
                    K Offline
                    Klaus Werner Konrad
                    wrote on last edited by
                    #36

                    Thank you very much for your helpful comment :rolleyes:

                    J 1 Reply Last reply
                    0
                    • F Florian Rappl

                      Yes, not only in Italy. We already learn this in Mid-school here in Germany (I learned this around 5th or 6th grade). Also every calculus 101 is introducing this notation.

                      K Offline
                      K Offline
                      Klaus Werner Konrad
                      wrote on last edited by
                      #37

                      Interesting - I'm from Germay, too... But my time in scool was 'til 1976, maybe things (and stuff to learn) changed since then

                      F 1 Reply Last reply
                      0
                      • S Stefan_Lang

                        If your intention is to express a range of numbers from a to but not including b, then the mathematical notation for that is one of the following:

                        [a,b)
                        [a;b)
                        [a,b[
                        [a;b[

                        The use of ';' instead of ',' is optional in countries that use ',' as a decimal point (e. g. germany). Otherwise you'd only use ','. That said, in mathematics open or half-open intervals are normally only used in the context of real numbers, not for integral numbers. Bjarne Stroustrup was obviously thinking of that notation, and that makes sense if his intention was to generalize the notation for use with floating point intervals. But, since the index range in this example can only be an integer interval, it would have been clearer to just write [0, size()-1] instead. Also, his use of ':', while common in computer science to express a range of values, does deviate from the mathematical notation. In CS a range is often expressed as a:b, with or without brackets of any type. The meaning, as I understand it, is always the range of values between and including a and b. Personally I can't remember having seen the half-open interval notation in the context of computer science in the past 30 years. If it is formally defined somewhere, I must have missed it. I suspect this is just a mixup of mathematical ([)) and CS (a:b) notation. (CH/DE)

                        K Offline
                        K Offline
                        Klaus Werner Konrad
                        wrote on last edited by
                        #38

                        That was my thought, too; for me it was also total unfamiliar. As Richard Deeming pointed, it is the standard notation for right-open ranges according to https://en.wikipedia.org/wiki/ISO_31-11[^] As I wrote in another reply to him, this notation seems to be quite unusual in programming context, but I've read the next chapters of Bjarne's book, and he uses this notation at least consistent ...

                        1 Reply Last reply
                        0
                        • R Rob Grainger

                          As all the STL algorithms are based on right open ranges, I think it's reasonable to assume knowledge of this notation. I was taught this in the UK, as far as I know it really is standard mathematical notation, internationally, so it seems reasonable to assume knowledge of it.

                          "If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.

                          K Offline
                          K Offline
                          Klaus Werner Konrad
                          wrote on last edited by
                          #39

                          Yes, I learned a bit about this stuff recently, but coming from plain C, where there are nothing of this fancy things, I've never come accross this Notation, although I used it all the time:

                          for (i = 0; i < count; i++)
                          {
                          do_something( i );
                          }

                          1 Reply Last reply
                          0
                          • C cmmello

                            I was taught that notation in school here in Brazil too. Best regards Mello

                            K Offline
                            K Offline
                            Klaus Werner Konrad
                            wrote on last edited by
                            #40

                            Thanks for your reply, but my question was 'Is it common' (in use by programmers), not 'is it common' (to know it) May be my question was a little ambiguous - sorry for that, but english isn't my native language and some questions that in german language are total clear are not this clear in english language

                            1 Reply Last reply
                            0
                            • R RafagaX

                              As several other have pointed out this is standard mathematical notation, I remember seeing it in my mathematics courses for my degree, so at least on this side of the river (Mexico) it exists, however as a programmer I have never used it to describe anything, and i'm pretty sure that my ex classmates don't even remember what this is for. So answering your question in programming is not very common, but in mathematics it's.

                              CEO at: - Rafaga Systems - Para Facturas - Modern Components for the moment...

                              K Offline
                              K Offline
                              Klaus Werner Konrad
                              wrote on last edited by
                              #41

                              Thanks for your reply. Coming from Germany (and - of course - using a german keyboard layout) for me it's more than just confusing, because: ')' is SHIFT+9 ']' is ALTGR+9 So there is naturally a doubt if the ')' is intended or a typo ...

                              1 Reply Last reply
                              0
                              • P patbob

                                Klaus-Werner Konrad wrote:

                                ‘‘the index must be in the [0 : size()) range,’’

                                His reply to my question about this discrepance was (as I asked for) that I'm missing that it is 'common (at least in US) a Notation for 'open range''; what's quiet not definite - it's a 'right open range'.

                                Ah, but its not an open range. Stroustrup expressed it exactly correctly -- its a range inclusive of the value 0, and exclusive of the value "size()" . If you're bounds checking access to an array, that's exactly the correct range check to make on the index. The parenthesis/square-bracket notation is indeed a standard mathematical notation for expressing the inclusive/exclusiveness of the range endpoints. Like you, I find the notation confusing. Despite reading it and using it myself countless times, I still have to look it up to make sure I use the correct bracketing. I do it because the notation is compact enough to include in the description of a function parameter, where popup documentation tips (a la intellisense) can present it to the next programmer while they're writing their code.

                                We can program with only 1's, but if all you've got are zeros, you've got nothing.

                                K Offline
                                K Offline
                                Klaus Werner Konrad
                                wrote on last edited by
                                #42

                                Ok, you're right, I was not precise enough... It's not a open range, but a half-open range, or, to be really precise, a 'right half open interval' Look at: https://en.wikipedia.org/wiki/ISO_31-11[^]

                                P 1 Reply Last reply
                                0
                                • E englebart

                                  I think the [0, size()) makes sense. What I want to know is why he wrote size() <= i I would have written i >= size() was he trying to show that i was to the right of the valid range? Or maybe he reads right to left from the close parenthesis? Reading right to left on size() <= i makes more sense to me. This looks like it is talking about C++. Many loops are written with an "open interval" sequence that terminates when you pass the end of the valid interval and reach the "undefined/inaccessible" end() marker. for (itr = ..initialize..; itr != the.end(); ..advance..) { .. do the work .. } I always thought this looping structure was kind of like these driving directions: "Drive straight until you see empty air in front of you, then take a left at the top of the cliff" but it is an easy metaphor to implement for almost any container.

                                  K Offline
                                  K Offline
                                  Klaus Werner Konrad
                                  wrote on last edited by
                                  #43

                                  I always used

                                  for (i=0; i< Size(); i++)

                                  from the logical POV, but in reality I prefer

                                  int sz = size;
                                  for (i=0; i < sz; i++)

                                  to avoid the function call overhead in every condition check

                                  1 Reply Last reply
                                  0
                                  • K Klaus Werner Konrad

                                    Ok, you're right, I was not precise enough... It's not a open range, but a half-open range, or, to be really precise, a 'right half open interval' Look at: https://en.wikipedia.org/wiki/ISO_31-11[^]

                                    P Offline
                                    P Offline
                                    patbob
                                    wrote on last edited by
                                    #44

                                    Wow. Referring to that kind of range as an "open" range was not the terminology I expected. I was so sure it meant one end of the range was unbounded, that I didn't actually go read that terminology page until you replied.. so thanks for taking the time to reply -- I learned something today.

                                    We can program with only 1's, but if all you've got are zeros, you've got nothing.

                                    1 Reply Last reply
                                    0
                                    • K Klaus Werner Konrad

                                      Interesting - I'm from Germay, too... But my time in scool was 'til 1976, maybe things (and stuff to learn) changed since then

                                      F Offline
                                      F Offline
                                      Florian Rappl
                                      wrote on last edited by
                                      #45

                                      That is one possibility. There are several others: - Either you forgot (if you do not need it, you will simply forget it) - It is state dependent (rumors are that in Bavaria school is the "best", but I do not know if that has ever been correct) - Your school focused on something different (I went to a school with mathematics as primary direction) Either way - don't worry about it :)

                                      1 Reply Last reply
                                      0
                                      • K Klaus Werner Konrad

                                        Thank you very much for your helpful comment :rolleyes:

                                        J Offline
                                        J Offline
                                        jibalt
                                        wrote on last edited by
                                        #46

                                        Sure, no problem.

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups