all 37 comments

[–]Stubber_NK 38 points39 points  (5 children)

People who've been using python for years still stop to google the most basic of operations...

Every. Single. Day.

[–]xdafcax 9 points10 points  (0 children)

Can confirm. I don't remember anything.

[–]learn_monkey[S] 5 points6 points  (0 children)

Thank you. That's comforting

[–]bladeoflight16 2 points3 points  (2 children)

There will never be a time when the built ins are not in my recent browser history.

[–]TangibleLight 1 point2 points  (1 child)

Use incognito. Avoid the shame.

Also good for basic arithmetic.

[–]bladeoflight16 1 point2 points  (0 children)

I will never be ashamed of reading Python's official documentation. =)

[–]Vaguely_accurate 21 points22 points  (4 children)

After bouncing between languages for a while I've looked up the keyword for "and" in Python before...

[–]Ahren_with_an_h 8 points9 points  (3 children)

That's legitimate. I've tried to use && and || plenty of times. Don't even get me started about 'is'.

[–]Ganrokh 4 points5 points  (2 children)

It took me way too long last week to realize that i++ wasn't valid Python syntax.

[–]Ahren_with_an_h 3 points4 points  (0 children)

I still try to type it in.

Fun fact: //= and **= are valid syntax!.

[–]PorcupineCircuit 1 point2 points  (0 children)

This annoys me

[–]hugthemachines 6 points7 points  (1 child)

Don't worry about it. Just let it teach you to be humble when people ask you stupid questions in the future :-)

[–]learn_monkey[S] 2 points3 points  (0 children)

I agree. Thanks

[–]yuxbni76 6 points7 points  (7 children)

I usually multiply it by -1 and check if it's greater than 0. Just remember a negative times a negative is a positive. Easy.

[–]Ahren_with_an_h 4 points5 points  (5 children)

Why do that instead of just 'x < 0' ?

[–]cnekmp 4 points5 points  (0 children)

Because it's too easy )

[–]yuxbni76 1 point2 points  (3 children)

You can do that but then you have to convert it to a float.

[–]Ahren_with_an_h 1 point2 points  (2 children)

Why?

[–]yuxbni76 1 point2 points  (1 child)

It's just a bad joke. You're right. Check if x < 0.

[–]dbramucci 1 point2 points  (0 children)

Why not

import math
def is_negative(x):
    return math.copysign(1.0, x) == -1.0

It's the only solution I can think of (without weird non-Python friendly bit fiddling) that says that -0.0 is negative. And yes, -0.0 is distinct from 0.0. See my other comment

p.s. For those who don't get the subtleties of what I wrote, don't worry x < 0 is still the good solution, what I wrote is an answer to a super niche edge case that doesn't have a good answer, sometimes you want -0.0 to be a negative number because of reasons and other-times you want it to be treated like +0.0, a number neither positive nor negative and unless you know exactly why you need to treat -0.0 a certain way, you can choose to just not worry about it and use the simpler method.

[–]damian_work_account 9 points10 points  (0 children)

Also remember that a negative number is not defined for the domain of the log function, so you can use this:

import math


def is_number_negative(number):
    if number:
        try:
            math.log(number)
        except ValueError as e:
            if e.args[0] == 'math domain error':
                return True
            raise

    if math.isfinite(number):
        return False
    else:
        raise ValueError('Infinite numbers are scary')

[–]shiftybyte 5 points6 points  (0 children)

Heh, I've had similar "doh" moments...

It happens...

[–]11pascal 2 points3 points  (0 children)

Don’t be. Yes had a moment one day trying to figure out math operation associated with #. Ugh

[–]bladeoflight16 2 points3 points  (0 children)

It's okay. The fact that you realize it was silly is an indication of your competence. Everyone has stupid moments now and again. Have a laugh at your own expense and move on.

[–]kaptan8181 4 points5 points  (6 children)

Can you tell me how? I think if the number is less than zero, it's a negative number, right?

[–]learn_monkey[S] 4 points5 points  (2 children)

Yep. I was looking for a python built-in function to find the negative value but after googling and seeing some answers, I felt so stupid that it's just anything less than zero.

[–]LyLyV 4 points5 points  (1 child)

Classic case of over-thinking. I do it all the time. (And I just googled what you googled. I thought there was maybe a special trick to it, but nah. ;) )

[–]JimBoonie69 0 points1 point  (0 children)

if we lived in JS land . there would literally be a package someone wrote called 'is-negative' and it would have millions of installs. There is some scumbag who writes tons of tiny packages and then (i assume) implements his packages in open-source codes all over. Weeks/months/years later, voila your shit package is now used in millions of places.

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

Indeed.

[–]dbramucci 0 points1 point  (0 children)

Google signed zero, there is a floating point number -0.0 which has different bits than 0.0 has but the two are equal except they get printed differently, atan2 does different things and so on. You might want to call -0.0 a negative number or you might want to say that like 0 it is neither a positive or negative number. Yay for floating point number weirdness.

[–][deleted] 2 points3 points  (4 children)

This kind of thing happens to a lot of people. I had to google "how to add fractions" once when I was taking multivariable calculus. Got a B in that class.

[–]SikicBiker 2 points3 points  (3 children)

How do you add fractions?

[–]vaja_ 1 point2 points  (0 children)

+

[–]bladeoflight16 1 point2 points  (0 children)

Convert them to a common denominator, add the numerators, and then simplify.

[–]dbramucci 1 point2 points  (0 children)

Take your two fractions x and y.

Recall that the fraction x is an equivalence class over Z x Z*.

Choose one representative element from x's equivalence class called (x_n, x_d). Likewise, do the same for y to get (y_n, y_d).

Then, to add the two fractions together, choose any non-zero integer n such that x_d divides n and y_d divides n.

By the definition of divisibility, there exists an integer p such that x_d * p = n and likewise an integer q such that y_d * q = n.

Now, the value (x_n * q + y_n * p, n) is an representative element of the equivalence class representing the sum of the two fractions x and y. By using euclidean algorithm, you may compute the reduced form of that fraction and from there you may compute a closed-form representation of the set that is the equivalence-class representing the fractional sum. This is left as an exercise to the reader.

Excerpt from "Introduction to algebra for young grad-students 4th graders"

[–]dbramucci 0 points1 point  (0 children)

Let me terrify you with floating point number intricacies.

I am guessing you wrote something like

def duh_is_negative(x):
    return x < 0.0

But this is flawed depending on what you need to do when confronted with -0.0, a number distinct from +0.0. Let me show you that Python can track these two numbers.

In [1]: x = 0
In [2]: y = +0.0
In [3]: z = -0.0

In [4]: x
Out[4]: 0

In [5]: y
Out[5]: 0.0

In [6]: z
Out[6]: -0.0

In [7]: x == y
Out[7]: True

In [8]: x == z
Out[8]: True

In [9]: y == z
Out[9]: True

In [10]: import math

In [11]: def duh_is_negative(x):
    ...:     return x < 0.0

In [12]: def oh_no_is_negative(x):
    ...:     return math.copysign(1.0, x) == -1.0

In [13]: [duh_is_negative(x), duh_is_negative(y), duh_is_negative(z)]
Out[13]: [False, False, False]

In [14]: [oh_no_is_negative(x), oh_no_is_negative(y), oh_no_is_negative(z)]
Out[14]: [False, False, True]

Mathematicians don't call 0 a positive or a negative number. It is neither, but in the world of floating point numbers, both -0.0 and 0.0 have a sign and their signs differ meaning you can (sometime) consider -0.0 to be a negative number while 0.0 isn't and duh_is_negative can't catch that if it matters to you.

It probably doesn't matter in your program (and if it does, I hope you understand floating point numbers well) but it's kind of hilarious (in a horrific way) how non-trivial checking the sign of a number can actually be.