If I were teaching a physics class that required some light coding, this is how I write the code because (surprise!) not everyone knows how to write/read code. This is very readable, even for a non programmer
Can you express the solution with just division? I think you'd need at least a loop or a floor function. The former would complicate the code, the latter would introduce a 'black box' into the code or may not be available in the language even.
This works everywhere except for when x = 0.4. Since it works everywhere else, I'd probably just leave in one if statement for the edge case. Although it could be a bug given they use less than in all the other elif statements.
Every single topic is not ranked on a single scale of difficulty. There are plenty of physicists who have a knowledge of physics far beyond what we could achieve who wouldn't understand a lot of basic programming functions, and vice versa.
Floor is not a rounding function. It does something different. I've never heard of floor calculations being useful in physics, but I am not a physicist, either.
There’s nothing wrong with a black box if it properly implements what it claims to without causing side effects. And to someone who’s just starting to learn how to program, pretty much every keyword is a black box anyway. And if you’re using a language that somehow doesn’t have basic math libraries for a physics class, you’re about to have much bigger problems
Thank you I thought i was going insane This is supposed to be a class and they aren't learning shit. That else if structure is really going to save them when they're calculating a derivative of an insane physics problem /s.
Which is really frustrating. I‘ve had to write emails to the authors about their code so often and they‘re always reluctant. I don‘t care how bad your code is, but it saves me hours of work if you just publish it somewhere.
I've been doing a lot of numerical relativity recently, and really small implementation details can significantly change the evolution of the system you're looking at, because some features are moderately chaotic even on a short timescale
While authors tend to be good at publishing their research, they're terrible at making code available, resulting in playing a fun game called "am I doing this wrong, are the authors exaggerating their results, or are my results different because of the inherent chaos of the system"
There's also a lot of ambiguous notation as well, or people misusing notation in ways that are unclear, and my goodness I'd just love to see what you actually implemented, rather than trying to translate your potentially (but understandably) typo heavy wall of equations
One of the even more fun things about it is that there's a lot of things about numerical stability that authors don't know that they don't know, so they're never described. Eg how someone performs a matrix inverse is pretty important, but by golly nobody's ever going to report that
In my experience its usually because the code is buggy and they know it. When I was doing my dissertation there was a task I needed to do that a few dozen papers claimed to have done. Only a few had code. A couple of them didn't work, and would never have worked. A couple of them had bugs that made them unreliable. Only two had working code that didn't have obvious bugs.
Never forget the famous economy paper that was undone because someone didn't do the right calculation in excel.
From my experience most CS papers that don't have code attached, aren't worth the time to read. Far too often they don't even bother describing the algorithms that they actually used, or how they implemented it.
Then its just like "That's cool man. Good for you." But thanks for wasting my time bragging about the results, without telling me how you do it.
I agree, but CS is one of the worst fields when it comes to reproducibility. Its only in the last few years that conferences decided that you would get a gold. I interned at a US national lab (won't name which one) in group that had some highly regarded work. In order to get a paper in somewhere they had to release the code to a tool they had been publishing on for years.They released the code. Only it was 5 years out of date. And the version they released had nothing to do with what was being published.
And yet these are some of the most highly regarded people in that side of CS. Its the major reason I decided to skip out on academia. Because it made it clear to me they were more interested about egos than actual computing or science.
People with formal education in mathematics and science usually suck at coding. Data scientists understand the statistical methods used to extract meaning out of data, but they do so with the most slapdash and unmaintainable python scripts I've ever seen. Like a CS freshman coding their first personal project quality
People with formal education in computer science or software engineering aren't necessarily any better until they spend some time in a commercial environment.
Academia is the haven for "it works on my machine" and "oh, yeah, sometimes it does that - just reboot and run it again".
Which is fine - in academia, you're trying to do something new, not write production code.
you're trying to do something new, not write production code.
Correct, I'm in the process of publishing some code for research. The original code was sloppy and had errors but I know what those errors are and how to check for them, and fix them. This doesn't make my results incorrect, it just means that it took human intervention with special knowledge that others can't be expected to have.
I taught myself arduino c and i have a weird style of using arrays and for loops to compress stuff, why? Because why the heck not, it increases the chances of it fucking up, for uniformity and expandability.
seriously academics should just hire developers to program their simulations and other apps, I'm sure they'll give much more optimized results than what they currently have.
In the general sense, its not worth the time, unless you are building a company out of it. Which most researchers/academics are not.
They've got a research grant to study X and Y, and the quality of the code has no impact on them getting paid or the outcome. Hiring a software developer would 10x their funding requirements, and they'd take 3 times as long, to get to the same research outcomes.
Basically research for the sake of research, where the goals are made up and the results don't matter.
Even in like, "edutainment" youtubers that use code, sometimes they'll have code that takes weeks to finish to demonstrate a point, and then when they show it, a viewer rewrites it in an afternoon to take like 12 minutes, and be more accurate.
Effective code in a research context (accurate, stable, fast) gets you more results more often.
Also, using division and modulus operations are actually quite slow and this code would likely be faster than using those operations.
On my machine, 100_000_000 ⌊x÷0.4⌋0.4 ops take 4.7s unoptimised, or 2.8s with -O3. Meanwhile 100_000_000 × 11 branches take 7.4s unoptimised, or 3.6s with -O3.
I did say 'would likely'. There isn't enough information in the OP code, or your code, to draw any conclusions.
It is obviously a MICRO optimization and this potential performance improvement isn't important in 99.999% of applications.
However it could be that in the context of a high performance application the compiler could see large performance benefits from things like branch prediction.
I regret adding that line since I knew it would bring out these "Technically on MY machine that isn't true" people.
MICRO optimization I regret adding that line since I knew it would bring out these "Technically on MY machine that isn't true" people.
I also think f(floor(x / 0.4) * 0.4) to be clearer than lots of branches. That's also probably what's written in the spec: "round x to the previous multiple of 0.4." Less brain power required.
For most apps true. But if you are in a high level math or physics and you are running a large simulation these small efficiencies add up.
I would use the '%' as well in anything I work with, but I don't think it is silly to call out another reason why the ifs might be getting used. That is to say it may be the teachers intent to avoid using division or modulus for a particular reason besides readability.
[The image is a photograph of a screen with code displayed on it. There is text beneath the code. The condition of the final elseif statement, and the end of the text beneath the code, are blocked out by a silhouetted object between the screen and the camera. The code reads as follows:]
h(x):= (
if x <= 0.4 then f(0)
elseif x < 0.8 then f(0.4)
elseif x <1.2 then f(0.8)
elseif x < 1.6 then f(1.2)
elseif x < 2 then f(1.6)
elseif x < 2.4 then f(2)
elseif x < 2.8 then f(2.4)
elseif x < 3.2 then f(2.8)
elseif x < 3.6 then f(3.2)
elseif x<4 then f(3.6)
elseif
It’s easier than you’re making it: just f(floor(x/0.4)*0.4) But I think these attempts prove the point that the original code is not bad at all: it’s much more readable and less error prone than a more compact formula.
And actually I did a mistake too, because I didn’t notice that in the first case there’s a <=, while in the other cases it’s just <. See, another proof that being more compact isn’t always the best solution.
Why is this method of floor, then division and multiplication better Than Just saying f(x-0.4)? For me saying x-0.4 would be the first approach. Is it about something with the representation of floats that I miss?
f(x-0.4) will represent a continuous line, while what we are looking for is a step function. For example, every value of x between 0 and 0.4 must give 0, while x-0.4 always returns a non-zero value.
So…. Is this translation table mimicking gravity (or other physical properties) on a different planet or something?
Since this seems to be a linear proportion that is offset by 0.4, why couldn’t you just reassign f(x-0.4)? There is probably some reason for this I am sure.
Dude, I’m softest engineer and idk wtf those statements are doing. What’s f(x)? What’s h(x)? Are we saying if X is less than something then h(x) evaluates to whatever the function “f” is?
55
u/[deleted] Apr 21 '23
for those of you wondering what this is, we were using maxima to do numerical integration
639
u/IBJON Apr 21 '23
If I were teaching a physics class that required some light coding, this is how I write the code because (surprise!) not everyone knows how to write/read code. This is very readable, even for a non programmer
708
u/The_Northern_Light Apr 21 '23
i hate to tell you this but the further you go into physics the worse the code gets
there's a reason academics don't release their code (usually)
166
u/_kashew_12 Apr 21 '23
I would make fun of them, but if you can teach physics you’re 1000000% smarter.
96
u/Betamaxxs Apr 21 '23
I hate to break it to the elitists out there. This is reasonable code, especially for teaching.
Sure there are spacing issues, but I suspect they aren't using a linter. Also...this is obviously not production code.
The purpose of this code is to TEACH and I suspect this isn't a class on advanced programming techniques.
Also, using division and modulus operations are actually quite slow and this code would likely be faster than using those operations.
27
u/mpfthprblmtq Apr 21 '23
Just for fun, let’s not
27
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 21 '23
Image Transcription: Code
[The image is a photograph of a screen with code displayed on it. There is text beneath the code. The condition of the final elseif statement, and the end of the text beneath the code, are blocked out by a silhouetted object between the screen and the camera. The code reads as follows:]
[The text beneath the code reads:]
Just for fun, let's
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
8
u/MasiTheDev Apr 21 '23
Truly r/yanderetechnique
27
u/kr4t0s007 Apr 21 '23
Was the police called?
41
u/[deleted] Apr 21 '23
If there‘d only be a way to store multiple numbers in something like a list and then programmatically access them by i and i-1…
4
u/deepwaterpaladin Apr 21 '23
What language is this?
3
u/JGHFunRun shameless Apr 21 '23
Language name?
3
u/7decimals Apr 21 '23
What would be the alternatives?
4
u/AdjustedMold97 Apr 21 '23
I mean, it’s not all that bad though. a compiler would probably optimize it anyway, this is just more readable for people who aren’t in CS
7
u/noxylliero Apr 21 '23
what's problem with this ? should have used switch ?
or it should have been h(x) := ( x - 0.4 ) ???
2
u/No-Adeptness5810 Apr 21 '23
isnt it just floor(x / 0.4) * 0.4 i came up with this in my head
2
u/TheFridgeOfSecrets Apr 22 '23
Yanderedev if he knew how to do math
2
u/MistaLOD Apr 23 '23
x = floor(x * 2.5) / 2.5
3
u/robin_888 Apr 21 '23
1
u/uvero wicked Apr 21 '23 edited Apr 21 '23
f((floor(x/0.4) - 1)*0.4)
1
u/Pauliboo2 Apr 22 '23
Surely simplify to
If (x - 0.4) <0 then f(0) else f(x-0.4)
1
u/Financial-Year-8261 Apr 21 '23
B
1
u/hopeless-nerd Apr 21 '23
So…. Is this translation table mimicking gravity (or other physical properties) on a different planet or something?
Since this seems to be a linear proportion that is offset by 0.4, why couldn’t you just reassign f(x-0.4)? There is probably some reason for this I am sure.
1
u/deniercounter Apr 22 '23
That’s a beginner course. It makes sense to show the bad to achieve the better.
1
u/filpglupman Apr 22 '23
Add another elseif and make it chemistry
1
u/[deleted] Apr 23 '23
Dude, I’m softest engineer and idk wtf those statements are doing. What’s f(x)? What’s h(x)? Are we saying if X is less than something then h(x) evaluates to whatever the function “f” is?
1
u/sugarsnuff Apr 23 '23
Does this work?
if x > 4: return -1
i = 0
while ( i < (x - 0.4) ): i += 0.4
f(i)
1
u/MeowkyCharliecatt Apr 23 '23
Return x-0.4
1
u/Bishnukt Apr 23 '23
Once my friend from physics told me they also do coding in their classes, now I know what he meant :).
1
u/rco8786 Apr 23 '23
This seems incredibly legible to me. Like even someone who has never seen code before could probably figure out what this is doing.
1
u/Sanduhr32 Apr 24 '23
h(x) := f(int(x/0.4)*0.4) :)
1
u/Macko2YT_ Apr 21 '23
f=x-x%0.4
1
u/Oheligud Apr 22 '23
Did the thought of simply doing f(x-0.4) just not occur to them?
-2
u/No-Cover4152 Apr 21 '23
If it works it’s good code.. u r doing physics and not programming.. 🤣🤣🤣
0
u/SaltyWahid Apr 21 '23
This is too triggering.
0
u/zealouszorse Apr 21 '23 edited Apr 21 '23
h(x) = min( abs( max( 0.0, floor( 2.5*x )/2.5 ) ), 3.6 )Avoids floating point errors and satisfies the boundary conditions
-4
u/[deleted] Apr 21 '23
thinms could have been done using a loop
-5
u/popdemtech Apr 21 '23
The internet just got smart
0
u/[deleted] Apr 21 '23
[deleted] — view deleted comment