Click here to Skip to main content
15,894,362 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: 7 types of Windows 8 users and non-users Pin
Marco Bertschi15-Oct-13 2:48
protectorMarco Bertschi15-Oct-13 2:48 
GeneralRe: 7 types of Windows 8 users and non-users Pin
Brisingr Aerowing15-Oct-13 3:34
professionalBrisingr Aerowing15-Oct-13 3:34 
GeneralRe: 7 types of Windows 8 users and non-users Pin
Johnny J.15-Oct-13 2:45
professionalJohnny J.15-Oct-13 2:45 
GeneralRe: 7 types of Windows 8 users and non-users Pin
Dan Neely15-Oct-13 2:46
Dan Neely15-Oct-13 2:46 
GeneralRe: 7 types of Windows 8 users and non-users Pin
Corporal Agarn15-Oct-13 3:00
professionalCorporal Agarn15-Oct-13 3:00 
GeneralRe: 7 types of Windows 8 users and non-users Pin
Eddy Vluggen15-Oct-13 3:02
professionalEddy Vluggen15-Oct-13 3:02 
GeneralRe: 7 types of Windows 8 users and non-users Pin
Mike Hankey15-Oct-13 3:27
mveMike Hankey15-Oct-13 3:27 
GeneralTiny Quiz Pin
harold aptroot15-Oct-13 1:18
harold aptroot15-Oct-13 1:18 
This
C#
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
C#
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:
C#
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.
GeneralRe: Tiny Quiz PinPopular
Mark_Wallace15-Oct-13 1:26
Mark_Wallace15-Oct-13 1:26 
GeneralRe: Tiny Quiz Pin
Rage15-Oct-13 1:51
professionalRage15-Oct-13 1:51 
GeneralRe: Tiny Quiz Pin
nick70315-Oct-13 1:59
nick70315-Oct-13 1:59 
GeneralRe: Tiny Quiz Pin
harold aptroot15-Oct-13 2:15
harold aptroot15-Oct-13 2:15 
GeneralRe: Tiny Quiz Pin
Rage15-Oct-13 2:21
professionalRage15-Oct-13 2:21 
GeneralRe: Tiny Quiz Pin
Mark_Wallace15-Oct-13 2:34
Mark_Wallace15-Oct-13 2:34 
GeneralRe: Tiny Quiz Pin
Nicholas Marty15-Oct-13 2:47
professionalNicholas Marty15-Oct-13 2:47 
GeneralRe: Tiny Quiz Pin
Rage15-Oct-13 2:48
professionalRage15-Oct-13 2:48 
GeneralRe: Tiny Quiz Pin
Nicholas Marty15-Oct-13 3:15
professionalNicholas Marty15-Oct-13 3:15 
GeneralRe: Tiny Quiz Pin
Rage15-Oct-13 3:35
professionalRage15-Oct-13 3:35 
GeneralRe: Tiny Quiz Pin
Nicholas Marty15-Oct-13 3:44
professionalNicholas Marty15-Oct-13 3:44 
GeneralRe: Tiny Quiz Pin
Mark_Wallace15-Oct-13 4:09
Mark_Wallace15-Oct-13 4:09 
GeneralRe: Tiny Quiz Pin
Mark_Wallace15-Oct-13 4:11
Mark_Wallace15-Oct-13 4:11 
GeneralRe: Tiny Quiz Pin
glennPattonWork315-Oct-13 2:24
professionalglennPattonWork315-Oct-13 2:24 
GeneralRe: Tiny Quiz Pin
Rage15-Oct-13 2:28
professionalRage15-Oct-13 2:28 
GeneralRe: Tiny Quiz Pin
glennPattonWork315-Oct-13 2:31
professionalglennPattonWork315-Oct-13 2:31 
GeneralRe: Tiny Quiz Pin
Colin Mullikin15-Oct-13 3:42
professionalColin Mullikin15-Oct-13 3:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.