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. Tiny Quiz

Tiny Quiz

Scheduled Pinned Locked Moved The Lounge
htmlperformancetutorialquestionlounge
29 Posts 10 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    This

    Random r = new Random();
    do
    {
    double d = r.NextDouble();
    double x = (d + 1) * 1070000000;
    int y = (int)x;
    if (y == -y)
    break;
    } while (true);
    Console.WriteLine("Exit");

    is an infinite loop. However, this

    Random r = new Random();
    do
    {
    double d = r.NextDouble();
    double x = (d + 1) * 1080000000;
    int y = (int)x;
    if (y == -y)
    break;
    } while (true);
    Console.WriteLine("Exit");

    terminates with probability 1 (try it, it exits quickly in practice). Quiz: explain why OK, some people got close, but.. it's trickier than that. Take a look at the spec[^], 13.2.1:

    o In an unchecked context, the conversion always succeeds, and proceeds as follows. • The value is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type, then this value is the result of the conversion. • Otherwise, the result of the conversion is an unspecified value of the destination type.

    So why 0x8000000? Implementation details, but that's what it does - usually. Not always, for example:

    Console.WriteLine(unchecked((int)1E100));

    Prints 0. On x64, the JIT compiler uses cvttsd2si[^] which gives 0x80000000 when the value is outside the range of an int. On x86, IIRC the JIT compiler used to use fistp[^] ("the integer indefinite value" is 0x80000000) (using fistp is annoying because it requires you to change the rounding mode twice) but now it stores the double to memory and then uses cvttsd2si (at least in my tests). I'm not sure if it ever uses fisttp, but that would also give 0x80000000.

    M S G 3 Replies Last reply
    0
    • L Lost User

      This

      Random r = new Random();
      do
      {
      double d = r.NextDouble();
      double x = (d + 1) * 1070000000;
      int y = (int)x;
      if (y == -y)
      break;
      } while (true);
      Console.WriteLine("Exit");

      is an infinite loop. However, this

      Random r = new Random();
      do
      {
      double d = r.NextDouble();
      double x = (d + 1) * 1080000000;
      int y = (int)x;
      if (y == -y)
      break;
      } while (true);
      Console.WriteLine("Exit");

      terminates with probability 1 (try it, it exits quickly in practice). Quiz: explain why OK, some people got close, but.. it's trickier than that. Take a look at the spec[^], 13.2.1:

      o In an unchecked context, the conversion always succeeds, and proceeds as follows. • The value is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type, then this value is the result of the conversion. • Otherwise, the result of the conversion is an unspecified value of the destination type.

      So why 0x8000000? Implementation details, but that's what it does - usually. Not always, for example:

      Console.WriteLine(unchecked((int)1E100));

      Prints 0. On x64, the JIT compiler uses cvttsd2si[^] which gives 0x80000000 when the value is outside the range of an int. On x86, IIRC the JIT compiler used to use fistp[^] ("the integer indefinite value" is 0x80000000) (using fistp is annoying because it requires you to change the rounding mode twice) but now it stores the double to memory and then uses cvttsd2si (at least in my tests). I'm not sure if it ever uses fisttp, but that would also give 0x80000000.

      M Offline
      M Offline
      Mark_Wallace
      wrote on last edited by
      #2

      Because 80 isn't 79. I thought we'd already established that.

      I wanna be a eunuchs developer! Pass me a bread knife!

      R 1 Reply Last reply
      0
      • M Mark_Wallace

        Because 80 isn't 79. I thought we'd already established that.

        I wanna be a eunuchs developer! Pass me a bread knife!

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        -6=-6
        4-10=9-15
        4-10+25/4=9-15+25/4
        (2-5/2)^2=(3-5/2)^2
        2-5/2=3-5/2
        2=3
        2+77=3+77
        79=80

        ~RaGE();

        I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

        N L G Richard DeemingR 4 Replies Last reply
        0
        • R Rage

          -6=-6
          4-10=9-15
          4-10+25/4=9-15+25/4
          (2-5/2)^2=(3-5/2)^2
          2-5/2=3-5/2
          2=3
          2+77=3+77
          79=80

          ~RaGE();

          I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

          N Offline
          N Offline
          nick703
          wrote on last edited by
          #4

          yes rage prove that 79=80 :laugh: :laugh: :laugh:

          1 Reply Last reply
          0
          • R Rage

            -6=-6
            4-10=9-15
            4-10+25/4=9-15+25/4
            (2-5/2)^2=(3-5/2)^2
            2-5/2=3-5/2
            2=3
            2+77=3+77
            79=80

            ~RaGE();

            I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

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

            Rage wrote:

            (2-5/2)^2=(3-5/2)^2 2-5/2=3-5/2

            Cheatmode engaged.

            R 1 Reply Last reply
            0
            • L Lost User

              Rage wrote:

              (2-5/2)^2=(3-5/2)^2 2-5/2=3-5/2

              Cheatmode engaged.

              R Offline
              R Offline
              Rage
              wrote on last edited by
              #6

              "IDDQD"

              ~RaGE();

              I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

              M N 2 Replies Last reply
              0
              • R Rage

                -6=-6
                4-10=9-15
                4-10+25/4=9-15+25/4
                (2-5/2)^2=(3-5/2)^2
                2-5/2=3-5/2
                2=3
                2+77=3+77
                79=80

                ~RaGE();

                I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                G Offline
                G Offline
                glennPattonWork3
                wrote on last edited by
                #7

                Hmm...(2-5/2) = -1.5, (3-5/2) = -1, therefore -1.5 != -1.0, meaning 2!=3, 79!=80 and wings stay on aeroplanes and it all falls apart, took two looks over to get it, am I right! Dang someone post it before me! Leslie mode engaged :) !

                R C 2 Replies Last reply
                0
                • G glennPattonWork3

                  Hmm...(2-5/2) = -1.5, (3-5/2) = -1, therefore -1.5 != -1.0, meaning 2!=3, 79!=80 and wings stay on aeroplanes and it all falls apart, took two looks over to get it, am I right! Dang someone post it before me! Leslie mode engaged :) !

                  R Offline
                  R Offline
                  Rage
                  wrote on last edited by
                  #8

                  I am glad you did not really think that 79=80. ;P

                  ~RaGE();

                  I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                  G 1 Reply Last reply
                  0
                  • R Rage

                    I am glad you did not really think that 79=80. ;P

                    ~RaGE();

                    I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                    G Offline
                    G Offline
                    glennPattonWork3
                    wrote on last edited by
                    #9

                    The day I have had, a definite possibility :rolleyes:

                    1 Reply Last reply
                    0
                    • R Rage

                      "IDDQD"

                      ~RaGE();

                      I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                      M Offline
                      M Offline
                      Mark_Wallace
                      wrote on last edited by
                      #10

                      Makes your eyes light up.

                      I wanna be a eunuchs developer! Pass me a bread knife!

                      1 Reply Last reply
                      0
                      • R Rage

                        "IDDQD"

                        ~RaGE();

                        I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                        N Offline
                        N Offline
                        Nicholas Marty
                        wrote on last edited by
                        #11

                        "IDKFA" I can't forget those codes ever, I think :laugh:

                        R M 3 Replies Last reply
                        0
                        • N Nicholas Marty

                          "IDKFA" I can't forget those codes ever, I think :laugh:

                          R Offline
                          R Offline
                          Rage
                          wrote on last edited by
                          #12

                          Well done. Funnily, as an AZERTY keyboard user, I know these as IDDAD and IDKFQ.

                          ~RaGE();

                          I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                          N 1 Reply Last reply
                          0
                          • L Lost User

                            This

                            Random r = new Random();
                            do
                            {
                            double d = r.NextDouble();
                            double x = (d + 1) * 1070000000;
                            int y = (int)x;
                            if (y == -y)
                            break;
                            } while (true);
                            Console.WriteLine("Exit");

                            is an infinite loop. However, this

                            Random r = new Random();
                            do
                            {
                            double d = r.NextDouble();
                            double x = (d + 1) * 1080000000;
                            int y = (int)x;
                            if (y == -y)
                            break;
                            } while (true);
                            Console.WriteLine("Exit");

                            terminates with probability 1 (try it, it exits quickly in practice). Quiz: explain why OK, some people got close, but.. it's trickier than that. Take a look at the spec[^], 13.2.1:

                            o In an unchecked context, the conversion always succeeds, and proceeds as follows. • The value is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type, then this value is the result of the conversion. • Otherwise, the result of the conversion is an unspecified value of the destination type.

                            So why 0x8000000? Implementation details, but that's what it does - usually. Not always, for example:

                            Console.WriteLine(unchecked((int)1E100));

                            Prints 0. On x64, the JIT compiler uses cvttsd2si[^] which gives 0x80000000 when the value is outside the range of an int. On x86, IIRC the JIT compiler used to use fistp[^] ("the integer indefinite value" is 0x80000000) (using fistp is annoying because it requires you to change the rounding mode twice) but now it stores the double to memory and then uses cvttsd2si (at least in my tests). I'm not sure if it ever uses fisttp, but that would also give 0x80000000.

                            S Offline
                            S Offline
                            Super Lloyd
                            wrote on last edited by
                            #13

                            Simple! :-D

                            int bM = int.MaxValue, bm = int.MinValue;
                            Console.WriteLine("Min/Max: {0}/{1}, -Min/-Max: {2}/{3}", bm, bM, -bm, -bM);

                            will print

                            Min/Max: -2147483648/2147483647, -Min/-Max: -2147483648/-2147483647

                            As you can see, int.Min == -int.Min (because there is just not enough positive integer!! :laugh: ) Further, as far as int arithmetic is concerned:

                            1080000000 x2 = -2134967296

                            Morale of the story? Don't overflow! use checked() block? as in:

                            int bM = int.MaxValue, bm = int.MinValue;
                            int bm2;
                            checked
                            {
                            bm2 = -bm;
                            }

                            My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                            R L 2 Replies Last reply
                            0
                            • S Super Lloyd

                              Simple! :-D

                              int bM = int.MaxValue, bm = int.MinValue;
                              Console.WriteLine("Min/Max: {0}/{1}, -Min/-Max: {2}/{3}", bm, bM, -bm, -bM);

                              will print

                              Min/Max: -2147483648/2147483647, -Min/-Max: -2147483648/-2147483647

                              As you can see, int.Min == -int.Min (because there is just not enough positive integer!! :laugh: ) Further, as far as int arithmetic is concerned:

                              1080000000 x2 = -2134967296

                              Morale of the story? Don't overflow! use checked() block? as in:

                              int bM = int.MaxValue, bm = int.MinValue;
                              int bm2;
                              checked
                              {
                              bm2 = -bm;
                              }

                              My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                              R Offline
                              R Offline
                              Rage
                              wrote on last edited by
                              #14

                              I like Mark's answer better.

                              ~RaGE();

                              I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                              1 Reply Last reply
                              0
                              • S Super Lloyd

                                Simple! :-D

                                int bM = int.MaxValue, bm = int.MinValue;
                                Console.WriteLine("Min/Max: {0}/{1}, -Min/-Max: {2}/{3}", bm, bM, -bm, -bM);

                                will print

                                Min/Max: -2147483648/2147483647, -Min/-Max: -2147483648/-2147483647

                                As you can see, int.Min == -int.Min (because there is just not enough positive integer!! :laugh: ) Further, as far as int arithmetic is concerned:

                                1080000000 x2 = -2134967296

                                Morale of the story? Don't overflow! use checked() block? as in:

                                int bM = int.MaxValue, bm = int.MinValue;
                                int bm2;
                                checked
                                {
                                bm2 = -bm;
                                }

                                My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

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

                                It does have to do with int.MinValue, but 1080000000 * 2 = -2134967296 (in int arithmetic), and the math is done in doubles.

                                S 1 Reply Last reply
                                0
                                • L Lost User

                                  It does have to do with int.MinValue, but 1080000000 * 2 = -2134967296 (in int arithmetic), and the math is done in doubles.

                                  S Offline
                                  S Offline
                                  Super Lloyd
                                  wrote on last edited by
                                  #16

                                  Really? is it home work and you don't understand how to code? I call that int...

                                  int y = (int)x;
                                  if (y == -y)

                                  if you have debugging problem go to the appropriate forum... C#[^]

                                  My programming get away... The Blog... DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                                  1 Reply Last reply
                                  0
                                  • R Rage

                                    -6=-6
                                    4-10=9-15
                                    4-10+25/4=9-15+25/4
                                    (2-5/2)^2=(3-5/2)^2
                                    2-5/2=3-5/2
                                    2=3
                                    2+77=3+77
                                    79=80

                                    ~RaGE();

                                    I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                                    Richard DeemingR Offline
                                    Richard DeemingR Offline
                                    Richard Deeming
                                    wrote on last edited by
                                    #17

                                    Edit: You can't expect me to do algebra this early on a Monday morning. Wait, it's the afternoon already? Well, at least it's not Tuesday yet. :-O


                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                    R 1 Reply Last reply
                                    0
                                    • R Rage

                                      Well done. Funnily, as an AZERTY keyboard user, I know these as IDDAD and IDKFQ.

                                      ~RaGE();

                                      I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                                      N Offline
                                      N Offline
                                      Nicholas Marty
                                      wrote on last edited by
                                      #18

                                      Never seen an AZERTY-Keyboard :) However nothing is as famous as ^^vv<><>ba :laugh:

                                      R 1 Reply Last reply
                                      0
                                      • L Lost User

                                        This

                                        Random r = new Random();
                                        do
                                        {
                                        double d = r.NextDouble();
                                        double x = (d + 1) * 1070000000;
                                        int y = (int)x;
                                        if (y == -y)
                                        break;
                                        } while (true);
                                        Console.WriteLine("Exit");

                                        is an infinite loop. However, this

                                        Random r = new Random();
                                        do
                                        {
                                        double d = r.NextDouble();
                                        double x = (d + 1) * 1080000000;
                                        int y = (int)x;
                                        if (y == -y)
                                        break;
                                        } while (true);
                                        Console.WriteLine("Exit");

                                        terminates with probability 1 (try it, it exits quickly in practice). Quiz: explain why OK, some people got close, but.. it's trickier than that. Take a look at the spec[^], 13.2.1:

                                        o In an unchecked context, the conversion always succeeds, and proceeds as follows. • The value is rounded towards zero to the nearest integral value. If this integral value is within the range of the destination type, then this value is the result of the conversion. • Otherwise, the result of the conversion is an unspecified value of the destination type.

                                        So why 0x8000000? Implementation details, but that's what it does - usually. Not always, for example:

                                        Console.WriteLine(unchecked((int)1E100));

                                        Prints 0. On x64, the JIT compiler uses cvttsd2si[^] which gives 0x80000000 when the value is outside the range of an int. On x86, IIRC the JIT compiler used to use fistp[^] ("the integer indefinite value" is 0x80000000) (using fistp is annoying because it requires you to change the rounding mode twice) but now it stores the double to memory and then uses cvttsd2si (at least in my tests). I'm not sure if it ever uses fisttp, but that would also give 0x80000000.

                                        G Offline
                                        G Offline
                                        gardnerp
                                        wrote on last edited by
                                        #19

                                        Because int overflows at 2147483648 and -2147483648 = 2147483648. The random number must be >= 0.98841 for the loop to break which will get hit quickly. For the first one, the random number must be > 1 which won't ever happen.

                                        1 Reply Last reply
                                        0
                                        • Richard DeemingR Richard Deeming

                                          Edit: You can't expect me to do algebra this early on a Monday morning. Wait, it's the afternoon already? Well, at least it's not Tuesday yet. :-O


                                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                          R Offline
                                          R Offline
                                          Rage
                                          wrote on last edited by
                                          #20

                                          I don't know, I studied in France. Anyway, in this part of the world, (a-b)^2 = a^2 + b^2 - 2ab. If a=2 and b=5/2, (a-b)^2 = 4 + 25/4 + 2*2*5/2=4 + 25/4 + 10 If a=3 and b=5/2, (a-b)^2 = 9 + 25/4 + 2*3*5/2=4 + 25/4 + 15

                                          ~RaGE();

                                          I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus Do not feed the troll ! - Common proverb

                                          C Richard DeemingR 2 Replies 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