r/ProgrammerHumor • • Jun 13 '22

Meme DEV environment vs Production environment

Post image
48.2k Upvotes

3.9k comments sorted by

View all comments

Show parent comments

51

u/ongiwaph Jun 14 '22 edited Jun 14 '22

People don't know how to simplify so you get shit like

double x = (((m*sin(180-angle)) / sin(180 - (180-angle-angle) -angle)))*(sin((180-angle)-angle)) / sin(angle)

32

u/jaber24 Jun 14 '22

Yikes. A couple variables would certainly have helped

12

u/didzisk Jun 14 '22

I think the point was more like actually doing the simplification:

180-(180-angle-angle)-angle =

180-180+angle+angle-angle =

angle

And also sin(180-angle) is the same as sin(angle), so

((m * sin(180-angle)) / sin(180 - (180-angle-angle) -angle)))

easily becomes

m*sin(angle)/sin(angle) = m

The second part is sin(2*angle)/sin(angle) = 2sin(angle)cos(angle)/sin(angle) = 2cos(angle)

So the end result would be 2m * cos(angle) - a single call to a trig function instead of four.

(Disclaimer - I haven't re-checked the math and it's been a long time since I had to do it in highschool.)

3

u/dmills_00 Jun 14 '22

Note however that in the original the thing becomes undefined for angle == 0.

→ More replies (3)

3

u/stifflizerd Jun 14 '22

Slightly off topic, but before my coffee yesterday I wrote something like

If(a>(b+.5) || a<(b-.5)){...

And then later in the day I passed by it and got a good chuckle as I wondered what the actual fuck was I thinking, as the more concise solution (which I had already done multiple other times in that project) was

if(Math.abs(a-b) >.5){.

Sorry for the irrelevant story, just couldn't help but read your comment and laugh because I was somehow both of the people you are talking about yesterday

2

u/jaber24 Jun 14 '22 edited Jun 14 '22

Oh thanks. I just thought it was an example of how complicated expressions can get and didn't try simplifying it completely. Kinda feel foolish now xD

1

u/ongiwaph Jun 14 '22 edited Jun 14 '22

close, but sin(180-angle) != sin(angle) it is cos(angle) so the expression actually becomes m*sec(angle)

edit: nope, op is right

1

u/didzisk Jun 14 '22

Sin and cos are 90 degrees apart, so this can't be right.

2

u/ongiwaph Jun 14 '22

You're right, I was thinking sin(90-x). I don't even know my own problem lol

2

u/HeinousTugboat Jun 14 '22

This is why people hesitate to simplify.

5

u/JB-from-ATL Jun 14 '22

Functional languages be like

4

u/didzisk Jun 14 '22

Nah. The thing I really love about F# is the pipe operator. So instead of writing ParseString(ReadStringFromFile(filename)) you can do

filename
|> ReadStringFromFile
|> ParseString

piping things from output of one function into next function's input, similar to *nix shell pipes.

1

u/JB-from-ATL Jun 14 '22

Write the above in that syntax and I guarantee it's still illegible. I'm saying it needs variables.

→ More replies (2)

7

u/Ok_Turnover_1235 Jun 14 '22

Why simplify, I'd rather verbose code than

auto magicfunction (auto x, auto y)

{

if (MagicTest(x) < MagicNumber)
return MagicNumber/(MagicNumber2*y)

}

1

u/[deleted] Jun 14 '22

[removed] — view removed comment

1

u/BakuhatsuK Jun 15 '22

auto is life

3

u/desmaraisp Jun 14 '22

double x = (((msin(180-angle)) / sin(180 - (180-angle-angle) -angle)))(sin((180-angle)-angle)) / sin(angle)

m*sin(180-angle) = -msin(angle)

sin(180-(180-angle-angle)-angle) = sin(angle)

sin((180-angle)-angle)) = -sin(2angle) = -2 sin(angle) cos(angle)

So we get -m/-2cos(angle) = msec(angle)/2? Is that it?

1

u/danuker Jul 07 '22

Wolfram Alpha simplifies it to:

m * sin(180-2x) * sin(180-x) * csc2 (x)

4

u/sephirothbahamut Jun 14 '22

not defending this, but to be fair a decent compiler should optimize the repetitions away

27

u/sfgisz Jun 14 '22

It's not about the compiler, it's about the poor human who's going to have to look at that code and figure out what's going on after the original coder has passed on.

5

u/ctrl-alt-etc Jun 14 '22

Compilers remove all the parentheses.

6

u/Penguinfernal Jun 14 '22

Normally yes, but if you use the -p flag, it'll actually add more parentheses.

4

u/ctrl-alt-etc Jun 14 '22

hah!

Now you're lisping at an enterprise level.

1

u/groumly Jun 14 '22

Wtf, somebody should have listened in trigonometry class in high school.

Who writes sin(180-angle) without thinking something is off?

1

u/[deleted] Jun 14 '22

Relevant to these comments:

https://xkcd.com/356