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. Other Discussions
  3. The Weird and The Wonderful
  4. zero int?

zero int?

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
41 Posts 13 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.
  • B BobJanova

    I think all zero bits in a float represents zero too (zero value and zero exponent).

    A Offline
    A Offline
    AspDotNetDev
    wrote on last edited by
    #23

    Indeed. I think there are multiple other ways to represent +-0 (e.g., if the significand is 0, any exponent should produce a result of -0 or +0, depending on the sign bit), though all zeroes should work.

    Somebody in an online forum wrote:

    INTJs never really joke. They make a point. The joke is just a gift wrapper.

    L 1 Reply Last reply
    0
    • B b10543748

      Int32 contador;
      contador = (Int32)0;

      and a store procedure with +/- 2600 lines.

      F Offline
      F Offline
      Fabio Franco
      wrote on last edited by
      #24

      b10543748 wrote:

      a store procedure with +/- 2600 lines.

      That's the real horror here. It gives me goose bumps just to think about it.

      b10543748 wrote:

      Int32 contador; contador = (Int32)0;

      I think that deserves the hall of shame, just for the fact that the person that wrote this probably had no idea what he was doing. But, what if, historically this code looked like this:

      Int32 contador;
      contador = (Int32)3.2;

      And then, for whatever reason it was change to 0 and the cast was just kept. Or even:

      long outroContador = paramX * 1000000L;
      .
      .
      .
      Int32 contador;
      contador = (Int32)outroContador;

      So, maybe, just maybe, it was just a matter of safety.

      "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

      1 Reply Last reply
      0
      • A AspDotNetDev

        Indeed. I think there are multiple other ways to represent +-0 (e.g., if the significand is 0, any exponent should produce a result of -0 or +0, depending on the sign bit), though all zeroes should work.

        Somebody in an online forum wrote:

        INTJs never really joke. They make a point. The joke is just a gift wrapper.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #25

        AspDotNetDev wrote:

        e.g., if the significand is 0, any exponent should produce a result of -0 or +0, depending on the sign bit

        No, that won't work. There is an implicit leading 1-bit when the exponent is not zero, so if the exponent is nonzero it can never represent zero. Also if the exponent is all ones you'd get infinity if the mantissa is zero.

        A 1 Reply Last reply
        0
        • L Lost User

          Even then +0 is just "all bits zero". It's just -0 that is slightly odd there.

          J Offline
          J Offline
          jsc42
          wrote on last edited by
          #26

          harold aptroot wrote:

          It's just -0 that is slightly odd

          Shouldn't it be slightly even? I am aware that there is a school of thought that 0 is neither odd nor even; but it divides by 2 with no remainder.

          L 1 Reply Last reply
          0
          • J jsc42

            harold aptroot wrote:

            It's just -0 that is slightly odd

            Shouldn't it be slightly even? I am aware that there is a school of thought that 0 is neither odd nor even; but it divides by 2 with no remainder.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #27

            Slightly even must also be slightly odd, no? Or are you suggesting that it is part even part neither?

            S 1 Reply Last reply
            0
            • L Lost User

              AspDotNetDev wrote:

              e.g., if the significand is 0, any exponent should produce a result of -0 or +0, depending on the sign bit

              No, that won't work. There is an implicit leading 1-bit when the exponent is not zero, so if the exponent is nonzero it can never represent zero. Also if the exponent is all ones you'd get infinity if the mantissa is zero.

              A Offline
              A Offline
              AspDotNetDev
              wrote on last edited by
              #28

              Good to know. :thumbsup:

              Somebody in an online forum wrote:

              INTJs never really joke. They make a point. The joke is just a gift wrapper.

              1 Reply Last reply
              0
              • L Lost User

                No. You would get the 'use of an unassigned variable' error and it would not compile. Edit: To be precise: I made two assumptions: 1) This variable is declared inside a method, not as a class member. Initializing it separately leaves no other possibility than that. 2) If you declare a variable inside a method and don't initialize it, it's still ok for the compiler as long as you don't try to use it in the following code. Such an unused declaration would only trigger a compiler warning. The error would occur as soon as you tried to use the uninitialized variable (other than initializing it) in the following code.

                And from the clouds a mighty voice spoke:
                "Smile and be happy, for it could come worse!"

                And I smiled and was happy
                And it came worse.

                K Offline
                K Offline
                KP Lee
                wrote on last edited by
                #29

                Generally, you're right. The exception for that rule is if you declare an int array. It will then initialize every int in the array to 0. Kind of nice that you don't have to go through every row in the array to begin with.

                L 1 Reply Last reply
                0
                • L Lost User

                  Where would that apply? Floating point types?

                  And from the clouds a mighty voice spoke:
                  "Smile and be happy, for it could come worse!"

                  And I smiled and was happy
                  And it came worse.

                  K Offline
                  K Offline
                  KP Lee
                  wrote on last edited by
                  #30

                  That would apply in a 1's comp environment on it's integer type. Certainly not on a Windows OS which is 2's comp like every other system that realized 2's comp is a superior mathematical process. On 1's comp with a 32 bit integer, -0 is for every bit set to 1. In 2's comp every bit as 1 in a signed integer is always -1 On 1's comp, this is how you get -0: x=-1 x=x+1 When you print it, you get "0", not "-0", but internally it's still -0. When you add 1 to -0, it first converts all the bits from 1 to 0 and then because you are changing signs, you add an additional 1 to the number so it becomes 1. -1 + 10 would produce 8 and then add 1 to get 9. You've got that extra step of adding or subtracting 1 to be done every time a mathematical operation changes case in either direction with the one exception of reaching -0. 2's comp uses no additional steps when changing cases In SQL: select (-2*1024)*1024*1024 select (2*1024)*1024*1024 will produce -2147483648 in the first result, the second will get the following error: Msg 8115, Level 16, State 2, Line 2 Arithmetic overflow error converting expression to data type int. Take out the parens and both fail with the same error. In C# with type int, both will produce -2147483648 (Assuming checked isn't applied.)

                  1 Reply Last reply
                  0
                  • B BobJanova

                    I think all zero bits in a float represents zero too (zero value and zero exponent).

                    K Offline
                    K Offline
                    KP Lee
                    wrote on last edited by
                    #31

                    I'm not sure if float will represent an entered 0 as all "bits" 0. It's the only time the value will be all zero bits. I can't remember if the exponent value is 0 or 1, but in either case, I think the exponent needs at least 1 bit to be set to 1. In any case, the -0 concept has nothing to do with float types. It's for signed integers in a 1's comp environment. I'm not sure if 1's comp was still being produced 30 years ago, I do know I learned about it around 1979 and it was known then how poor that mathematical model was. Basically -0 is produced by having all 1's set in a signed integer. You get it by first getting a negative number and then adding to reach 0. In 1's comp, every negative number is the exact complement of the same positive number. (Position to position every 1 bit is set to 0 and every 0 bit is set to 1 to change from a positive number to the same negative number. In 1's comp, the 1's bit is on for odd positive numbers and off for odd negative numbers. In 2's comp the 1's bit is on for both positive and negative numbers.

                    L 1 Reply Last reply
                    0
                    • K KP Lee

                      Generally, you're right. The exception for that rule is if you declare an int array. It will then initialize every int in the array to 0. Kind of nice that you don't have to go through every row in the array to begin with.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #32

                      No that's not true, it only zeroes out the array if you create it, not if you declare it. If you declare it, there is no array, just a variable that could hold it. So there's nothing to zero. And the array would still have to satisfy the rules of definite assignment. (not its elements of course, which is what you seem to be talking about)

                      K 1 Reply Last reply
                      0
                      • K KP Lee

                        I'm not sure if float will represent an entered 0 as all "bits" 0. It's the only time the value will be all zero bits. I can't remember if the exponent value is 0 or 1, but in either case, I think the exponent needs at least 1 bit to be set to 1. In any case, the -0 concept has nothing to do with float types. It's for signed integers in a 1's comp environment. I'm not sure if 1's comp was still being produced 30 years ago, I do know I learned about it around 1979 and it was known then how poor that mathematical model was. Basically -0 is produced by having all 1's set in a signed integer. You get it by first getting a negative number and then adding to reach 0. In 1's comp, every negative number is the exact complement of the same positive number. (Position to position every 1 bit is set to 0 and every 0 bit is set to 1 to change from a positive number to the same negative number. In 1's comp, the 1's bit is on for odd positive numbers and off for odd negative numbers. In 2's comp the 1's bit is on for both positive and negative numbers.

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #33

                        -0 has everything to do with floats. OK there is also one's complement, which died a long time ago. And sign-magnitude integers, which also died. FYI, 0.0f = 0x00000000 and -0.0f = 0x80000000

                        1 Reply Last reply
                        0
                        • L Lost User

                          No that's not true, it only zeroes out the array if you create it, not if you declare it. If you declare it, there is no array, just a variable that could hold it. So there's nothing to zero. And the array would still have to satisfy the rules of definite assignment. (not its elements of course, which is what you seem to be talking about)

                          K Offline
                          K Offline
                          KP Lee
                          wrote on last edited by
                          #34

                          I was thinking in terms of int[] num = new int[10]; not int[] num; You're agreeing with me that the 10 ints in the first case will all be zero. if (num == null) would be true in the latter case because there isn't any value types to compare. if (num[0] == null) would blow up because the array doesn't exist, I don't think it would compile because a value type can't be null. I don't think that first "if" would work if you declared int num;

                          L 1 Reply Last reply
                          0
                          • K KP Lee

                            I was thinking in terms of int[] num = new int[10]; not int[] num; You're agreeing with me that the 10 ints in the first case will all be zero. if (num == null) would be true in the latter case because there isn't any value types to compare. if (num[0] == null) would blow up because the array doesn't exist, I don't think it would compile because a value type can't be null. I don't think that first "if" would work if you declared int num;

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #35

                            Uninitialized local variables actually aren't null, but uninitialized. So you can't do if (num == null) if you didn't initialize num, you would get "Error: Use of unassigned variable 'num'". You actually can compare an int with null, that just gives a warning that it's always false.

                            K 1 Reply Last reply
                            0
                            • L Lost User

                              Uninitialized local variables actually aren't null, but uninitialized. So you can't do if (num == null) if you didn't initialize num, you would get "Error: Use of unassigned variable 'num'". You actually can compare an int with null, that just gives a warning that it's always false.

                              K Offline
                              K Offline
                              KP Lee
                              wrote on last edited by
                              #36

                              There is a difference between local and declared variables. int IS set to zero here:

                                  // set these variables in the declaration section
                                  bool isEmpty = false;
                                  int\[\] num;
                                  bool isEmpty2;
                                  int num2;
                                  private void SetEndElement(bool strt, bool outer, bool vert)
                                  {
                                      // have these in a routine, it will compile without error
                                      if (num == null) isEmpty = true;
                                      // I expected a runtime error here, but it runs, isEmpty2 is false, num2 is 0
                                      if (num2 == null) isEmpty2 = true;
                              

                              The following produces 2 warnings and 2 errors and won't compile Warning 1 The variable 'isEmpty' is assigned but its value is never used ... (stats on where the error is) ... Error 3 Use of unassigned local variable 'num' ...

                                  private void SetEndElement(bool strt, bool outer, bool vert)
                                  {
                                      bool isEmpty = false;
                                      int\[\] num;
                                      bool isEmpty2;
                                      int num2;
                                      if (num == null) isEmpty = true;
                                      if (num2 == null) isEmpty2 = true;
                              

                              So we're both wrong. :)

                              harold aptroot wrote:

                              warning that it's always false.

                              L 1 Reply Last reply
                              0
                              • K KP Lee

                                There is a difference between local and declared variables. int IS set to zero here:

                                    // set these variables in the declaration section
                                    bool isEmpty = false;
                                    int\[\] num;
                                    bool isEmpty2;
                                    int num2;
                                    private void SetEndElement(bool strt, bool outer, bool vert)
                                    {
                                        // have these in a routine, it will compile without error
                                        if (num == null) isEmpty = true;
                                        // I expected a runtime error here, but it runs, isEmpty2 is false, num2 is 0
                                        if (num2 == null) isEmpty2 = true;
                                

                                The following produces 2 warnings and 2 errors and won't compile Warning 1 The variable 'isEmpty' is assigned but its value is never used ... (stats on where the error is) ... Error 3 Use of unassigned local variable 'num' ...

                                    private void SetEndElement(bool strt, bool outer, bool vert)
                                    {
                                        bool isEmpty = false;
                                        int\[\] num;
                                        bool isEmpty2;
                                        int num2;
                                        if (num == null) isEmpty = true;
                                        if (num2 == null) isEmpty2 = true;
                                

                                So we're both wrong. :)

                                harold aptroot wrote:

                                warning that it's always false.

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #37

                                KP Lee wrote:

                                So we're both wrong.

                                I don't see how you got to this conclusion. Maybe I have to make it clearer. - statically uninitialized local variables can not be used. - comparing an int with null gives a warning that it's always false. - the thread was about local variables. - fields in a class are not called "declared variables", but are implicitly set to default(T).

                                K 1 Reply Last reply
                                0
                                • L Lost User

                                  KP Lee wrote:

                                  So we're both wrong.

                                  I don't see how you got to this conclusion. Maybe I have to make it clearer. - statically uninitialized local variables can not be used. - comparing an int with null gives a warning that it's always false. - the thread was about local variables. - fields in a class are not called "declared variables", but are implicitly set to default(T).

                                  K Offline
                                  K Offline
                                  KP Lee
                                  wrote on last edited by
                                  #38

                                  harold aptroot wrote:

                                  - comparing an int with null gives a warning that it's always false.

                                  That statement is where you are wrong. It NEVER says that. At least on my compiler. With local variables, that results in a fatal error (using uninialized local variables) Absolutely no warning about the comparison. See my prior post for the exact error msg. If YOUR compiler gives THAT warning, I appologize, I assumed you were using a Microsoft compiler. One new enough to compile (var x = "tst";) I definitely will not vouch for all versions.

                                  harold aptroot wrote:

                                  - fields in a class are not called "declared variables", but are implicitly set to default(T).

                                  POE TAE TOE/ PAW TAW TOE :laugh:

                                  L 1 Reply Last reply
                                  0
                                  • B b10543748

                                    Int32 contador;
                                    contador = (Int32)0;

                                    and a store procedure with +/- 2600 lines.

                                    K Offline
                                    K Offline
                                    KP Lee
                                    wrote on last edited by
                                    #39

                                    b10543748 wrote:

                                    and a store procedure with +/- 2600 lines.

                                    I assume you are talking SQL. I haven't counted lines, but I've had printouts of over 45 pages. Personally, if your sproc prints out more than 5 pages, you've screwed up the design. At least in an OLTP environment. (I've never said anything like that to my employer, I'll just think it.)

                                    1 Reply Last reply
                                    0
                                    • K KP Lee

                                      harold aptroot wrote:

                                      - comparing an int with null gives a warning that it's always false.

                                      That statement is where you are wrong. It NEVER says that. At least on my compiler. With local variables, that results in a fatal error (using uninialized local variables) Absolutely no warning about the comparison. See my prior post for the exact error msg. If YOUR compiler gives THAT warning, I appologize, I assumed you were using a Microsoft compiler. One new enough to compile (var x = "tst";) I definitely will not vouch for all versions.

                                      harold aptroot wrote:

                                      - fields in a class are not called "declared variables", but are implicitly set to default(T).

                                      POE TAE TOE/ PAW TAW TOE :laugh:

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #40

                                      Well, what code are you using? I'm talking about something like this:

                                      int x = 0;
                                      if (x == null) ;

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        Slightly even must also be slightly odd, no? Or are you suggesting that it is part even part neither?

                                        S Offline
                                        S Offline
                                        Stefan_Lang
                                        wrote on last edited by
                                        #41

                                        Maybe he didn't mean slightly odd, but evenly odd?

                                        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