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
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.
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.
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.
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
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).
Not true! The indentation makes it look like that, but when there are no braces, the else statement always associates with the immediately preceding if. So here the else is matched with the inner if, not the outer one
Agreed. I think trunc() is theoretically slightly faster than floor(), but either works fine. There's also std::modf, but unfortunately that's rather ugly to use.
Generally, you still need to check if your value is not infinite, but that's not needed here since we already have an upper bound.
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.
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.
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.
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.
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!
83
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 04 '21
Image Transcription: Code
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!
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...
227
u/n0sh0re Aug 04 '21
Let me guess... It takes numbers and determines if they are perfect squares.
87
u/davawen Aug 04 '21
I yes, let's use abs and THEN check if it's more than zero.
maf
31
u/[deleted] Aug 04 '21
cout << “no”
17
u/nonElitistProgrammer Aug 04 '21
The way this looks makes me wanna commit die
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.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?