For Programmers: Free Programming Magazines  


Home > Archive > VC Language > May 2006 > How to ensure that a double is not 0.0?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to ensure that a double is not 0.0?
Richard Lewis Haggard

2006-05-29, 7:12 pm

I've got an old 16 bit application from a customer that appears to be
crashing at odd moments due to a divide by zero problem. It's been a long
time since I've done this, but I vaguely remember that it is considered to
be somewhat risky to directly ask if a double is exactly equal to zero. How
does one ensure that a double is not 0.0?
--
Richard Lewis Haggard
www.Haggard-And-Associates.com


Mark Randall

2006-05-29, 7:12 pm

"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:emnrTt3gGHA.3860@TK2MSFTNGP02.phx.gbl...
> I've got an old 16 bit application from a customer that appears to be
> crashing at odd moments due to a divide by zero problem. It's been a long
> time since I've done this, but I vaguely remember that it is considered to
> be somewhat risky to directly ask if a double is exactly equal to zero.
> How does one ensure that a double is not 0.0?


(((int)(dblVar * 1000)) != 0)

--
- Mark Randall
http://www.temporal-solutions.co.uk

"We're Systems and Networks..."
"It's our job to know..."


Victor Bazarov

2006-05-29, 7:12 pm

Mark Randall wrote:
> "Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
> news:emnrTt3gGHA.3860@TK2MSFTNGP02.phx.gbl...
>
> (((int)(dblVar * 1000)) != 0)


I think you're missing a couple of zeros there. Shouldn't it be

(((int)(dblVar * 1000)) != 000)

?

V
--
Please remove capital As from my address when replying by mail


Mark Randall

2006-05-29, 10:04 pm

"Victor Bazarov" wrote:
> I think you're missing a couple of zeros there. Shouldn't it be
>
> (((int)(dblVar * 1000)) != 000)


Eh? Octal?

But no, its just a nice simple check for if it is zero down to a certain
accuracy.

--
- Mark Randall
http://www.temporal-solutions.co.uk

"We're Systems and Networks..."
"It's our job to know..."


2006-05-29, 10:04 pm

File: C:\Program Files\vs8\vc\include\float.h

44: #define DBL_EPSILON 2.2204460492503131e-016
/* smallest such that 1.0+DBL_EPSILON != 1.0 */

56: #define FLT_EPSILON 1.192092896e-07F
/* smallest such that 1.0+FLT_EPSILON != 1.0 */

70: #define LDBL_EPSILON DBL_EPSILON
/* smallest such that 1.0+LDBL_EPSILON != 1.0 */


--
40th Floor - Software @ http://40th.com/
iPlay : the ultimate audio player for mobiles
parametric eq, xfeed, reverb; all on a mobile
Victor Bazarov

2006-05-29, 10:04 pm

Mark Randall wrote:
> "Victor Bazarov" wrote:
>
> Eh? Octal?


Like your regular '0' isn't!

> But no, its just a nice simple check for if it is zero down to a
> certain accuracy.


I thought you were joking. If division by zero is the problem, all
you need to check whether the divisor is _exactly_ zero, no "certain
accuracy" needed.

If FP overflow is the problem to tackle, then you need to check if the
divisor is smaller than the result of dividing the dividend by the
maximum FP value on your system.

V
--
Please remove capital As from my address when replying by mail


Mark Randall

2006-05-29, 10:04 pm

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote:
> Mark Randall wrote:
> I thought you were joking. If division by zero is the problem, all
> you need to check whether the divisor is _exactly_ zero, no "certain
> accuracy" needed.


Ah yes, thats what I get for posting at 3am....

I use a precision so that I dont accidently divide by 0.0000000000001 or
something like that and overflow an int that I may be casting to, just a
novelty really, ill go back to bed.

--
- Mark Randall
http://www.temporal-solutions.co.uk

"We're Systems and Networks..."
"It's our job to know..."


Abdo Haji-Ali

2006-05-30, 8:08 am

"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:emnrTt3gGHA.3860@TK2MSFTNGP02.phx.gbl...
> I've got an old 16 bit application from a customer that appears to be
> crashing at odd moments due to a divide by zero problem. It's been a long
> time since I've done this, but I vaguely remember that it is considered to
> be somewhat risky to directly ask if a double is exactly equal to zero.

How
> does one ensure that a double is not 0.0?

Excuse me. Am I missing something here? Does dividing a double by zero
actually crash a program? I thought that dividing a double by zero simply
returns 1.#INF (infinity) isn't it?

--
Abdo Haji-Ali
Programmer
In|Framez


Victor Bazarov

2006-05-30, 8:08 am

Abdo Haji-Ali wrote:
> "Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
> news:emnrTt3gGHA.3860@TK2MSFTNGP02.phx.gbl...
> Excuse me. Am I missing something here? Does dividing a double by zero
> actually crash a program? I thought that dividing a double by zero
> simply returns 1.#INF (infinity) isn't it?


Why ask? Try it and see...


Abdo Haji-Ali

2006-05-30, 7:15 pm

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:OMZ5VH$gGHA.3900@TK2MSFTNGP05.phx.gbl...
> Abdo Haji-Ali wrote:
>
> Why ask? Try it and see...
>
>

Already did, and it didn't throw an exception; which makes made me
wondering...

--
Abdo Haji-Ali
Programmer
In|Framez


Abdo Haji-Ali

2006-05-30, 7:15 pm

"Abdo Haji-Ali" <ahali@inframez.org_use_com_instead> wrote in message
news:O3vIwW$gGHA.5088@TK2MSFTNGP02.phx.gbl...
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:OMZ5VH$gGHA.3900@TK2MSFTNGP05.phx.gbl...
> Already did, and it didn't throw an exception; which makes made me
> wondering...

Sorry, I meant:
Already did, and it didn't throw an exception; which made me
wonder* what made Richard think it's caused by dividing double by 0...

--
Abdo Haji-Ali
Programmer
In|Framez


Richard Lewis Haggard

2006-05-30, 7:15 pm

Thanks guys.

I would think that the problem would manifest in a way that was dependent
upon the compiler and environment. In this case, the compiler is Borland
Turbo C++ 5 and the environment is a 16 bit DOS variant used in instruments.
Actually, the exception being thrown is a GPF (remember them?), not a divide
by zero exception so I suspect that the Borland math library is running off
the end of something while exceeding the limits of whatever it is that it is
trying to do. In any case, if I simply modify the code to check for a
denominator of something real close to zero, then I can do away with this
particular problem.
--
Richard Lewis Haggard
www.Haggard-And-Associates.com

"Abdo Haji-Ali" <ahali@inframez.org_use_com_instead> wrote in message
news:OpiVia$gGHA.2208@TK2MSFTNGP05.phx.gbl...
> "Abdo Haji-Ali" <ahali@inframez.org_use_com_instead> wrote in message
> news:O3vIwW$gGHA.5088@TK2MSFTNGP02.phx.gbl...
> Sorry, I meant:
> Already did, and it didn't throw an exception; which made me
> wonder* what made Richard think it's caused by dividing double by 0...
>
> --
> Abdo Haji-Ali
> Programmer
> In|Framez
>
>



Alexander Grigoriev

2006-05-30, 10:05 pm

By default, FP exceptions are disabled by RTL. You have to enable them
explicitly. By integer divide by zero is always enabled.

"Abdo Haji-Ali" <ahali@inframez.org_use_com_instead> wrote in message
news:O3vIwW$gGHA.5088@TK2MSFTNGP02.phx.gbl...
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:OMZ5VH$gGHA.3900@TK2MSFTNGP05.phx.gbl...
> Already did, and it didn't throw an exception; which makes made me
> wondering...
>
> --
> Abdo Haji-Ali
> Programmer
> In|Framez
>
>



David Webber

2006-05-31, 4:15 am


"Richard Lewis Haggard" <HaggardAtWorldDotStdDotCom> wrote in message
news:emnrTt3gGHA.3860@TK2MSFTNGP02.phx.gbl...

> I've got an old 16 bit application from a customer that appears to be
> crashing at odd moments due to a divide by zero problem. It's been a long
> time since I've done this, but I vaguely remember that it is considered to
> be somewhat risky to directly ask if a double is exactly equal to zero.
> How does one ensure that a double is not 0.0?


You *can* test

double x;
......
if( x == 0 ) ....

but numbers which close to zero may also be problematic. For example a/x
may not be infinite but may overflow. So you more often need a test like

if( abs(x) < epsilon )...

where epsilon is a small positive number which may need to be chosen
according to what you are about to do with x.

As an (extreme) example:

y = exp( -1.0/(x*x) );

In the mathematical expression, as x goes to zero then y goes to zero.
Trying to compute it like this for x=0 (or for very small x) would be a bad
idea. So you need

y = abs(x)<epsilon? 0: exp( -1.0/(x*x) );

In this particular case, in the original formula, y will be smaller than
some number delta if x < epsilon = 1/sqrt( log(1/delta) ) and so taking
delta to be around the smallest number the machine can distinguish from
zero, will generate you an appropriate epsilon. (Do it by hand!) In this
case it is a largish value and you can get by with smaller one and allow the
exp( -largenumber ) to give zero.

In general your program needs to know what the zero divide is telling you.
You may be able to calculate a valid answer by a diffferent route (as in
this example) or you may have to put up an "invalid input" message and stop.
Or you may just have to put up a "something has gone awry" message and stop.

Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm









Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com