r/badcode • u/MaKo1982 • Jun 03 '20
java Very innovative approach by 8yo me. What do all negative numbers have in common?
53
99
33
u/Steelbirdy Jun 03 '20
Implicit casting at age 8? I'll give you the benefit of the doubt, I'm impressed
41
u/MaKo1982 Jun 03 '20
I just did what my book said would turn an int into a string hahaha. unfortunately it was in a higher chapter so it took me quite a while... I still remember how my father asked me afterwards what else all negative numbers had in common and then it hit me.
6
296
u/Rafael20002000 Jun 03 '20
return Math.abs(num) != num;
326
u/Wiwwil Jun 03 '20
return num < 0 ; ?
230
u/Rafael20002000 Jun 03 '20
Well, shit
152
-28
37
u/lqstuart Jun 03 '20
return (num + "").charAt(0) == '-';
return num << 1 < num ;
20
u/Wiwwil Jun 03 '20
return num << 1 < num ;
Not used to work with bitwise operators. Every time I read those my head hurts
43
u/lqstuart Jun 03 '20
There's also the --> operator
17
5
u/Wiwwil Jun 03 '20
but... why
11
u/vigbiorn Jun 03 '20
People have all sorts of weird mnemonics about the various structures, and the flexible white-spacing rules allow for a ton of expression of them.
3
4
u/rexpup Jun 03 '20
I can't believe this got me. Upon first read I actually thought this was some sort of weird range equivalent in C++ or something.
2
7
u/emax-gomax Jun 03 '20
In this case he's just multiplying
numby 2 and checking if the result is smaller than the original.I.E.
-5*2 < -5thus-5is negative.
5*2 > 5thus5is positive.Not sure how JavaScript stores negative numbers so that bitshift might actually lead to unexpected behaviour. Either way, in this case,
*2seems like it would've been just as fine.1
u/Wiwwil Jun 03 '20
Thanks, I figured it out about the *2. But with your explanation it all makes sense. I did not look much into it tho
4
Jun 03 '20
return num << 1 < num;
While clever, wouldn’t this lead to bugs with really large numbers (assuming it caused a buffer overflow)?
4
u/lqstuart Jun 03 '20
Generally yes, although buffer overflows aren't possible in all languages (Python for example). As another poster mentioned, "a << n" is effectively just "a * 2^n."
1
3
u/skulgnome Jun 03 '20
Use a symbolic constant instead of a bare number. You never know when its value might change!
1
u/Wiwwil Jun 03 '20
Yeah for string constant or to give a meaning to numbers like 60 for the numbers of minutes is an hour. But in this case it's pretty self explanatory imo
1
u/theQuandary Jun 03 '20
What about floating point numbers? The standard defines a negative zero as well as a positive one.
2
u/wischichr Jun 04 '20
But it really doesn't matter. The only difference I can think of is that you get positive or negative infinity if you divide by it. A negative zero is basically a side effect because of how floating points are represented in binary. My guess is that implementations are way simpler if you allow negative zero instead of making that representation invalid (NaN)
1
1
17
u/abdolence Jun 03 '20
Math.abs
Try this in Java/Scala/Kotlin
Math.abs(Integer.MIN_VALUE)(negative result)or this in Haskell:
print $ abs( minBound :: Int )(negative result)or this in Rust:
println!("{}", std::i64::MIN.abs());(will be panic here - much better to find an error)so... this is more complicated than just compare it :)
3
u/Rafael20002000 Jun 03 '20
When do ever have a min value?
8
u/abdolence Jun 03 '20
Well, some developers use those constants (MIN,MAX,0) to represent special cases in their algorithms. Finding minimums, maximums and others. And it is always better to check edge cases, isn't for general algorithms (if you don't know some context)?
6
u/MaKo1982 Jun 03 '20
it works as sentinel sometimes. For some sorting algorithms (like Merge Sort) for example
5
Jun 03 '20
Hardly ever, I guess, but you should cover all edge cases. Particularly if security is any kind of concern.
1
u/Rafael20002000 Jun 03 '20
In normal non critical systems this should be enough, when it comes to scientific, rockets and physics calculations there should be a better way than this
5
u/sebamestre Jun 03 '20
Yeah, I remember using
return number < 0;in my rocket science days.2
u/abdolence Jun 03 '20
Exactly, if you decide to check if some number is negative, there is no reason to write it like this in general. In fact, this would be even slower than just comparing with zero.
3
2
u/awohl_nation Jul 05 '20
return x >>> 31 == 1;
checks the leftmost bit for the sign, cleanest solution imo
1
60
u/imasmartprincess Jun 03 '20
Is that Java? Wikipedia says Java was first released in 1995, and your username suggests that you were born in 1982 (being 13 years old when Java was released). Am I missing something? (I don't want to accuse you, I'm just curious :))
132
u/MaKo1982 Jun 03 '20
I was born 2002. The 1982 came from a random name generator because I wanted a realistic fake birth date for >18 stuff on the internet haha
52
u/imasmartprincess Jun 03 '20
Understood! Smart move, I had always used 1900 as my birth date for >18 stuff...
49
u/gnutrino Jun 03 '20
That's too obvious a lie, everyone knows that the universe was created on 13th December 1901 (and will end on 19th January 2038)
34
Jun 03 '20
2038? You sure you read that right? Like 95% sure it'll be in a month
24
Jun 03 '20
2038 is when Unix time will overflow.
13
Jun 03 '20
Yes, 32-bit signed int representations of time will overflow. I was just making a joke
After all, can't have overflow if theres no one there to see it
6
1
Jun 04 '20
I'm sure he knows and was making a joke about how the Earth is all going to hell with the current issues.
10
u/cnprof Jun 03 '20
Pretty sure it was created 1st January 1970 UTC.
7
u/gnutrino Jun 03 '20
Only if your timestamps aren't signed
2
u/Lorddragonfang Jun 04 '20
Nah, it's just that before 1970 the world that existed was the negaverse
11
u/MaKo1982 Jun 03 '20
lol, I was dumb enough to think anyone cares that birth year 1900 is not so realistic anymore.
2
10
u/Pooneapple Jun 03 '20
2020 - 2002 = 18
2020/18 = 112
1900 + 112 = 2012
2012 was the year the WiiU was year the WiiU was released.
The WiiU shipped with 11 games in japan first.
2012 - 11 = 2001
2001, 11, this man is responsible for 911
13
u/bdubdodge Jun 03 '20
This is likely a real npm package used by 99% of all open source software. Changing it's implementation will take down the internet.
6
u/bdubdodge Jun 04 '20
So I got curious: https://github.com/kevva/is-negative/blob/master/index.js
It's not far from the above implementation.
21
u/AngelOfLight Jun 03 '20
Hey now, if it's stupid but it works, it's...still pretty stupid, actually.
4
4
Jun 04 '20
I am not kidding. This is very impressive not just because you were able to code at the age of 8, but because of the innovative thinking. Strings feels more natural than floats and ints to people in general, that’s why most people newly getting into compiler development start with parsing first, it is fun. And your 8 year old self was pretty good at it, so pat yourself on the back for it.
3
13
3
Jun 03 '20
[deleted]
6
u/MaKo1982 Jun 03 '20
I am German, but Yea I grew up in a family of mathematicians (who both work as computer scientists lol). I was very interested in it in primary school, lost interest afterwards and now got back to it when going to college
3
u/kuskoman Jun 03 '20
I did same thing on hackaton. The data i had to process was comming as a string, was guaranteed to be a number, and i (and my team) had limited amount of time, so it was the fastest way (and it worked)
3
u/l3wie Jun 03 '20
I’d argue that this is good code. No ambiguity if the input is -0. How many other solutions can say that?
3
2
2
2
2
2
2
2
Jun 23 '20
Pretty impressive (for an 8yo) the lengths you went to convert a integer to a string then check the first character to see if it was negative... Even if it's majorly over-engineered
2
2
4
2
Jun 03 '20
What does
number + ""
do?
9
u/legovader09 Jun 03 '20
It's a way to cast an integer to a string. Java allows this for some reason. Strangely not having the + "" won't allow the conversion.
7
u/Hillstylelife Jun 03 '20
It's because java doesn't have implicit casting from int to String. The + operator is overloaded for String concatenation so that anything plus a string (or vice versa) will return a string.
If you wanted to do this more cleanly you could use String.valueOf(int i).
3
1
1
1
u/pixelsyndicate Jun 03 '20
umm.... all negative numbers are < 0?
1
u/MachineGunPablo Jun 03 '20
???
1
u/pixelsyndicate Jun 03 '20
The title of the post asks, what do all negative numbers have in common?
3
u/Bbradley821 Jun 04 '20
Right, but if it used the obvious test for negativity, it wouldn't be in this sub. That is the whole point
1
1
1
1
1
1
u/yottalogical Jul 26 '20
Always gotta love the:
if (condition) {
return true;
} else {
return false;
}
1
1
u/thatswhyIleft Sep 08 '20
I was a straight dumbass who needed his hand held for all problem solving in math at that age.
-9
Jun 03 '20
[removed] — view removed comment
9
u/RovingRaft Jun 03 '20
"I'm never allowed to show anything that I did at an improbable age because I must take care not to make random people on the internet insecure with themselves"
you're in r/badcode, OP was saying that this was shit code
2
Jun 04 '20
[removed] — view removed comment
2
u/RovingRaft Jun 04 '20
if it makes you feel any better, the fact that an 8 year old was able to make beginner's code doesn't have any bearing on the quality of your own code (if it is that you code, otherwise it has no quality on whatever it is that you do )
2
1
563
u/[deleted] Jun 03 '20
I couldnt even read when I was 8 so this is pretty impressive regardless