1

I hope this isn't too dumb, but is 0 and -0 the same, just as in math? Python returns the first character of a string when either of these are used as index values.

harry
  • 157
  • 7
  • 1
    what would you expect to get for `astr[-0]`. For floats there can be a difference because the FPU you can represent `0.0` and `-0.0` – rioV8 Sep 25 '20 at 11:11
  • 1
    `0` and `-0` are the same. `0.0` and `-0.0` are not quite the same. – khelwood Sep 25 '20 at 11:15

3 Answers3

1

That question is not dumb at all: 0 and -0 are the same in case they are numbers, but when they are strings they are clearly different (one has only one and the other has two characters).

You mention both being index values, which are numbers, so they are equal, as you can see here:

Prompt>python
>>> print(0)
0
>>> print(-0)
0

In both cases, the output is clearly the same.

In case they would be strings, the length would already show a clear difference:

>>> print(len("-0"))
2
>>> print(len("0"))
1

For the people, interested in floating point numbers, the equality can be shown as follows:

>>> print(0.0+0.0)
0.0
>>> print(-0.0+0.0)
0.0
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • You say "when they are strings they are clearly different", yet you print them (which calls their string representation functions) and show that they both give the same output string. – gspr Sep 25 '20 at 11:41
  • @gspr: I edited my answer according to your comment. – Dominique Sep 25 '20 at 11:45
  • 2
    But now you're answering a different question, namely *is there a difference between the strings `"0"` and `"-0"`?* That's clearly not what OP is getting at, because in that question there's nothing special about 0, and OP probably knows that prepending the string `"-"` to any string makes the new string different from the old one. – gspr Sep 25 '20 at 11:47
  • This works for me, thanks; might want to add the stuff about it being different with floats, though. – harry Sep 25 '20 at 12:06
  • @HarryHolmes: I've added an example to show the equality for floating point numbers. – Dominique Sep 25 '20 at 12:10
  • 1
    @Dominique: What is your floating point example meant to show? – gspr Sep 25 '20 at 12:15
  • @gspr: if you use 0.0 or -0.0, the result stays the same. – Dominique Sep 25 '20 at 12:29
  • Yes, for that particular calculation. There are many others for which the result does not stay the same. For example `-1.0*0.0` vs `-1.0*(-0.0)`. Or see my answer. – gspr Sep 25 '20 at 12:57
1

I here assume you are talking about 0.0 and -0.0 as (IEEE-754) floating point values. The below does not apply to integers.

One thing having a signed zero allows you to do is provide some useful extra information for certain undefined quantities. For example, while the 2-argument arctangent function is undefined when the horizontal coordinate is 0, the atan2 function in Python and many other programming languages still distinguish between atan2(0.0, 0.0) and atan2(0.0, -0.0) (the second argument of atan2 is the horizontal coordinate).

As you say, as actual real numbers, 0 and -0 are the same. And if you think of atan2 as a function in the mathematical sense, both atan2(0, 0) and atan2(0, -0) are undefined. Amending the definition to handle these cases – and handle them differently (one returns 0, the other (a floating point approximation of) π) – does conveniently allow the user to avoid a bunch of special-casing in code that uses this function.

gspr
  • 11,144
  • 3
  • 41
  • 74
1

Coding is a wide field and it depends on the language and context.

In Python, you have the builtin int type for integers and float for floating-point numbers.

In the case of integers, there is only a single zero. Both 0 and -0 are int literals that map to that single zero.

However, for floating-point numbers, you have two: positive (0.) and negative zero (-0.). Take a look at IEEE 754 and questions like Why do floating-point numbers have signed zeros? for more about this.

Acorn
  • 24,970
  • 5
  • 40
  • 69