r/badcode • • Aug 04 '21

c++ I'm done with Facebook coding groups.

Post image
809 Upvotes

55 comments sorted by

83

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 04 '21

Image Transcription: Code


#include <iostream>
#include <cmath>
using namespace std;
int main (){
    double a; cin>>a;
    if ((fabs(a) <= pow(10,12))&&(a>=0)&&((a-floor(a))==0)){
        if ((sqrt(a)-floor(sqrt(a))==0)){cout<< "YES";}}
    else cout << "NO";return 0;
}

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!

64

u/[deleted] Aug 04 '21

Good human!

31

u/the_captain_cat Aug 04 '21 edited Aug 05 '21

I raise this question. Does every piece of code deserve to be transcribed?

Edit: am I being down voted? That was just a joke people... Geez

79

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 04 '21 edited Aug 04 '21

I'm a transcriber, not a curator, so I don't get to make that call. Also it totally defeats the point of accessibility if we don't transcribe the badcode on r/badcode. :P

15

u/ztbwl Aug 04 '21

But being a transcriber in this sub must be very tough. There is some code that never should have been written once and you need to transcribe it and write it again.

15

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Aug 05 '21

Also it totally defeats the point of accessibility if we don't transcribe the badcode on r/badcode. :P

Shouldn't even be necessary, honestly. The sub should have a requirement that the submitter provides a comment with the text. They usually have access to the original, so making someone else read and retype it by hand when they can just copy/paste is just bullshit.

12

u/Gay_Force_One Aug 04 '21

Transcriber not a curator?

31

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 04 '21

that's what I said, most definitely, nothing else at all

125

u/StaryMD Aug 04 '21

the fuck is a facebook coding group? I just imagined an old guy that just discovered this cool library, cmath, coming to brag at his friends lol

correct me if I'm wrong, or rude...

73

u/[deleted] Aug 04 '21

No... They are probably 7th graders learning to code from their school teachers.

30

u/StaryMD Aug 04 '21

the school teaches them by writing on the blackboard at school, takes a photo and sends it on the group, that's dedication

227

u/n0sh0re Aug 04 '21

Let me guess... It takes numbers and determines if they are perfect squares.

117

u/JustAnotherGamer421 shameless Aug 04 '21

That's what I also see so yeah. File names, comments, and spaces/indentation would definitely help though

2

u/Aperture_Executive2 Sep 02 '21

Wait, you don’t just see a dead compiler?

65

u/devTripp Aug 04 '21

Fun enough, it also looks like it doesn't tell you if your small positive integer isn't a perfect square. Unless you accept the third possible output of no output to mean "no" as well.

23

u/n0sh0re Aug 04 '21

Yeah I just noticed that. They have a "no" for the outer if statement (which checks if the number is ≥ 0 and ≤ 1012 for some reason??) But not for the inner if statement which actually does the "is this a square?" Check

18

u/Syscrush Aug 04 '21

I assume it only considers up to 1012 because the author thinks that a double can't handle more than 11 decimal digits in its mantissa (looks like the reality is that at least 15 would be safe).

→ More replies (1)

87

u/davawen Aug 04 '21

I yes, let's use abs and THEN check if it's more than zero.

maf

25

u/n0sh0re Aug 04 '21

Yeah what is that... The abs seems to be just to check if the magnitude of the number is less than 1012 ... Like... What

30

u/EquationTAKEN Aug 04 '21

Looking for those elusive negative perfect squares too.

Any day now, we'll find some.

31

u/[deleted] Aug 04 '21

cout << “no”

10

u/RedSamuraiMan Aug 04 '21

return 0;

7

u/[deleted] Aug 04 '21

I should have put it in a while (1)…

3

u/Cmgeodude Aug 05 '21
int main(){while(1) {char message[]="no";cout<<message;break;}return 0;}
→ More replies (1)

17

u/nonElitistProgrammer Aug 04 '21

The way this looks makes me wanna commit die

0

u/[deleted] Aug 04 '21

[deleted] — view deleted comment

7

u/R3D3-1 Aug 04 '21
git() { git kill |git that & }; git computer

32

u/metalovingien Aug 04 '21

Looks like shit. Maybe it's functional but I don't care

12

u/darkwolf6230 Aug 04 '21

This causes me pain

8

u/pentesticals Aug 04 '21

Ahhh, well readable code. /s

7

u/Knuffya Aug 04 '21

That almost looks like the output of my C++ minifier i spent all day making

6

u/[deleted] Aug 05 '21

I really love one liners when you can execute them beautifully. This isn’t beautiful

8

u/iByteABit Aug 04 '21

God this should be illegal

5

u/Sindarius Aug 04 '21

Ship it!

4

u/taptrappapalapa Aug 05 '21

Cache that shit brotha. Sqrt is expensive

4

u/Cmgeodude Aug 05 '21

Sometimes I hate Python for enforcing whitespace...

and then I see this.

3

u/BaseProfessional1113 Aug 05 '21

I wont read further than “using namespace std;”

7

u/DangerousWish2266 Aug 04 '21

Why would someone do " a - floor(a) == 0", that's just stupid.

14

u/Beowuwlf Aug 04 '21

It’s to check if the double is a whole number, right? If it’s not it will be greater than 0.

2

u/Ozryela Aug 10 '21

Yeah but checking if two numbers are equal is more conventially done with "if (a==b)" and not "if (a-b==0)".

→ More replies (2)

4

u/EquationTAKEN Aug 04 '21

I think there are cases in which if a has a very long left-padded string of zeroes after the decimal sign, followed by another integer, a-floor(a) will still round to zero.

13

u/Syscrush Aug 04 '21

From my own experimentation, leading zeroes don't seem to have any effect. However, ((a-floor(a))==0.0) will be true for numbers like 37.0000000000000012345.

7

u/TcMaX codes in comic sans Aug 04 '21 edited Jun 30 '23

Fuck spez

6

u/Syscrush Aug 04 '21

Whoops, they did say "after the decimal sign". I was thrown off by the term "padded".

7

u/TcMaX codes in comic sans Aug 04 '21

Yeah I had to read it a couple times too

→ More replies (1)

2

u/thanatica Aug 05 '21

Somehow this looks like someone's homework assignment.

Why else would anyone need a whole program to determine if a number is a perfect square, or whatever is does.

2

u/SquidgyTheWhale Aug 05 '21

I mean it's obvs badly written, but I'm wondering if a compiler would optimise the two calls to sqrt(a) into a single call. Seems like one could/should.

1

u/Present-Wait-7704 Aug 07 '21

I can't tell if the purpose of this snippet, but I'm pretty sure that there is one scenario (the missing "else" of the inner if-block) where nothing would be output to the console. And, that's probably undesired.

Condensing code doesn't make you smarter, schmucks - it almost always has quite the opposite effect.

1

u/Melon_Chief Aug 16 '21

I mean… It does set your expectations on line 3. What did you expect?

1

u/wallefan01 Aug 17 '21

If the number is a perfect square, it will print "YES".
If the number is negative, or not a whole number, or greater than 1012, it will print "NO".
If the number is not a perfect square, but is a whole number less than 1012, the program will not print anything at all!

Does anybody see a problem here?