r/ProgrammerHumor • • Feb 07 '19

other Spotted on GitHub 🤓

Post image
57.0k Upvotes

934 comments sorted by

View all comments

Show parent comments

105

u/TheOldTubaroo Feb 07 '19 edited Feb 07 '19
days_late*100/days_deadline/100

Isn't that pointless? Aren't all js numbers floats, so you don't need to worry about integer division?

And also clamping the opacity to [0,1] and then checking if opacity is greater than 0 and less than 1...

321

u/shitmyspacebar Feb 07 '19

You mean you don't add redundant checks just in case the laws of mathematics suddenly change?

64

u/TheLuckySpades Feb 07 '19

I'm studying math and when programming by my own I'll have a bunch of redundant checks, even if it's just implementing something I just proved to always work.

If I'm stressed, say an exam, I'll skip any and all checka, even if I'm not even sure if the math is correct and won't accidentally summon a great old one.

27

u/joe579003 Feb 07 '19

If in the process of compiling a choir starts chanting in Latin and an organ starts playing out of thin air you know you done fucked up.

15

u/TheLuckySpades Feb 07 '19

Depends o if the unearthly light is coming from above or below.

Above you have a chance it's thw angelic hostcome to take away your code and they might spare your soul.

7

u/Katyona Feb 07 '19

Or as it's compiling you hear laughter/boss music of any sort coming from your machine, that's usually the appropriate time to give up hope.

3

u/XarrenJhuud Feb 07 '19

one winged angel intensifies

→ More replies (1)
→ More replies (3)

159

u/UsedCondition1 Feb 07 '19

Well, if we are talking about the implementation of those laws in javascript... yes, yes you should.

77

u/Finchyy Feb 07 '19

Ah, JavaScript... where 2 + 2 is 4 but also sometimes 4.00000000001...

32

u/Layers3d Feb 07 '19

2+2 = 5 if the party says so.

23

u/thecravenone Feb 07 '19

And also for large values of 2

6

u/wirelyre Feb 07 '19

Don't worry, it's not! It's always safe to manipulate integers up to the full value of the significand! In JavaScript, which uses IEEE-754 binary64, which effectively corresponds to 54-bit integers.

Bit operations on JS numbers cast them to 32 bits first, so a lot of people only think about the embedding of signed 32-bit two's complement. But through plain old addition, subtraction, multiplication, and division joined with Math.trunc, you can get a lot more precision!

16

u/an_opinionated_moron Feb 07 '19

In javascript 2 + 2 = '22'

3

u/Lorddragonfang Feb 07 '19

I mean, that kind of error happens in every language. It's not JavaScript specific, it's part of the IEEE spec.

And integer addition (the mathematical category, not the type) will never have that problem, by the way. The example you're looking for is 0.1 + 0.2 == 0.3 -> false

9

u/Absbshshshshshshhs Feb 07 '19

***Ah every single language that has floats

but yes internet points for shitting on javascript

4

u/Finchyy Feb 07 '19

Internet points for sharing in bemusement of the quirks of a programming language on a related subreddit*

3

u/KinterVonHurin Feb 07 '19

I've never multiplied two integers and got a float in any language unless I type cast in which case I would get 4.0 and not 4.00000000001

7

u/Zephirdd Feb 07 '19

The basic law of JavaScript numbers is that numbers as all double precision floating point numbers (aka. A "double")

Sure in most languages the default is an integer, but in JavaScript that's not the case. JS has a ton of fuck ups, but numbers are probably the most consistent part of it all.

3

u/KinterVonHurin Feb 07 '19

Well sure but the guy I responded to said that every language with a floating point type has this problem and that isn’t the case.

→ More replies (4)

14

u/JasonDeroelo Feb 07 '19

This is my fear

13

u/TexMexxx Feb 07 '19

Well when developing in a big team (or teamS) some redundant checks might not be the worst idea. Someone WILL break your interface and if you have some checks in place the culprit MAY see what he is doing wrong... MAYBE! (OK, chances are good he just delets your check) ;)

23

u/TechnicallyAnIdiot Feb 07 '19

"Sir, do you know why I stopped you today?"

"No?"

"You were dividing in an addition only zone. You really need to know the laws of mathematics."

"Oh jeez, I didn't see the sign, when did they put that in? This won't make me lose my mathematical operator license will it?"

7

u/nininini2 Feb 07 '19

I usually don't even add the checks I'm supposed to. And let's not even discuss handling exceptions.

→ More replies (1)

30

u/inu-no-policemen Feb 07 '19

And also clamping the opacity to [0,1] and then checking if opacity is greater than 0 and less than 1...

Fun fact: The browser already does the clamping.

https://developer.mozilla.org/en-US/docs/Web/CSS/opacity

Any value outside the interval, though valid, is clamped to the nearest limit in the range.

So, -1 is just like 0 and 42 is just like 1.

→ More replies (8)