Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110314 > unrolled thread
| Started by | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| First post | 2016-06-22 23:27 +1000 |
| Last post | 2016-06-23 02:48 +0000 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
Is signed zero always available? Steven D'Aprano <steve@pearwood.info> - 2016-06-22 23:27 +1000
Re: Is signed zero always available? Grant Edwards <grant.b.edwards@gmail.com> - 2016-06-22 14:19 +0000
Re: Is signed zero always available? Random832 <random832@fastmail.com> - 2016-06-22 10:34 -0400
Re: Is signed zero always available? Grant Edwards <grant.b.edwards@gmail.com> - 2016-06-22 14:59 +0000
Re: Is signed zero always available? Christopher Reimer <christopher_reimer@icloud.com> - 2016-06-22 15:50 -0700
Re: Is signed zero always available? Steven D'Aprano <steve@pearwood.info> - 2016-06-23 11:16 +1000
Re: Is signed zero always available? Michael Selik <michael.selik@gmail.com> - 2016-06-23 00:34 +0000
Re: Is signed zero always available? Grant Edwards <grant.b.edwards@gmail.com> - 2016-06-23 02:48 +0000
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-06-22 23:27 +1000 |
| Subject | Is signed zero always available? |
| Message-ID | <576a925f$0$1597$c3e8da3$5496439d@news.astraweb.com> |
Both IEEE-754 floats and Decimals support signed zeroes, that is -0.0 and 0.0 are two distinct (but equal) values. (There is only one int zero: -0 and 0 are the same value.) Signed Decimal zero should always be available. What about signed float zero? Are there any platforms where Python is available that don't support signed zeroes? -- Steven
[toc] | [next] | [standalone]
| From | Grant Edwards <grant.b.edwards@gmail.com> |
|---|---|
| Date | 2016-06-22 14:19 +0000 |
| Message-ID | <mailman.38.1466605183.11516.python-list@python.org> |
| In reply to | #110314 |
On 2016-06-22, Steven D'Aprano <steve@pearwood.info> wrote:
> Both IEEE-754 floats and Decimals support signed zeroes, that is -0.0 and
> 0.0 are two distinct (but equal) values.
>
> (There is only one int zero: -0 and 0 are the same value.)
Is that guaranteed by Python, or just a side-effect of the
implementation? Back in the days when Python used native C integers I
think the latter. Now that Python implements it's own
arbitrary-length ints, it may be the former.
> Signed Decimal zero should always be available. What about signed float
> zero? Are there any platforms where Python is available that don't support
> signed zeroes?
--
Grant Edwards grant.b.edwards Yow! This ASEXUAL PIG
at really BOILS my BLOOD
gmail.com ... He's so ... so
... URGENT!!
[toc] | [prev] | [next] | [standalone]
| From | Random832 <random832@fastmail.com> |
|---|---|
| Date | 2016-06-22 10:34 -0400 |
| Message-ID | <mailman.39.1466606092.11516.python-list@python.org> |
| In reply to | #110314 |
On Wed, Jun 22, 2016, at 10:19, Grant Edwards wrote: > Is that guaranteed by Python, or just a side-effect of the > implementation? Back in the days when Python used native C integers I > think the latter. AIUI, native C integers have never reliably supported signed zero even with representations that naively seem to have it. There's no well-defined way to detect it - no int version of copysign, for instance - and implementations are free to erase the distinction on every load/store or define one of them to be a trap representation.
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <grant.b.edwards@gmail.com> |
|---|---|
| Date | 2016-06-22 14:59 +0000 |
| Message-ID | <mailman.41.1466607616.11516.python-list@python.org> |
| In reply to | #110314 |
On 2016-06-22, Random832 <random832@fastmail.com> wrote:
> On Wed, Jun 22, 2016, at 10:19, Grant Edwards wrote:
>
>> Is that guaranteed by Python, or just a side-effect of the
>> implementation? Back in the days when Python used native C
>> integers I think the latter.
>
> AIUI, native C integers have never reliably supported signed zero
> even with representations that naively seem to have it. There's no
> well-defined way to detect it - no int version of copysign, for
> instance - and implementations are free to erase the distinction on
> every load/store or define one of them to be a trap representation.
It's been almost 25 years since I used hardware that supported signed
zero integers (CDC 6600). I don't recall there being a C compiler
available. We used Pascal and assembly, though I think FORTRAN was
what most people used. I don't recall whether the Pascal
implementation exposed the existence of -0 to the user or not.
I'm pretty certain there wasn't a Python implementation...
--
Grant Edwards grant.b.edwards Yow! Loni Anderson's hair
at should be LEGALIZED!!
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Christopher Reimer <christopher_reimer@icloud.com> |
|---|---|
| Date | 2016-06-22 15:50 -0700 |
| Message-ID | <mailman.52.1466639498.11516.python-list@python.org> |
| In reply to | #110314 |
> On Jun 22, 2016, at 7:59 AM, Grant Edwards <grant.b.edwards@gmail.com> wrote: > >> On 2016-06-22, Random832 <random832@fastmail.com> wrote: >>> On Wed, Jun 22, 2016, at 10:19, Grant Edwards wrote: >>> >>> Is that guaranteed by Python, or just a side-effect of the >>> implementation? Back in the days when Python used native C >>> integers I think the latter. >> >> AIUI, native C integers have never reliably supported signed zero >> even with representations that naively seem to have it. There's no >> well-defined way to detect it - no int version of copysign, for >> instance - and implementations are free to erase the distinction on >> every load/store or define one of them to be a trap representation. > > It's been almost 25 years since I used hardware that supported signed > zero integers (CDC 6600). I don't recall there being a C compiler > available. We used Pascal and assembly, though I think FORTRAN was > what most people used. I don't recall whether the Pascal > implementation exposed the existence of -0 to the user or not. When I took mathematics in college, the following was true: -1 * 0 = 0 I would probably have gotten rapped on the knuckles by my instructors if I answered -0. Zero was zero. No plus or minus about that. No discussion of signed integers ever mentioned signed zero. Did I miss something in college? Or did -0 represent zero volts on the negative rail of an op-amp in electronics? Thank you, Chris R.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-06-23 11:16 +1000 |
| Message-ID | <576b3874$0$1608$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #110346 |
On Thu, 23 Jun 2016 08:50 am, Christopher Reimer wrote: > When I took mathematics in college, the following was true: > > -1 * 0 = 0 > > I would probably have gotten rapped on the knuckles by my instructors if I > answered -0. Zero was zero. No plus or minus about that. No discussion of > signed integers ever mentioned signed zero. > > Did I miss something in college? Yes. In IEEE-754 floating point maths, zero can represent two distinct concepts: actual mathematical zero, and the result of a calculation which ought to be small but non-zero, but due to the limitations of a fixed data size, underflows to zero. Ideally, we would want *three* zeroes, representing underflow to zero but positive, underflow to zero but negative, and true mathematical zero. Inconveniently, the standard floating point format leads to *two* different representations of zero, one which is all zero bits, and one which is all zero bits except for the sign bit. So the behaviour of IEEE-754 zeroes is a mix of "treat them as the result of underflow" and "treat them as zero". For example, the standard mandates that -0.0 must compare equal to 0.0, and that in regular arithmetic there's no difference between the two: x+0.0 and x-0.0 are the same. But that doesn't mean that they are always treated the same. For instance, consider 1/x, where x is one of the zeroes. If we treat them as "very small numbers which have underflowed", then 1/x should be "very big numbers which will overflow", and IEEE-754 mandates two special values to represent overflow: INF and -INF. So 1/0.0 may return INF, and 1/-0.0 may return -INF. (I say "may" rather than "will", because the standard allows that behaviour to be configurable: you can specify whether to get ±INF or to signal divide by zero. Unfortunately although most CPUs and FPUs support this behaviour, support for it in compilers is mostly poor.) But where signed zeroes really become useful is when dealing with branch cuts for complex elementary functions: http://people.freebsd.org/~das/kahan86branch.pdf -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Michael Selik <michael.selik@gmail.com> |
|---|---|
| Date | 2016-06-23 00:34 +0000 |
| Message-ID | <mailman.53.1466642104.11516.python-list@python.org> |
| In reply to | #110314 |
On Wed, Jun 22, 2016 at 4:53 PM Christopher Reimer < christopher_reimer@icloud.com> wrote: > > On Jun 22, 2016, at 7:59 AM, Grant Edwards <grant.b.edwards@gmail.com> > wrote: > > > >> On 2016-06-22, Random832 <random832@fastmail.com> wrote: > >>> On Wed, Jun 22, 2016, at 10:19, Grant Edwards wrote: > >>> > >>> Is that guaranteed by Python, or just a side-effect of the > >>> implementation? Back in the days when Python used native C > >>> integers I think the latter. > >> > >> AIUI, native C integers have never reliably supported signed zero > >> even with representations that naively seem to have it. There's no > >> well-defined way to detect it - no int version of copysign, for > >> instance - and implementations are free to erase the distinction on > >> every load/store or define one of them to be a trap representation. > > > > It's been almost 25 years since I used hardware that supported signed > > zero integers (CDC 6600). I don't recall there being a C compiler > > available. We used Pascal and assembly, though I think FORTRAN was > > what most people used. I don't recall whether the Pascal > > implementation exposed the existence of -0 to the user or not. > > When I took mathematics in college, the following was true: > > -1 * 0 = 0 > > I would probably have gotten rapped on the knuckles by my instructors if I > answered -0. Zero was zero. No plus or minus about that. No discussion of > signed integers ever mentioned signed zero. > > Did I miss something in college? > I can't remember where I came across the concept. It might have been in calculus. Zero can be thought of as the asymptotic value of 1/n as n approaches infinity. If so, then negative zero would be the asymptote of -1/n as n approaches infinity.
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <grant.b.edwards@gmail.com> |
|---|---|
| Date | 2016-06-23 02:48 +0000 |
| Message-ID | <mailman.54.1466650156.11516.python-list@python.org> |
| In reply to | #110314 |
On 2016-06-22, Christopher Reimer <christopher_reimer@icloud.com> wrote: >> On Jun 22, 2016, at 7:59 AM, Grant Edwards <grant.b.edwards@gmail.com> wrote: >> >>> On 2016-06-22, Random832 <random832@fastmail.com> wrote: >>>> On Wed, Jun 22, 2016, at 10:19, Grant Edwards wrote: >>>> >>>> Is that guaranteed by Python, or just a side-effect of the >>>> implementation? Back in the days when Python used native C >>>> integers I think the latter. >>> >>> AIUI, native C integers have never reliably supported signed zero >>> even with representations that naively seem to have it. There's no >>> well-defined way to detect it - no int version of copysign, for >>> instance - and implementations are free to erase the distinction on >>> every load/store or define one of them to be a trap representation. >> >> It's been almost 25 years since I used hardware that supported signed >> zero integers (CDC 6600). I don't recall there being a C compiler >> available. We used Pascal and assembly, though I think FORTRAN was >> what most people used. I don't recall whether the Pascal >> implementation exposed the existence of -0 to the user or not. > > When I took mathematics in college, the following was true: > > -1 * 0 = 0 Well, we're talking about computers and programming languages, not mathematics. > I would probably have gotten rapped on the knuckles by my instructors > if I answered -0. Zero was zero. No plus or minus about that. No > discussion of signed integers ever mentioned signed zero. > > Did I miss something in college? If you took computer architecture courses, then yes, you missed the chapters on machine representation of integers. If you build a CPU that uses either 1's compliment or signed magnitude representations then there is a postive zero and a negative zero. If you design it to use 2's complement, then there is only a positive zero. 1's complement and signed-magnitude are excedingly rare these days, but used to be much more common. -- Grant
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web