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 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
    #20

    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
    • N Nicholas Marty

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

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

      Here.[^] Ah, the famous Konami code[^]. Have you ever tried it here in the Lounge ? :cool:

      ~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
      • 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 :) !

        C Offline
        C Offline
        Colin Mullikin
        wrote on last edited by
        #22

        glennPattonWork wrote:

        Hmm...(2-5/2) = -1.5, (3-5/2) = -1

        Speaking of new math, I was always under the impression that 2-5/2 = -.5 and 3-5/2 = .5 ... :doh:

        The United States invariably does the right thing, after having exhausted every other alternative. -Winston Churchill America is the only country that went from barbarism to decadence without civilization in between. -Oscar Wilde Wow, even the French showed a little more spine than that before they got their sh*t pushed in.[^] -Colin Mullikin

        G 1 Reply Last reply
        0
        • R Rage

          Here.[^] Ah, the famous Konami code[^]. Have you ever tried it here in the Lounge ? :cool:

          ~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
          #23

          Hm. Makes sense for French speaking people. Q is used quite a lot there, I guess... And yeah. There are quite few pages on the web where you can use it ;)

          1 Reply Last reply
          0
          • R Rage

            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 Offline
            C Offline
            Colin Mullikin
            wrote on last edited by
            #24

            These Brits sure are bad at math today...

            Rage wrote:

            in this part of the world, (a-b)^2 = a^2 + b^2 - 2ab.

            As far as I know, that is true in all parts of the world...

            The United States invariably does the right thing, after having exhausted every other alternative. -Winston Churchill America is the only country that went from barbarism to decadence without civilization in between. -Oscar Wilde Wow, even the French showed a little more spine than that before they got their sh*t pushed in.[^] -Colin Mullikin

            1 Reply Last reply
            0
            • C Colin Mullikin

              glennPattonWork wrote:

              Hmm...(2-5/2) = -1.5, (3-5/2) = -1

              Speaking of new math, I was always under the impression that 2-5/2 = -.5 and 3-5/2 = .5 ... :doh:

              The United States invariably does the right thing, after having exhausted every other alternative. -Winston Churchill America is the only country that went from barbarism to decadence without civilization in between. -Oscar Wilde Wow, even the French showed a little more spine than that before they got their sh*t pushed in.[^] -Colin Mullikin

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

              Maff's is all mumbo jumbo! :-\

              1 Reply Last reply
              0
              • R Rage

                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

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

                :doh:


                "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
                • Richard DeemingR Richard Deeming

                  :doh:


                  "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
                  #27

                  :laugh: good edit !

                  ~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
                  • N Nicholas Marty

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

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

                    Or IDSPISPOPD (later IDCLIP), to save time. Funny how the really important things always stay in your mind.

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

                    1 Reply Last reply
                    0
                    • N Nicholas Marty

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

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

                      That's cheating! IDFA is OK, but you have to find the keys yourself!

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

                      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