Tiny Quiz
-
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.
-
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.
Because 80 isn't 79. I thought we'd already established that.
I wanna be a eunuchs developer! Pass me a bread knife!
-
Because 80 isn't 79. I thought we'd already established that.
I wanna be a eunuchs developer! Pass me a bread knife!
-
-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
-
-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
-
-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
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 :) !
-
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 :) !
-
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
The day I have had, a definite possibility :rolleyes:
-
"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
Makes your eyes light up.
I wanna be a eunuchs developer! Pass me a bread knife!
-
"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
"IDKFA" I can't forget those codes ever, I think :laugh:
-
"IDKFA" I can't forget those codes ever, I think :laugh:
-
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.
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!
-
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!
-
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!
-
It does have to do with int.MinValue, but 1080000000 * 2 = -2134967296 (in int arithmetic), and the math is done in doubles.
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!
-
-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
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
-
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
Never seen an AZERTY-Keyboard :) However nothing is as famous as ^^vv<><>ba :laugh:
-
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.
-
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
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