Re: Divide by zero...

From: Robert Creager <Robert_Creager(at)LogicalChaos(dot)org>
To: Mike Leahy <mgleahy(at)fes(dot)uwaterloo(dot)ca>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Divide by zero...
Date: 2003-09-30 14:06:25
Message-ID: 20030930080625.7b890f46.Robert_Creager@LogicalChaos.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

When grilled further on (Tue, 30 Sep 2003 09:26:19 -0400),
Mike Leahy <mgleahy(at)fes(dot)uwaterloo(dot)ca> confessed:

> I have a query that calculates various using variables from a survey
> database. As with any survey, there are many instantces of null values. I'm
> wondering if there is any way to escape the error caused by dividing by zero
> or null values. The specific message i get is:
>
> ERROR: floating point exception! The last floating point operation either
> exceeded legal ranges or was a divide by zero
>
> Is there a simple trick that won't make my queries excessively complex?

I believe CASE and COALESCE will solve your problem. Something like this:

SELECT CASE COALESCE( denom, 0.0 )
WHEN 0.0 THEN 0.0
ELSE COALESCE( num, 0.0 ) / denom
END
FROM some_table;

Cheers,
Rob

--
07:48:16 up 60 days, 19 min, 4 users, load average: 2.32, 2.78, 2.97

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-09-30 14:07:51 Re: ADD FOREIGN KEY (was Re: [GENERAL] 7.4Beta)
Previous Message Tom Lane 2003-09-30 13:58:01 Re: ADD FOREIGN KEY (was Re: [GENERAL] 7.4Beta)