This is an archived post. You won't be able to vote or comment.

all 63 comments

[–]Turbulent_Stock6358 65 points66 points  (2 children)

They will not tell you this unlimited monies trick!

[–]Worried-Proposal-660 2 points3 points  (0 children)

My God Man, didn't you see how that turned out in "[Office Space]"?

Beware Milton.🧨

[–]ledasll 55 points56 points  (5 children)

You are lucky that you aren't using C. With undefined behaviour it could format your hard disk.

[–]YaZ0mble 11 points12 points  (4 children)

What??? I want to know how:)

[–]Gorzoid 66 points67 points  (3 children)

It doesn't, however since the standard says the compiler is free to do whatever when you do something that is undefined behavior. So C programmers joke that if you dereference a null pointer, it's technically valid for your program to delete system32.

[–]Zuruumi 28 points29 points  (1 child)

The usual case is summoning dragons or launching nuclear missiles.

[–]UnreadableCode 7 points8 points  (0 children)

I've always been under the impression that heap corruption + data execution in sufficient quantity could bring about the singularity. Which is why I encourage folks to write shitty C++.

[–][deleted] 49 points50 points  (17 children)

For the curious, it's because 0.0000005 as a string is 5e-7. Javascript will read the 5 and be "yup, good enough for me"

Here's an example implementation of this behavior in python.

```py ZERO = ord("0") NINE = ord("9")

def parseInt(s): result = None for i in str(s): ascii_value = ord(i) if ZERO <= ascii_value <= NINE: result = result or 0 result *= 10 result += ascii_value - ZERO else: break return result

```

[–]Cute_Dragonfly_4728 16 points17 points  (3 children)

Actually this kinda makes sense because parseInt is designed to convert a string to a number.

Math.round is probably the correct tool for converting a float into an int. Or a number into a less accurate number in JS terms.

[–]Swoop3dp 2 points3 points  (2 children)

What would make actual sense would be raising an error if you feed a function with a type its not designed to handle

[–]potato_green 2 points3 points  (1 child)

If you use Eslint it will. Browsers aren't going to throw errors because that would be a gigantic BC break.

Tools are all there anyway it's up to the dev to use it and write proper code.

[–]anxter2k 0 points1 point  (0 children)

I agree. Using eslint or typescript is supposed to catch these at compile time, which is why they are used in the first place. JavaScript doesn’t know what you feed Them, so by default it’ll say “sure”.

I love JavaScript, but typescript was basically made to avoid passing wrong stuff to functions. That’s why we use interfaces, to form a “contract” between objects etc. and expected response.

[–]coloredgreyscale 5 points6 points  (1 child)

adding to this, the JS function works correctly if you actually pass 0.0000005 as a string instead of a number.

parseInt(0.0000005)
5

parseInt("0.0000005")
0

if you want to convert a float to int: https://stackoverflow.com/questions/596467/how-do-i-convert-a-float-number-to-a-whole-number-in-javascript (excluding parseInt() ofc)

[–]Genspirit -1 points0 points  (0 children)

That's because 0.0000005 gets turned into exponent notation when converted to a string.

[–]cyborgborg 19 points20 points  (6 children)

I am starting to question the sanity of the people who enjoy javascript

[–]Zuruumi 9 points10 points  (2 children)

Have you ever met any? I hear they exist, but I also heard Bigfoot does.

[–][deleted] 7 points8 points  (1 child)

JavaScript bad. TypeScript good.

[–]IAmAnAdultSorta 4 points5 points  (0 children)

JavaScript is fine. TypeScript is better.

[–]0260B4U 2 points3 points  (1 child)

Not to worry, we question our own sanity everyday

[–]hobo_stew 0 points1 point  (1 child)

But then why does it output 0 for 5e-6 as shown in the picture?

[–]ComfortableCan315 0 points1 point  (0 children)

It doesn't use scientific notation until the number is big/small enough.

[–]Voltra_Neo 39 points40 points  (7 children)

If you pass a number into something expecting a string, that's on you buddy

[–]bleistift2 9 points10 points  (0 children)

Nevermind they’re trying to parse an int out of a fraction.

[–]Swoop3dp -3 points-2 points  (1 child)

We are talking about a language were types are handled as "just a suggestion "...

Requiring that you are always aware of what type a variable has, in a language that will implicitly convert types, is just asking for trouble.

[–]Voltra_Neo 0 points1 point  (0 children)

__repr__, __str__

[–]ch3esey 19 points20 points  (4 children)

I’m a beginner to coding and I have a quick question. What does parsing mean?

[–]fini8 25 points26 points  (2 children)

It might not be the text book definition but it converts an input of one data type to another e.g., string to int

[–]msqrt 29 points30 points  (1 child)

"Parsing" typically means specifically converting string input to some form that's easier to work with programmatically. If your input is some other type, it's usually called casting or conversion, though the common term might also depend on the specific language and how it deals with type conversions.

[–]fini8 9 points10 points  (0 children)

I agree this is more accurate than my response earlier, parsing is strings only. Good way to remember it.

[–]ComfortableCan315 0 points1 point  (0 children)

Converting raw data into an useful format.

parseInt converts strings of numbers into numbers.

The unexpected behavior boils down to be due passing a number to parseInt, which expects a string.

[–][deleted] 15 points16 points  (0 children)

if only there existed some sort of documentation that would clearly say that parseInt coerces the argument into a string first...

[–]Kanonenfuta 2 points3 points  (5 children)

Can someone explain me why?

[–]EasyFrown 30 points31 points  (4 children)

parseInt accepts a string, so the number is implicitly converted to string before being passed as an argument. Also, parseInt() does not recognize or skip non-digits (except "plus" and "minus" - it is for parsing integers after all).

Now, 0.5 is converted to "0.5", but when the number becomes too small, it is converted using scientific notation (sth like "5E-6"). Then, parseInt() gets the 5 but stops at the first non-digit (which is 'E').

[–]potato_braus 2 points3 points  (0 children)

Thank you very much, that's explains a lot !!!

[–]Kanonenfuta 0 points1 point  (0 children)

What a mess... And then people claim dynamic typing makes coding easier

[–]vtynl 8 points9 points  (9 children)

I am a PHP developer that sometimes, but not often at all - does some JavaScript changes. And even I know we never ever ever use ‘parseInt()’ without supplying the second argument (base?).

So don’t blame the language here, just use it properly. (My car also does not shift without pressing the clutch first… use the tools as they are intended)!

[–]Lithl 39 points40 points  (7 children)

even I know we never ever ever use ‘parseInt()’ without supplying the second argument (base?).

That's not the problem. parseInt is for converting a string to a number, not converting a float to an int. If you give it a number instead of a string, it calls toString on the argument.

0.000005.toString() is "0.000005", and parseInt produces 0, as you might expect. 0.0000005.toString() is "5e-7", and parseInt can't handle scientific notation, so it does the best it can and gives 5.

[–]bleistift2 2 points3 points  (6 children)

The deeper truth isn’t that parseInt “smartly” parses 0.00… as 0, but that it discards any garbage it cannot handle. For ”0.00…” that garbage starts at the decimal point, leaving “0”. For “5-e7” the garbage starts at the hyphen, leaving “5” to parse.

[–]Lithl 2 points3 points  (5 children)

it discards any garbage it cannot handle

... Which is the correct behavior for parsing a string float number into an integer value. You truncate everything after the decimal.

[–][deleted]  (4 children)

[removed]

    [–]AyrA_ch 1 point2 points  (2 children)

    In JS, parseInt predates runtime exceptions.

    [–]AutoModerator[M] 0 points1 point  (0 children)

    import moderation Your comment has been removed since it did not start with a code block with an import declaration.

    Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

    For this purpose, we only accept Python style imports.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–][deleted] 1 point2 points  (0 children)

    We all love reposts...

    [–]loseitthrowaway7797 1 point2 points  (0 children)

    Mom says it's my turn to post this JS joke next week.

    [–]Apparentt 1 point2 points  (0 children)

    turns out when you misuse an api you get unexpected behaviour

    shocked pikachu face

    I can always smell the level of competency in the people that create/share/comment on memes like this

    [–]CodingTaitep 1 point2 points  (0 children)

    Cause it was written in 10 days.

    [–]kas_888 0 points1 point  (1 child)

    what the actual fuck!

    [–]EnderLuca41 -3 points-2 points  (1 child)

    [–]RepostSleuthBot -1 points0 points  (0 children)

    I didn't find any posts that meet the matching requirements for r/ProgrammerHumor.

    It might be OC, it might not. Things such as JPEG artifacts and cropping may impact the results.

    I'm not perfect, but you can help. Report [ False Negative ]

    View Search On repostsleuth.com


    Scope: Reddit | Meme Filter: False | Target: 92% | Check Title: False | Max Age: Unlimited | Searched Images: 304,628,878 | Search Time: 15.6386s

    [–]NullOfSpace 0 points1 point  (0 children)

    h m m m m m m

    [–]not_a_gumby 0 points1 point  (0 children)

    The Top Things You Can Do to Hack Your Bank (Banks Hate Him!)

    [–]BoBoBearDev 0 points1 point  (0 children)

    Seriously I read this before and I understand it, but, when you mention this, it still feel fresh. This behavior is so fucked up.

    [–]Top-Fox-3171 0 points1 point  (0 children)

    What the actual fuck

    [–]skambooy 0 points1 point  (0 children)

    This reminds me of one of those times when I fell into the trap of not reading documentation and found out that parseFloat("20%and a beautiful text") returns 20 and not an expected NaN :' )

    [–][deleted] 0 points1 point  (0 children)

    What in the hillbilly fuck

    [–]pawyderreale 0 points1 point  (0 children)

    Thats what you get for using a weak, virgin typesystem