✅ Strange output of my code
why does this output 0? is it due to overflow, and if it is, why is an exception not thrown?
3 Replies
By default overflow/underflow doesn't throw but you can enable it via a setting in your csproj:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/language#checkforoverflowunderflow
Or by wrapping it in
checked(...)
IE in your case:
.Aggregate((x, y) => checked(x * y))
C# Compiler Options - language feature rules - C#
C# Compiler Options for language feature rules. These options control how the compiler interprets certain language constructs.
Since your math there is effectively doing
2^125250
I think, if I did the math right.
2x4x6x8x10....1000
Is the same as
2^(1+2+3+4+5...500)
Which can then just be
2^(1+500+2+499+3+498...)
Which becomes
2^(501+501+501...)
=
2^(501*250)
=
2^125250
=
37,704 digits long numbahcool, thank you!