The worst case of math I've ever seen was when I worked retail. Lady bought a coat, was the wrong size (guess which way). Returned coat and bought another. That coat was the wrong color, she bought a 3rd coat. It was literally the Spongebob meme.
Now this was an online order so all the charges were listed. Since it was never a straight swap it was 3 charges and 2 refunds. I had to deal with her when she looked at her bill and said she paid 3 times for the same coat. No, she got charged 3 but she also got 2 refunds she paid 1 time for the coat. Still argues. So I do the math:
She paid for one <she agrees> returned it for a refund <she agrees> so its 0 <again agrees> She paid for second <she agrees> return it for a refund <she agrees> so we're still at 0 <again agrees> She paid for third <she agrees> so 0 plus the price of the coat is xyz <she agrees>
Me: Do you under stand?
Her: Yes
Me: Anything else?
Her: I'm still paying 3 times for the same coat
I'm pretty sure the head shaped dent in the desk is still there.
Wow that must've been insufferable to go through. I've had one customer tell me the register's total was wrong and that he was certain of it because he was math teacher. He must have been a terrible math teacher. At least he didn't make a fuss about it, just paid and left.
I had a math teacher that used to go on field trips to banks and point out calculators that had arithmetic errors (think things like truncating instead of rounding) this was of course with much older tech.
For expensive functions, when you are only expecting some values, it can still be very worth it. As a later optimization.
For example, let's say you want the sin of an angle, but you know it will only come in increments of 15 degrees. You make that a switch statement, and you just reduced a lot of computational complexity if you are expected to process a lot of angles.
Originally yes, but when you're optimizing later, you can replace it with narrow logic for narrow cases. The Doom codebase is full of those hacks, and it's one of the reasons why the game had such a good performance.
If it was some 25 years ago, this code would actually be ok. But I'm talking about now. And now, if you found out that the calculation actually was too expensive you would just add a build script that does this or you would write the part in C/C++ or any other solution better than writing it manually.
The argument stays. At the beginning, you can "do the machine that does the machine", but as you optimize later, you narrow things down. If I have an expensive calculation for a narrow use case, I don't want to learn how to code and compile C++, I'll just put a lookup table or something.
There’s also the Doom pseudo random lookup table which is just 1-255 in a random order. There’s a bloke who does a lot of Doom videos on YouTube with yellow backgrounds that go into some of the tricks.
The example he's giving of the "randomness" being the same with each playthrough isn't really a great example, because you'll get the same result when an RNG is used, so long as a fixed seed is being used.
Fixed seeds are still a common thing these days, so that you get deterministic playbacks, but I guess the main difference is that an RNG is being used now instead of a lookup table that had been generated from an RNG.
this video talks about the Super NES Doom port, here you will find more details about pre-computed vectors maths. It's very interesting, the programmer who made that port was incredibly skilled
RNG was expensive back then. So they just made it random "enough" so that the player wouldn't notice. These days an RNG is such a small expense that it's an afterthought.
They basically precompute any transcendental and geometric functions they need. Its still sometimes done today. Randoms were also sometimes computed but that was more because the RNGs at the time weren't all that good.
Computing a sine takes forever. Let's say an addition takes 1 cycle to execute, a multiply might take around 7 or 8. A sine on the other hand can take 75 cycles on a good day (50-155 for Zen 3, 40-100 for Crystal lake if you're interested). For graphics you have a lot of these, so you can imagine they would really slow down the program.
So instead the precomputed the values and stored them in a table. Properly implemented (as in not this thing) it takes around 5 or 6 cycles to get a value. Turns out that with a fairly small table (may 100 entries) you get reasonably accurate results.
One variation that you see nowdays is that you can tell the computer you want reduced accuracy for better speed. Basically you get the results twice as fast but you loose a digit or two worth of accuracy.
One big one was sine/cosine lookups, where you’re doing thousands (millions?) of trig calculations every frame and only need a certain precision. That was before GPUs of course.
actually, minecraft java edition still uses them for sin and cos. they calculate an array of values on initialization and use them throughout the game.
I do that now writing vhdl. Multiplication is expensive and luts are cheap. I mean, I don't hand write it I wore python scripts to write it, but the outcome looks a lot like this post.
Put n=4 in there. 270 degrees. Your rectangle would only have 3 right angles.
How to get the formula: put point inside your n-polygon. Connect it to vertices. You have n triangles. Sum of angles is 180*n minus 360 from the central point. 180n-360 = (n-2)*180
Haha, it's a whole thing. It's just the transcribers that choose to transcribe /r/badcode posts typically have to deal with some seriously tedious stuff.
Again, it has been stated multiple times here that OCR doesn't work that great on code pictures. Let's just appreciate the work the human probably did instead of lowering the hassle without any proof.
That just means you have to add extra code to handle the scenario when a value isn't returned. Loosely typed languages can be powerful because of their flexibility, but typically have a bad reputation because of people doing things like treating return values as optional.
Pretty easy to forget to add a return to a single code path in the function, I'd much rather be yelled at by the compiler when that happens than getting an unexpected undefined and having to debug to find out why.
I somewhat agree in most cases, but there are many places where early return is good, especially if you're making something with a low time budget such as games.
And if you structure the function well, it can be fairly readable
I mean that's because C and C++ just assume you aren't being an idiot, so if you have a number that you KNOW will be between 1 and 100 (like you just randomly generated it or already validated it) you can just skip the default case. Which also means when you are an idiot debugging will suck. On the bright side gdb works very well...
This has nothing to do with being an idiot. Imagine the code base changes the maintainer. Now this same maintainer changes something where 100 is not enough anymore. Now this poor guy has to determine why the heck the program does absolute garbage or randomly segfaults. Unlike js, c/c++ won't even give you the undefined value whose source you can search; it might crash; it might just continue operation with nonsensical numbers; you just don't know and that's very very dangerous. Undefined behavior like in c/c++ is expensive and a serious design flaw.
I mean it was the norm when C was created, and there are definitely cases where you need that minimal performance increase by not having the extra comparison, and if you are optimizing to that point you going to want to use C or C++ instead of a more modern language anyways for performance reasons.
Oh sure, I don't wanna blame this on the C committee from the olden times. The biggest problem is not that this behavior exists, but that it is not defined. Compiler makers can do what they want in this territory and this leads to code that might work just fine on one compiler but break on another. I argue that undefined behavior should never exist. The standard should specify what happens if you do such things. To your second i disagree. There are modern languages, like Rust, who don't suffer from these problems, because they have far better type systems, but deliver essentially the same performance. For most of this undefined behavior stuff it is furthermore stuff you want the compiler to enforce, afterwards you also don't need any runtime code to check it.
I disagree, this is stupid, but easily understandable. Imho the really bad code is the one that is so poorly written that you can not understand what it does.
Holy shit I looked at it and was like eww 🤮,then I opened it and went “who the fuck” and have been laughing ever since!!!!Must have missed the day they went over the other conditional operators
They didn't even use else if statements... WHY?! I don't understand what they're even doing. Is it multiplication? The function name seems to suggest it converts degrees to radians or the other way around. I don't know!
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/Sirius02, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
593
u/Notimecelduv Jul 22 '21
I mean, you wouldn't want the machine to get a multiplication wrong...
268
u/[deleted] Jul 22 '21
I mean... many years ago to improve performance in 3D games they used to write lookup tables with pre-computed math values... XD
231
u/Quanalack Jul 22 '21
return (n - 2) * 180;143
u/afa5e Jul 22 '21
Image Transcription:
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
28
u/junacik99 Jul 22 '21
Math is useless, they said
24
u/brj5_yt Jul 22 '21
If only there was a mathematical equation for something like this…
17
u/campbellm Jul 22 '21
So the next time anyone asks, "Do you need to be good at math to be a programmer?", point them here.
64
u/megamaz_ Jul 22 '21
JavaScript is nice like that. If you give 101, it don't give anything back.
C# will yell at you "NOT EVERY CODE PATH RETURN A VALUE"
12
u/[deleted] Jul 22 '21 edited Jul 22 '21
this looks like it was written by github copilot
11
u/valschermjager Jul 22 '21
better yet, switch statement ;-)
8
u/[deleted] Jul 22 '21 edited Jul 22 '21
Probably the worst piece of code I've ever seen here
Edit: verbosity-wise
4
u/React04 Jul 22 '21
They didn't even use triple equals...
5
u/AHeroicLlama Jul 22 '21
You could write code, which itself writes this code, in many many less lines...
2
u/virgindriller69 Jul 22 '21
It’s funny how the last few are wrong
2
u/Rajarshi1993 Jul 22 '21
Ah, the
(n - 2) * 180But what if they forgot the formula, googled it, and found an almanac?
2
u/TSReactReduxSASSDev Jul 22 '21
if(n) die();
2
u/paraverso Jul 22 '21
Bad code indeed, this should have clearly been a switch statement.
2
u/fazi_lost Jul 22 '21
should have used switch instead
2
u/[deleted] Jul 22 '21
looks like a copilot suggestion...
2
u/awshuck Jul 22 '21
Love it or hate it, this code is fast!
3
u/[deleted] Jul 22 '21
r/yanderetechnique
1
u/Sir_Webster Jul 22 '21
What usecase does this function fill? I understand what it does, but why?
1
u/oobrat2i30liga Jul 22 '21
Yo what the actual fuck. Who sits down to write all these lines of code and thinks " yes this is perfect"
1
u/Popernicus Jul 22 '21
LOL WHY
1
u/bsluzar Jul 22 '21
What calculation was this supposed to make?
1st thought: sum of angles in a polygon with N sides, correct?
1
u/Wu_Fan Jul 22 '21
To do this they must have literally done the math 100 times and still not realised
1
u/[deleted] Jul 22 '21
Holy shit I looked at it and was like eww 🤮,then I opened it and went “who the fuck” and have been laughing ever since!!!!Must have missed the day they went over the other conditional operators
1
u/PotaytoPrograms Jul 23 '21
holy shit
1
u/purbub Jul 23 '21
This is why we learn math
1
u/[deleted] Jul 23 '21
Probably was given a list of expected results and wrote a script to convert every line to an if statement and copy pasted it in.
1
u/NotitsTyrion Jul 24 '21
AAAAAAAAAAAAAAA
1
u/explodingMonkeydeath Jul 27 '21
I have a headache after looking at this
1
u/LightTranquility3 Jul 29 '21
in superior languages like C#, you can do
return n switch1
u/[deleted] Aug 05 '21
They didn't even use
else ifstatements... WHY?! I don't understand what they're even doing. Is it multiplication? The function name seems to suggest it converts degrees to radians or the other way around. I don't know!1
u/FlamboyantApproval16 Aug 08 '21
Optimised code :
return (n-2)*180;1
u/[deleted] Jan 16 '23
return (n-2)*180;
0
u/[deleted] Jul 22 '21
Bro, legitimate question. How'd you get this screenshot?
-8
u/techek Jul 22 '21 edited Jul 22 '21
Shouldn't this method also return a default value if none of the statements are true?
Why hasn't this been refactored into something more effective and future-proof?
0
u/[deleted] Aug 19 '21
[deleted] — view deleted comment
19
u/[deleted] Jul 22 '21
[removed] — view removed comment