C
C#2y ago
BenMcLean

✅ dumb question involving Math.Max

Given that a and b are 32-bit integers within the range of -100 to 100, this should always return True right?
int first = a < b ? 0 : a - b;
int second = Math.Max(a - b, 0);
return first == second;
int first = a < b ? 0 : a - b;
int second = Math.Max(a - b, 0);
return first == second;
3 Replies
BenMcLean
BenMcLean2y ago
my brain's scrambled and i'm up way too late, that's my problem yeah ... no exception. what the heck
for (int a = -100; a < 100; a++)
for (int b = -100; b < 100; b++)
if ((a < b ? 0 : a - b) != Math.Max(a - b, 0))
throw new Exception();
for (int a = -100; a < 100; a++)
for (int b = -100; b < 100; b++)
if ((a < b ? 0 : a - b) != Math.Max(a - b, 0))
throw new Exception();
ero
ero2y ago
why would this throw an exception? you just said yourself that they should always be equal so they're never not equal. which is what you're testing for
BenMcLean
BenMcLean2y ago
yeah i was getting a result that looked crazy but it turns out i just needed to change an X to a Z in a different place 🙂 how i delete this i got it figured out, it wasn't this
Want results from more Discord servers?
Add your server
More Posts