division by zero

Started by Eric Ridgeabout 23 years ago30 messageshackersgeneral
Jump to latest
#1Eric Ridge
ebr@tcdi.com
hackersgeneral

I admit, I suck at math, but I really thought postgres would ERROR on
this:

foo=# select 1/0;
?column?
----------
0
(1 row)

More surprising is that this does ERROR:
foo=# select 1/0.0;
ERROR: division by zero on numeric

Is this a bug, or do I need to go back to high school algebra? "Both"
is an acceptable answer. :)

eric

#2Bruce Momjian
bruce@momjian.us
In reply to: Eric Ridge (#1)
hackersgeneral
Re: division by zero

I think this caused by floating point signals being reported by the OS,
while integer operations don't have signals. I suppose we could catch
the divide by zero easily. Folks?

---------------------------------------------------------------------------

Eric B.Ridge wrote:

I admit, I suck at math, but I really thought postgres would ERROR on
this:

foo=# select 1/0;
?column?
----------
0
(1 row)

More surprising is that this does ERROR:
foo=# select 1/0.0;
ERROR: division by zero on numeric

Is this a bug, or do I need to go back to high school algebra? "Both"
is an acceptable answer. :)

eric

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Eric Ridge (#1)
hackersgeneral
Re: division by zero

"Eric B.Ridge" <ebr@tcdi.com> writes:

I admit, I suck at math, but I really thought postgres would ERROR on
this:

foo=# select 1/0;

There's something wrong with your platform, I think. I get an error on
both HPUX and Red Hat Linux. What platform is this? and what PG
version?

z=# select 1/0;
ERROR: floating point exception! The last floating point operation either exceeded legal ranges or was a divide by zero
z=#

regards, tom lane

#4Eric Ridge
ebr@tcdi.com
In reply to: Tom Lane (#3)
hackersgeneral
Re: division by zero

On Friday, March 7, 2003, at 02:50 PM, Tom Lane wrote:

"Eric B.Ridge" <ebr@tcdi.com> writes:

I admit, I suck at math, but I really thought postgres would ERROR on
this:

foo=# select 1/0;

There's something wrong with your platform, I think. I get an error on
both HPUX and Red Hat Linux. What platform is this? and what PG
version?

I'm sorry. I know better.

% uname -a
Darwin foo.local. 6.4 Darwin Kernel Version 6.4: Wed Jan 29 18:50:42
PST 2003; root:xnu/xnu-344.26.obj~1/RELEASE_PPC Power Macintosh powerpc
Which translates to Mac OS X 10.2.4 (6I32)

foo=# select version();
version
------------------------------------------------------------------------
--------------------------
PostgreSQL 7.3.2 on powerpc-apple-darwin6.4, compiled by GCC gcc (GCC)
3.1 20020420 (prerelease)

select 1/0; fails as expected on my x86 Linux box, so yer right, it's
just my little Mac. "I switched because Mac's can divide by zero."

eric

Show quoted text

z=# select 1/0;
ERROR: floating point exception! The last floating point operation
either exceeded legal ranges or was a divide by zero
z=#

regards, tom lane

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Eric Ridge (#4)
hackersgeneral
Re: division by zero

"Eric B. Ridge" <ebr@tcdi.com> writes:

select 1/0; fails as expected on my x86 Linux box, so yer right, it's
just my little Mac. "I switched because Mac's can divide by zero."

Mph. I have a Mac handy, will look into this.

Can anyone confirm whether the error shows up on other BSD-derived
platforms?

regards, tom lane

#6Fernando Schapachnik
fernando@mecon.gov.ar
In reply to: Tom Lane (#5)
hackersgeneral
Re: division by zero

En un mensaje anterior, Tom Lane escribi�:

Can anyone confirm whether the error shows up on other BSD-derived
platforms?

FreeBSD 4.7-RELEASE-p7

db=# SELECT version();
version
---------------------------------------------------------------------
PostgreSQL 7.3.2 on i386-portbld-freebsd4.7, compiled by GCC 2.95.4
(1 row)

db=# SELECT 1/0;
ERROR: floating point exception! The last floating point operation either
exceeded legal ranges or was a divide by zero

Regards.

#7Pam Wampler
Pam_Wampler@taylorwhite.com
In reply to: Fernando Schapachnik (#6)
general
Re: division by zero

mcs=# select 1/0;
ERROR: floating point exception! The last floating point operation either
exceeded legal ranges or was a divide by zero
mcs=# select version();
version
---------------------------------------------------------------------
PostgreSQL 7.2.1 on i386-portbld-freebsd4.5, compiled by GCC 2.95.3
(1 row)

-----Original Message-----
From: Fernando Schapachnik [mailto:fernando@mecon.gov.ar]
Sent: Friday, March 07, 2003 3:09 PM
To: Tom Lane
Cc: Eric B. Ridge; pgsql-general@postgresql.org
Subject: Re: [GENERAL] division by zero

En un mensaje anterior, Tom Lane escribió:

Can anyone confirm whether the error shows up on other BSD-derived
platforms?

FreeBSD 4.7-RELEASE-p7

db=# SELECT version();
version
---------------------------------------------------------------------
PostgreSQL 7.3.2 on i386-portbld-freebsd4.7, compiled by GCC 2.95.4
(1 row)

db=# SELECT 1/0;
ERROR: floating point exception! The last floating point operation either
exceeded legal ranges or was a divide by zero

Regards.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

#8Bruce Momjian
bruce@momjian.us
In reply to: Pam Wampler (#7)
general
Re: division by zero

Error on BSD/OS too. I should have checked that.

---------------------------------------------------------------------------

Pam Wampler wrote:

mcs=# select 1/0;
ERROR: floating point exception! The last floating point operation either
exceeded legal ranges or was a divide by zero
mcs=# select version();
version
---------------------------------------------------------------------
PostgreSQL 7.2.1 on i386-portbld-freebsd4.5, compiled by GCC 2.95.3
(1 row)

-----Original Message-----
From: Fernando Schapachnik [mailto:fernando@mecon.gov.ar]
Sent: Friday, March 07, 2003 3:09 PM
To: Tom Lane
Cc: Eric B. Ridge; pgsql-general@postgresql.org
Subject: Re: [GENERAL] division by zero

En un mensaje anterior, Tom Lane escribi?:

Can anyone confirm whether the error shows up on other BSD-derived
platforms?

FreeBSD 4.7-RELEASE-p7

db=# SELECT version();
version
---------------------------------------------------------------------
PostgreSQL 7.3.2 on i386-portbld-freebsd4.7, compiled by GCC 2.95.4
(1 row)

db=# SELECT 1/0;
ERROR: floating point exception! The last floating point operation either
exceeded legal ranges or was a divide by zero

Regards.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#9Eric Ridge
ebr@tcdi.com
In reply to: Tom Lane (#5)
hackersgeneral
Re: division by zero

On Friday, March 7, 2003, at 03:05 PM, Tom Lane wrote:

"Eric B. Ridge" <ebr@tcdi.com> writes:

select 1/0; fails as expected on my x86 Linux box, so yer right, it's
just my little Mac. "I switched because Mac's can divide by zero."

Mph. I have a Mac handy, will look into this.

I doubt this helps, but I just tried on my Mac against 7.2.3 and it
still returns zero.

eric

Show quoted text

Can anyone confirm whether the error shows up on other BSD-derived
platforms?

regards, tom lane

#10Brian Hirt
bhirt@mobygames.com
In reply to: Eric Ridge (#9)
hackersgeneral
Re: division by zero

fyi, my mac running mac os 10.2.4 and postgres 7.3.1 returns zero also.

On Friday, March 7, 2003, at 02:36 PM, Eric B.Ridge wrote:

Show quoted text

On Friday, March 7, 2003, at 03:05 PM, Tom Lane wrote:

"Eric B. Ridge" <ebr@tcdi.com> writes:

select 1/0; fails as expected on my x86 Linux box, so yer right, it's
just my little Mac. "I switched because Mac's can divide by zero."

Mph. I have a Mac handy, will look into this.

I doubt this helps, but I just tried on my Mac against 7.2.3 and it
still returns zero.

eric

Can anyone confirm whether the error shows up on other BSD-derived
platforms?

regards, tom lane

---------------------------(end of
broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

#11Tom Lane
tgl@sss.pgh.pa.us
In reply to: Eric Ridge (#4)
hackersgeneral
Re: division by zero

"Eric B. Ridge" <ebr@tcdi.com> writes:

select 1/0; fails as expected on my x86 Linux box, so yer right, it's
just my little Mac. "I switched because Mac's can divide by zero."

I checked into this, and indeed OS X 10.2 is behaving funny: integer
divide by zero doesn't raise any signal, it just returns a bogus answer.
They're within their rights to do so according to the ANSI C spec
(wherein division by zero is stated to have undefined behavior).
But since other BSD-derived Unixen all seem to raise SIGFPE, I can't
help wondering if this shouldn't be considered a bug.

I think we have three possible responses:

1. Put explicit tests for zero into the integer division SQL function
routines.

2. Consider this Apple's problem and file a bug report.

3. Both.

I don't care for answer #1 alone, because it would only catch zero
divides in the specific places we put in tests; internal errors would
likely go uncaught. So I think a complaint to Apple is in order.
I'm not sure whether to also put in zero-divide guards. Comments?

regards, tom lane

#12Merlin Moncure
merlin.moncure@rcsonline.com
In reply to: Tom Lane (#11)
hackers
Re: [GENERAL] division by zero

Tom Lane wrote:

I checked into this, and indeed OS X 10.2 is behaving funny: integer
divide by zero doesn't raise any signal, it just returns a bogus

answer.

They're within their rights to do so according to the ANSI C spec
(wherein division by zero is stated to have undefined behavior).
But since other BSD-derived Unixen all seem to raise SIGFPE, I can't
help wondering if this shouldn't be considered a bug.

FWIW, this also is a problem with some of the windows ports. For
example, 'select 0/0' is unpredictable and can cause the server to gpf
and restart. This does not include the SRA port, because I don't have
it.

Merlin

#13Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Tom Lane (#11)
hackersgeneral
Re: division by zero

On Sat, 8 Mar 2003, Tom Lane wrote:

"Eric B. Ridge" <ebr@tcdi.com> writes:

select 1/0; fails as expected on my x86 Linux box, so yer right, it's
just my little Mac. "I switched because Mac's can divide by zero."

I checked into this, and indeed OS X 10.2 is behaving funny: integer
divide by zero doesn't raise any signal, it just returns a bogus answer.
They're within their rights to do so according to the ANSI C spec
(wherein division by zero is stated to have undefined behavior).
But since other BSD-derived Unixen all seem to raise SIGFPE, I can't
help wondering if this shouldn't be considered a bug.

I think we have three possible responses:

1. Put explicit tests for zero into the integer division SQL function
routines.

2. Consider this Apple's problem and file a bug report.

3. Both.

I don't care for answer #1 alone, because it would only catch zero
divides in the specific places we put in tests; internal errors would
likely go uncaught. So I think a complaint to Apple is in order.
I'm not sure whether to also put in zero-divide guards. Comments?

We probably should put in guards since relying on sane behavior for
division by zero is probably unsafe from a portability standpoint. This
could hide some internal error on a system that has this problem (since we
could otherwise put it into a regression test) but those systems are
currently broken if the internal error occurs anyway.

#14Justin Clift
justin@postgresql.org
In reply to: Tom Lane (#11)
hackersgeneral
Re: [GENERAL] division by zero

Tom Lane wrote:
<snip>

2. Consider this Apple's problem and file a bug report.

Is there a good place to report errors to Apple for this kind of thing?

Was chatting to one of the guys in the MacOSX development team at Apple
a while ago and can pursue it through there if needed.

:-)

Regards and best wishes,

Justin Clift

3. Both.

I don't care for answer #1 alone, because it would only catch zero
divides in the specific places we put in tests; internal errors would
likely go uncaught. So I think a complaint to Apple is in order.
I'm not sure whether to also put in zero-divide guards. Comments?

regards, tom lane

--
"My grandfather once told me that there are two kinds of people: those
who work and those who take the credit. He told me to try to be in the
first group; there was less competition there."
- Indira Gandhi

#15Eric Ridge
ebr@tcdi.com
In reply to: Justin Clift (#14)
hackersgeneral
Re: [GENERAL] division by zero

On Saturday, March 8, 2003, at 11:54 PM, Justin Clift wrote:

Tom Lane wrote:
<snip>

2. Consider this Apple's problem and file a bug report.

Is there a good place to report errors to Apple for this kind of thing?

The best place I can find is:
http://developer.apple.com/bugreporter/index.html

Unfortunately, there doesn't seem to be a way to query existing
reports... If there is, I can't find it.

Also, I can't help but wonder why Apple/DarwinTeam handle integer
division by zero this way. There must be a reason, which makes me
think that "[considering] this Apple's problem" might not work out for
postgres in the end.

eric

#16Justin Clift
justin@postgresql.org
In reply to: Eric Ridge (#15)
hackersgeneral
Re: [GENERAL] division by zero

Hi guys,

Was just looking around Google for similar reports of errors and came
across this:

MacOS X Server Developer Release Notes: Core OS Runtime
http://www.geminisolutions.com/WebObjects_4.5/Documentation/Developer/YellowBox/ReleaseNotes/Runtime.html

Looks like this is a known problem (as of 1998) and may not have been fixed.

Further hits come up when searching on the Apple Developer Connection
site too:

http://developer.apple.com/cgi-bin/search.pl?&amp;q=divide+by+zero&amp;num=10&amp;lang=lang_en|lang_zh-CN|lang_fr|lang_de|lang_ja&amp;ie=utf8&amp;oe=utf8
(that should be all one line)

And this one looks potentially interesting:

http://developer.apple.com/technotes/tn2002/tn2053.html
(search in this page for "FE_ENABLE_DIVBYZERO")

Have asked the member of the Apple MacOS X Server team what he
recommends the best way to proceed is.

Regards and best wishes,

Justin Clift

Eric B.Ridge wrote:

On Saturday, March 8, 2003, at 11:54 PM, Justin Clift wrote:

Tom Lane wrote:
<snip>

2. Consider this Apple's problem and file a bug report.

Is there a good place to report errors to Apple for this kind of thing?

The best place I can find is:
http://developer.apple.com/bugreporter/index.html

Unfortunately, there doesn't seem to be a way to query existing
reports... If there is, I can't find it.

Also, I can't help but wonder why Apple/DarwinTeam handle integer
division by zero this way. There must be a reason, which makes me think
that "[considering] this Apple's problem" might not work out for
postgres in the end.

eric

--
"My grandfather once told me that there are two kinds of people: those
who work and those who take the credit. He told me to try to be in the
first group; there was less competition there."
- Indira Gandhi

#17Bruce Momjian
bruce@momjian.us
In reply to: Merlin Moncure (#12)
hackers
Re: [GENERAL] division by zero

Merlin Moncure wrote:

Tom Lane wrote:

I checked into this, and indeed OS X 10.2 is behaving funny: integer
divide by zero doesn't raise any signal, it just returns a bogus

answer.

They're within their rights to do so according to the ANSI C spec
(wherein division by zero is stated to have undefined behavior).
But since other BSD-derived Unixen all seem to raise SIGFPE, I can't
help wondering if this shouldn't be considered a bug.

FWIW, this also is a problem with some of the windows ports. For
example, 'select 0/0' is unpredictable and can cause the server to gpf
and restart. This does not include the SRA port, because I don't have
it.

I just tested the SRA Win32 threaded port and both SELECT 1/0 and SELECT
0/0 crash the process. I have reported this to Tatsuo.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#18Justin Clift
justin@postgresql.org
In reply to: Bruce Momjian (#17)
hackers
Re: [GENERAL] division by zero

Bruce Momjian wrote:
<snip>

FWIW, this also is a problem with some of the windows ports. For
example, 'select 0/0' is unpredictable and can cause the server to gpf
and restart. This does not include the SRA port, because I don't have
it.

I just tested the SRA Win32 threaded port and both SELECT 1/0 and SELECT
0/0 crash the process. I have reported this to Tatsuo.

Reported the issue to the Apple guys earlier on today, but haven't heard
anything back from them yet.

Guess we'll have to wait a few days to find out where things are at in
regards to MacOS X.

Regards and best wishes,

Justin Clift

--
"My grandfather once told me that there are two kinds of people: those
who work and those who take the credit. He told me to try to be in the
first group; there was less competition there."
- Indira Gandhi

#19Merlin Moncure
merlin.moncure@rcsonline.com
In reply to: Justin Clift (#18)
hackers
Re: [GENERAL] division by zero

Bruce Momjian wrote:

I just tested the SRA Win32 threaded port and both SELECT 1/0 and

SELECT

0/0 crash the process. I have reported this to Tatsuo.

The PeerDirect version (beta4) does the same thing. This is the version
based on the 7.2.1 source that I have been testing for a while. If
there is a newer version than that I don't have it yet. The source is
not available so I can't verify the cause.

Merlin

#20Bruce Momjian
bruce@momjian.us
In reply to: Merlin Moncure (#19)
hackers
Re: [GENERAL] division by zero

The big question is how to fix this on Win32. Is a test in the integer
division routines enough? Is there a signal to catch on Win32?

---------------------------------------------------------------------------

Merlin Moncure wrote:

Bruce Momjian wrote:

I just tested the SRA Win32 threaded port and both SELECT 1/0 and

SELECT

0/0 crash the process. I have reported this to Tatsuo.

The PeerDirect version (beta4) does the same thing. This is the version
based on the 7.2.1 source that I have been testing for a while. If
there is a newer version than that I don't have it yet. The source is
not available so I can't verify the cause.

Merlin

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#21Merlin Moncure
merlin.moncure@rcsonline.com
In reply to: Bruce Momjian (#20)
hackers
#22Merlin Moncure
merlin.moncure@rcsonline.com
In reply to: Merlin Moncure (#21)
hackers
#23Tom Lane
tgl@sss.pgh.pa.us
In reply to: Merlin Moncure (#22)
hackers
#24Merlin Moncure
merlin.moncure@rcsonline.com
In reply to: Tom Lane (#23)
hackers
#25Tom Lane
tgl@sss.pgh.pa.us
In reply to: Merlin Moncure (#24)
hackers
#26Doug Royer
Doug@Royer.com
In reply to: Merlin Moncure (#24)
hackers
#27Merlin Moncure
merlin.moncure@rcsonline.com
In reply to: Doug Royer (#26)
hackers
#28Doug Royer
Doug@Royer.com
In reply to: Merlin Moncure (#27)
hackers
#29Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Tom Lane (#25)
hackers
#30Tom Lane
tgl@sss.pgh.pa.us
In reply to: Stephan Szabo (#29)
hackers