Comment On Math.Min(x, x - 0) = ?

Call it confirmation bias, call it superstition, but as developers and human beings, we're all susceptible to it. Say you're working on a particularly tricky bit of code, so you've fallen back on the "try all kinds of crazy crap until it passes the specific test case" method, an invaluable tool for all developers. You find an approach that works, and while you're not sure exactly why it worked, you keep it in mind for the future. [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:06 • by ounos
first - 0

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:06 • by Mark (unregistered)
Two possibilities would be (obviously) that either it is causing a cast or - more likely - it was just an indication of plus or minus for the offset that is now removed - in case he needs it again.

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:07 • by staying power (unregistered)
third! And yes, that's what I yell in bed.

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:12 • by Procedural (unregistered)

This is actually an old trick; on some old interpreted languages (think Applesoft Basic) with floating point expression issues (spurious resolution problems that went beyond normal resolution issues), a good way to clear up the problem was to force the variable into some arithmetic expression; a = a+ 0 did the trick.

Can't imagine that surviving a high-level optimizer nowadays, though.

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:14 • by anonymous (unregistered)
Possibly not a (total, because the result of the min/max is unique) WTF, as -0 < 0 and this could cause non overlapping rectangles appear overlapping using standard checks.

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:16 • by OhDear (unregistered)
I have no idea how many variables I have multiplied by one.
x*=1;
In various languages it has solved a multitude of hurts.

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:18 • by Mii (unregistered)
reminds me of javascript to string conversion: ""+x :P

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:18 • by ekolis
I always get min and max mixed up - when I want to set a minimum bound of zero for something, I do this:

foo = Math.Min(foo, 0);

and wonder why my "minimum bound" doesn't work and it acts like a "maximum bound" instead! :P

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:23 • by FunnyChauvinist (unregistered)
224030 in reply to 224023
staying power:
third! And yes, that's what I yell in bed.

Despite actually being fifth... the first two must usually be pretty quiet about it.

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:23 • by Smash King
224032 in reply to 224023
staying power:
third! And yes, that's what I yell in bed.
That's interesting! Your wife have some fun before you get home and you know it?

I suppose all you do about it is hope you can go home earlier the next day to scream "FRIST!!11!"? Care to give me your home address?

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:25 • by Chris (unregistered)
224033 in reply to 224028
Mii:
reminds me of javascript to string conversion: ""+x :P


x.toString();

;)

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:27 • by Jeff (unregistered)
Looks a lot like there used to be constants in there instead of 0. Perhaps whatever reason they were adding and subtracting constants went away, so a developer swapped the constants out for zeroes.

The permanent and more-correct fix would be to remove the line altogether, but perhaps the developer thought he'd be going right back in and changing the constants again?

Is this code any LESS worthless? And yet somehow it seems more appropriate...

#define X_OFFSET 0
rect.X1 = min(rect.X1, rect.X1 - X_OFFSET);

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:36 • by ounos
224038 in reply to 224032
Smash King:
staying power:
third! And yes, that's what I yell in bed.
That's interesting! Your wife have some fun before you get home and you know it?

I suppose all you do about it is hope you can go home earlier the next day to scream "FRIST!!11!"? Care to give me your home address?

So when you get home, you actually know the number of men your wife has been with previously? How exactly? You count them?

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:37 • by javabrain (unregistered)
224040 in reply to 224033
Chris:
Mii:
reminds me of javascript to string conversion: ""+x :P


x.toString();

;)


oh, you mean: (x==null ? "" : x.toString())

somehow ""+x seems simpler...

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:40 • by Kevin (unregistered)
Probably it's the easiest way to convert to INT!

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:41 • by Calm Mint (unregistered)
224043 in reply to 224035
Jeff:
Looks a lot like there used to be constants in there instead of 0. Perhaps whatever reason they were adding and subtracting constants went away, so a developer swapped the constants out for zeroes.

The permanent and more-correct fix would be to remove the line altogether, but perhaps the developer thought he'd be going right back in and changing the constants again?

Is this code any LESS worthless? And yet somehow it seems more appropriate...

#define X_OFFSET 0
rect.X1 = min(rect.X1, rect.X1 - X_OFFSET);

You're halfway there. You've shown the next person that X_OFFSET may occasionally change, and happens to be zero for now.

To finish your fix, just take that special symbol and add it to the other lines, this kinda ties them together in a group to show that they're all part of the same idea:

#define X_OFFSET 0
#this.rect.X1 = Math.Min(this.rect.X1, this.rect.X1 - 0);
#this.rect.X2 = Math.Max(this.rect.X2, this.rect.X2 + 0);
#this.rect.Y1 = Math.Min(this.rect.Y1, this.rect.Y1 - 0);
#this.rect.Y2 = Math.Max(this.rect.Y2, this.rect.Y2 + 0);

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:43 • by secundum (unregistered)
224044 in reply to 224040
javabrain:
Chris:
Mii:
reminds me of javascript to string conversion: ""+x :P


x.toString();

;)


oh, you mean: (x==null ? "" : x.toString())

somehow ""+x seems simpler...

Would'nt that return the string "null" when x is null?

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:43 • by Zeal_ (unregistered)
224045 in reply to 224035
Jeff:

#define X_OFFSET 0

rect.X1 = min(rect.X1, rect.X1 - X_OFFSET);


Anyone into a contest for saying


rect.X1 -= X_OFFSET;


in the most rundabout way?

Bonus points for solutions the compiler can optimize back to


rect.X1 -= X_OFFSET;


:)

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:44 • by snoofle
224046 in reply to 224033
Chris:
Mii:
reminds me of javascript to string conversion: ""+x :P


x.toString();

;)

Not if x is null...

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:45 • by dpm
224047 in reply to 224035
Jeff:
The permanent and more-correct fix would be to remove the line altogether
I beg to differ. The *correct* action, regardless of whatever change you want to make to the code, is to COMMENT the damned thing so that future maintainers will understand why you're doing something so unobvious.

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:48 • by SMB (unregistered)
224048 in reply to 224044
secundum:
javabrain:
Chris:
Mii:
reminds me of javascript to string conversion: ""+x :P


x.toString();

;)


oh, you mean: (x==null ? "" : x.toString())

somehow ""+x seems simpler...

Would'nt that return the string "null" when x is null?

No, it would ont ;-)

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:48 • by Visage (unregistered)
224049 in reply to 224045
What if X_OFFSET is negative?

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:52 • by Andy (unregistered)
Perhaps there's a similar block of code immediately above or below this one where the offsets are non-zero and this code was presented in this way to make the similarities more obvious?

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:52 • by Scott (unregistered)
Maybe I'm missing something but to me TRWTF is that a line like:
this.rect.X1 = Math.Min(this.rect.X1, this.rect.X1 - 0);
Is basically the same as
this.rect.X1 = this.rect.X1;
Which essentially does nothing.

Re: Math.Min(x, x - 0) = ?

2008-10-22 08:57 • by Mr B
224053 in reply to 224051
Scott:
Maybe I'm missing something but to me TRWTF is that a line like:
this.rect.X1 = Math.Min(this.rect.X1, this.rect.X1 - 0);
Is basically the same as
this.rect.X1 = this.rect.X1;
Which essentially does nothing.


You're missing something. That's not TRWTF, that's the TOWTF (The Original WTF).

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:06 • by FragFrog (unregistered)
224055 in reply to 224038
ounos:
So when you get home, you actually know the number of men your wife has been with previously? How exactly? You count them?

Tsk, don't you know that honesty is the basis for any good relationship? Naturally she would tell him!

All joking aside, for older PHP versions it would yield more accurate results to use $value * 1 then intval($value), for a $value big enough. Commenting something like that would be kind'a trivial tho'

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:07 • by Sigh (unregistered)
224056 in reply to 224048
SMB:
secundum:

Would'nt that return the string "null" when x is null?

No, it would ont ;-)

Time for another le's'son on apo'strophe's. You put them before every "'s" and 'sprinkle them rando'mly wherever el'se you think they might fit. They do'nt actually mean anything, 'so it do'e''s'nt really matter e'xactly where they go.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:09 • by SlyEcho
224057 in reply to 224045
Zeal_:
Jeff:

rect.X1 = min(rect.X1, rect.X1 - X_OFFSET);


rect.X1 -= X_OFFSET;


This wouldn't work if X_OFFSET is negative.

The Other WTF

2008-10-22 09:13 • by TGV
Well, this looks like C# to me, and that certainly shouldn't have any effect there. At least, it has to be .NET (Math.Min, shudder), so perhaps it is VB version whatever and it has some obscure ... Nah, can't imagine.

The other thing about this code is the consistent use of "this.". I can't imagine that being necessary, unless this code was automatically translated from some old obscure VB code where the minus and plus zero do have an effect?

Anyway, whoever wrote that -0 < +0 should have his numeric coprocessor checked or stop working with 1-complement machines.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:15 • by weirded verber (unregistered)

yes, but sadly, with nothing to say.
do you scream "first!" in bed too?


all the time. Sometimes 0th

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:15 • by halcyon1234
224060 in reply to 224038
ounos:
Smash King:
staying power:
third! And yes, that's what I yell in bed.
That's interesting! Your wife have some fun before you get home and you know it?

I suppose all you do about it is hope you can go home earlier the next day to scream "FRIST!!11!"? Care to give me your home address?

So when you get home, you actually know the number of men your wife has been with previously? How exactly? You count them?


Of course not. He lives in a small community, so he just assumes 'all of them'.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:16 • by sadwings (unregistered)
224062 in reply to 224040
javabrain:

oh, you mean: (x==null ? "" : x.toString())

somehow ""+x seems simpler...


I prefer self-explanatory to simpler.

being clever < being clear

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:17 • by Playit Safe (unregistered)
'T'o 'b'e 's'u'r'e, 'y'o'u 's'h'o'u'l'd 'p'r'o'b'a'b'l'y 'p'u't 'a'n 'a'p'o's't'r'o'p'h'e 'b'e'f'o'r'e 'e'v'e'r'y 'l'e't't'e'r. 'T'h'a't 'w'a'y 'y'o'u''r'e 'b'o'u'n'd 't'o 'b'e 'r'i'g'h't 's'o'm'e 'o'f 't'h'e 't'i'm'e!

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:21 • by weirded verber (unregistered)
224065 in reply to 224060
halcyon1234:
ounos:
Smash King:
staying power:
third! And yes, that's what I yell in bed.
That's interesting! Your wife have some fun before you get home and you know it?

I suppose all you do about it is hope you can go home earlier the next day to scream "FRIST!!11!"? Care to give me your home address?

So when you get home, you actually know the number of men your wife has been with previously? How exactly? You count them?


if they're all hiding in the cupboard, then just count the cars on the driveway

Of course not. He lives in a small community, so he just assumes 'all of them'.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:21 • by Playit Safe (unregistered)
(Ten bucks to the first Windows user who can figure out how I did that without compiling code. And no, it wasn't click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click, paste...)

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:22 • by halber_mensch
224067 in reply to 224026
OhDear:
I have no idea how many variables I have multiplied by one.
x*=1;
In various languages it has solved a multitude of hurts.


Casting by accident solves the hurt, but it also leaves a silly looking scar.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:22 • by Mike (unregistered)
Scott:

Maybe I'm missing something but to me TRWTF is that a line like:
this.rect.X1 = Math.Min(this.rect.X1, this.rect.X1 - 0);

Is basically the same as
this.rect.X1 = this.rect.X1;

Which essentially does nothing.


It 'appears' to do nothing. however, we don't see the code for the property setter. Maybe the setter does more than just set a field value. Maybe it has other side effects.

The WTF is that the property setter has (presumably) undocumented side effects that confuse the API consumers.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:31 • by Old Coder (unregistered)
224069 in reply to 224057
SlyEcho:
Zeal_:
Jeff:

rect.X1 = min(rect.X1, rect.X1 - X_OFFSET);


rect.X1 -= X_OFFSET;


This wouldn't work if X_OFFSET is negative.

In that case, what you need is:
rect.X1 = min(rect.X1, rect.X1 - abs(X_OFFSET));

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:31 • by infamous_blah (unregistered)
224070 in reply to 224062
sadwings:
javabrain:

oh, you mean: (x==null ? "" : x.toString())

somehow ""+x seems simpler...


I prefer self-explanatory to simpler.

being clever < being clear


String(x) has the exact same behavior as ""+x but with better readability.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:32 • by return of the spelling nazi (unregistered)
224071 in reply to 224066
Playit Safe:
(Ten bucks to the first Windows user who can figure out how I did that without compiling code. And no, it wasn't click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click,
paste...)


In Python:
print "".join([i + "'" for i in string])

I was trying for a one-liner, maybe there's a better one, though.

No compilation needed. But maybe you did some fancy shell scripting or use an advanced text editor?

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:34 • by return of the spelling nazi (unregistered)
224072 in reply to 224071
Oops, you said Windows, so it's probably a cmd.exe or notepad trick. I don't know. Can I have ten dollars just for being me?

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:37 • by JoJo (unregistered)
224073 in reply to 224038
ounos:
Smash King:
staying power:
third! And yes, that's what I yell in bed.
That's interesting! Your wife have some fun before you get home and you know it?

I suppose all you do about it is hope you can go home earlier the next day to scream "FRIST!!11!"? Care to give me your home address?

So when you get home, you actually know the number of men your wife has been with previously? How exactly? You count them?


I didn't see him mention anything about men - you're making assumptions there!

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:38 • by NSCoder
224074 in reply to 224071
return of the spelling nazi:
Playit Safe:
(Ten bucks to the first Windows user who can figure out how I did that without compiling code. And no, it wasn't click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click,
paste...)


In Python:
print "".join([i + "'" for i in string])

I was trying for a one-liner, maybe there's a better one, though.

No compilation needed. But maybe you did some fancy shell scripting or use an advanced text editor?
I was going to guess by typing it, since it wouldn't take long (though I'm not a Windows user so I guess I'm not eligible for the ten bucks anyway.) I am still trying to figure out why anyone would do it by clicking and pasting.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:38 • by Mark V Shaney (unregistered)
I wanted to write that the code probably evolved from another code dealing with some border around the rect, but such code would have been developed:

a) with negative borders in mind
b) by someone that never heard of abs()

So, I guess it is a wtf evolved from a previous wtf...

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:39 • by Typing (unregistered)
224076 in reply to 224066
For such a short message, where it is unlikely that the need should ever arise again, I would suggest: bruteforce typing.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:40 • by Playit Safe (unregistered)
224077 in reply to 224072
return of the spelling nazi:
Oops, you said Windows, so it's probably a cmd.exe or notepad trick. I don't know. Can I have ten dollars just for being me?

I'm running on a virtual machine, so here's your virtual ten bucks.
(And, no, I didn't say I'm using Windows.)

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:43 • by Playit Safe (unregistered)
224078 in reply to 224076
Typing:
For such a short message, where it is unlikely that the need should ever arise again, I would suggest: bruteforce typing.

'A'a'h 'y'e's, 'b'r'u't'e 'f'o'r'c'e 't'y'p'i'n'g. 'A'n 'e'l'e'g'a'n't 's'o'l'u't'i'o'n 'f'o'r 't'h'o's'e 'o'n'e-'o'f'f 'r'e'q'u'e's't's 't'h'a't 'w'i'l'l 'n'e'v'e'r 'e'v'e'r 'c'o'm'e 'u'p 'a'g'a'i'n.

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:43 • by JoJo (unregistered)
224079 in reply to 224066
Playit Safe:
(Ten bucks to the first Windows user who can figure out how I did that without compiling code. And no, it wasn't click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click, paste...)


use Word. Type in the original line and use Find and Replace, more options, Special and replace 'any letter' (^$) with Find What Text (^&) and ' i.e. ^&'

replaces every letter with the same text + '

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:44 • by JoJo (unregistered)
224080 in reply to 224079
JoJo:
Playit Safe:
(Ten bucks to the first Windows user who can figure out how I did that without compiling code. And no, it wasn't click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click, paste, click, paste...)


use Word. Type in the original line and use Find and Replace, more options, Special and replace 'any letter' (^$) with Find What Text (^&) and ' i.e. ^&'

replaces every letter with the same text + '


oops! apostrophe should be before the ^& to match your example...

Re: Math.Min(x, x - 0) = ?

2008-10-22 09:47 • by Anonymous (unregistered)
This is one of the stupidest WTF posts ever. There have already been several reasonable explanations for why it could be done. Forgetting to leave a comment explaining something that may not even be worthy of a comment is hardly a WTF.
« PrevPage 1 | Page 2 | Page 3 | Page 4Next »

Add Comment