
This was probably discussed a lot. Why is there no simple `sign` function in Python, like in the math module or something? I mean one that tells you the sign of a number. Ram.

On Tue, Apr 27, 2010 at 7:12 AM, cool-RR <cool-rr@cool-rr.com> wrote:
I really hate to shot you down man. First of all, how are you returning the sign? True/False for positive or negative? What about zero? Is zero a positive or a negative number? Secondly, when you get the output, how are you using it? Comparison -- ah wait, there is always if x > 0 ... I think you need to carefully think about these ideas before proposing them. I feel you're wasting your own time, here. Cheers, Xav

On Mon, Apr 26, 2010 at 11:16 PM, Xavier Ho <contact@xavierho.com> wrote:
Hey Xavier, I don't feel I'm wasting my time. If you feel I'm wasting yours you are free to ignore my messages. I return 1 for positive, 0 for zero, -1 for negative. Yes, I know I can improvise something like it, by using `x > 0` or `cmp(x, 0)`, but I like code that reads like what it does. Ram. -- Sincerely, Ram Rachum

On 4/26/10 4:21 PM, cool-RR wrote:
That is what small utility functions are for. Basically, there are number of different conventions a sign() function could choose. In floating point, there are signed zeros and NaNs. Similarly, there is no sign() function in the standard C math library, which the math module tries to wrap thinly. Since there is an important ambiguity, the standard libraries leave it to you to write your own small utility function which implements the convention you desire. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

Robert Kern <robert.kern@...> writes: | That is what small utility functions are for. | | Basically, there are number of different conventions a sign() function could | choose. In floating point, there are signed zeros and NaNs. Similarly, there | no sign() function in the standard C math library, which the math module tries | to wrap thinly. Since there is an important ambiguity, the standard libraries | leave it to you to write your own small utility function which implements the | convention you desire. That makes sense, now that you phrase it like that. Thanks Robert. Ram.

On Mon, Apr 26, 2010 at 15:12, cool-RR <cool-rr@cool-rr.com> wrote:
There is one. Python 2.6 added math.copysign(). However, it's intended to deal with float peculiarities such as negative 0, so it's a little obtuse. Most code, such as that using integers, should just use "if x > 0:".

On Mon, Apr 26, 2010 at 10:12 PM, cool-RR <cool-rr@cool-rr.com> wrote:
What are your use-cases? Can you give some examples of how you'd expect to use it? Given math.copysign and direct comparisons like "if x > 0", I don't think I've ever wanted a sign function. If a sign function were implemented, I'd probably want something like IEEE 754's signbit function, returning 1 for negative values and -0.0, and 0 for positive values and 0.0. Ideally, something giving a boolean result: hasSignBit. (Insert better name here.) Mark

On Mon, Apr 26, 2010 at 10:36 PM, Mark Dickinson <dickinsm@gmail.com> wrote:
If a sign function were implemented, I'd probably want something like IEEE 754's signbit function
Correction: it's C99 that has a signbit function. IEEE 754-2008 specifies an 'isSignMinus' function with exactly the same semantics: values with the sign bit set (including -0.0, -inf, and NaNs whose sign bit is set) return True; other values return False. I wouldn't object to a `math.is_signed` function or a `float.is_signed` method with these semantics. It's not clear where is the better place: we have `math.isnan` and `math.isinf`, but `float.is_integer`. Mark

Mark Dickinson wrote:
"is_signed" would probably be a bad name for this purpose. In typical compsci parlance, all of our numeric types are signed. For the semantics you're talking about, math.signbit would be a more reasonable name. (since we can legitimately answer the question for both integers and floats, whereas "is_integer" is a pretty redundant question if you are dealing with an integer type) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia ---------------------------------------------------------------

On Tue, Apr 27, 2010 at 10:52 AM, Nick Coghlan <ncoghlan@gmail.com> wrote:
I agree that it's not a great name. It comes from the Decimal module: there's a Decimal.is_signed method. That name in turn comes from the specification, which calls it "is-signed". I'd have preferred is_negative, but that's not really right either, since -0.0 isn't strictly speaking negative.
And if it's a function in the math module, it seems entirely reasonable to re-use the C99 name. So the proposal is: add a math.signbit function, with exactly the same semantics as the C99 signbit function: returning 1 for values with the signbit set (negative values, -0.0, nans with the sign bit set) and 0 otherwise. It would return 0 for a zero integer, too. A Python implementation is as simple as: import math def signbit(x): return 1 if math.copysign(1.0, x) == -1.0 else 0 Would this be of value to anyone? I'd only vote +0, since I don't feel a great need for this function. By the way, the signbit function definitely does have uses: for example, it's useful for implementing odd (in the mathematical sense) functions like asinh while making sure that signed zeros are treated correctly: def asinh(x): if signbit(x): return -asinh(-x) # deal with nonnegative values here If the test were simply "if x > 0.0:" then (depending on exactly what the rest of the function looks like) you'd risk getting the wrong sign for at least one of 0.0 and -0.0. Of course, you can still use copysign to do the test, as above, but it's clunkier. Mark

Mark Dickinson wrote:
Would this be of value to anyone? I'd only vote +0, since I don't feel a great need for this function.
I'm in a similar position. I was merely commenting that *if* we did anything, a C99 inspired math.signbit would probably be the thing to add. (I don't see the value of the 3 way comparison, since that doesn't suffer any signed zero oddities, and hence, NaN values aside, can be handled with the ordinary comparison operators). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia ---------------------------------------------------------------

On Tue, Apr 27, 2010 at 7:12 AM, cool-RR <cool-rr@cool-rr.com> wrote:
I really hate to shot you down man. First of all, how are you returning the sign? True/False for positive or negative? What about zero? Is zero a positive or a negative number? Secondly, when you get the output, how are you using it? Comparison -- ah wait, there is always if x > 0 ... I think you need to carefully think about these ideas before proposing them. I feel you're wasting your own time, here. Cheers, Xav

On Mon, Apr 26, 2010 at 11:16 PM, Xavier Ho <contact@xavierho.com> wrote:
Hey Xavier, I don't feel I'm wasting my time. If you feel I'm wasting yours you are free to ignore my messages. I return 1 for positive, 0 for zero, -1 for negative. Yes, I know I can improvise something like it, by using `x > 0` or `cmp(x, 0)`, but I like code that reads like what it does. Ram. -- Sincerely, Ram Rachum

On 4/26/10 4:21 PM, cool-RR wrote:
That is what small utility functions are for. Basically, there are number of different conventions a sign() function could choose. In floating point, there are signed zeros and NaNs. Similarly, there is no sign() function in the standard C math library, which the math module tries to wrap thinly. Since there is an important ambiguity, the standard libraries leave it to you to write your own small utility function which implements the convention you desire. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

Robert Kern <robert.kern@...> writes: | That is what small utility functions are for. | | Basically, there are number of different conventions a sign() function could | choose. In floating point, there are signed zeros and NaNs. Similarly, there | no sign() function in the standard C math library, which the math module tries | to wrap thinly. Since there is an important ambiguity, the standard libraries | leave it to you to write your own small utility function which implements the | convention you desire. That makes sense, now that you phrase it like that. Thanks Robert. Ram.

On Mon, Apr 26, 2010 at 15:12, cool-RR <cool-rr@cool-rr.com> wrote:
There is one. Python 2.6 added math.copysign(). However, it's intended to deal with float peculiarities such as negative 0, so it's a little obtuse. Most code, such as that using integers, should just use "if x > 0:".

On Mon, Apr 26, 2010 at 10:12 PM, cool-RR <cool-rr@cool-rr.com> wrote:
What are your use-cases? Can you give some examples of how you'd expect to use it? Given math.copysign and direct comparisons like "if x > 0", I don't think I've ever wanted a sign function. If a sign function were implemented, I'd probably want something like IEEE 754's signbit function, returning 1 for negative values and -0.0, and 0 for positive values and 0.0. Ideally, something giving a boolean result: hasSignBit. (Insert better name here.) Mark

On Mon, Apr 26, 2010 at 10:36 PM, Mark Dickinson <dickinsm@gmail.com> wrote:
If a sign function were implemented, I'd probably want something like IEEE 754's signbit function
Correction: it's C99 that has a signbit function. IEEE 754-2008 specifies an 'isSignMinus' function with exactly the same semantics: values with the sign bit set (including -0.0, -inf, and NaNs whose sign bit is set) return True; other values return False. I wouldn't object to a `math.is_signed` function or a `float.is_signed` method with these semantics. It's not clear where is the better place: we have `math.isnan` and `math.isinf`, but `float.is_integer`. Mark

Mark Dickinson wrote:
"is_signed" would probably be a bad name for this purpose. In typical compsci parlance, all of our numeric types are signed. For the semantics you're talking about, math.signbit would be a more reasonable name. (since we can legitimately answer the question for both integers and floats, whereas "is_integer" is a pretty redundant question if you are dealing with an integer type) Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia ---------------------------------------------------------------

On Tue, Apr 27, 2010 at 10:52 AM, Nick Coghlan <ncoghlan@gmail.com> wrote:
I agree that it's not a great name. It comes from the Decimal module: there's a Decimal.is_signed method. That name in turn comes from the specification, which calls it "is-signed". I'd have preferred is_negative, but that's not really right either, since -0.0 isn't strictly speaking negative.
And if it's a function in the math module, it seems entirely reasonable to re-use the C99 name. So the proposal is: add a math.signbit function, with exactly the same semantics as the C99 signbit function: returning 1 for values with the signbit set (negative values, -0.0, nans with the sign bit set) and 0 otherwise. It would return 0 for a zero integer, too. A Python implementation is as simple as: import math def signbit(x): return 1 if math.copysign(1.0, x) == -1.0 else 0 Would this be of value to anyone? I'd only vote +0, since I don't feel a great need for this function. By the way, the signbit function definitely does have uses: for example, it's useful for implementing odd (in the mathematical sense) functions like asinh while making sure that signed zeros are treated correctly: def asinh(x): if signbit(x): return -asinh(-x) # deal with nonnegative values here If the test were simply "if x > 0.0:" then (depending on exactly what the rest of the function looks like) you'd risk getting the wrong sign for at least one of 0.0 and -0.0. Of course, you can still use copysign to do the test, as above, but it's clunkier. Mark

Mark Dickinson wrote:
Would this be of value to anyone? I'd only vote +0, since I don't feel a great need for this function.
I'm in a similar position. I was merely commenting that *if* we did anything, a C99 inspired math.signbit would probably be the thing to add. (I don't see the value of the 3 way comparison, since that doesn't suffer any signed zero oddities, and hence, NaN values aside, can be handled with the ordinary comparison operators). Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia ---------------------------------------------------------------
participants (8)
-
Adam Olsen
-
cool-RR
-
Mark Dickinson
-
MRAB
-
Nick Coghlan
-
Ram Rachum
-
Robert Kern
-
Xavier Ho