Re: division by zero mystery



user923005 wrote:
John Smith wrote:

As a hobby project, I am writing a C library of functions that do
operations on complex numbers. I am also porting this code to
Visual Basic (v.6) in the form of a calculator. I'm seeing
something very odd. Code that generates a division by zero error
in VB seems to work correctly in the C routines. For example, it
is sometimes necessary to evaluate:

x = atan(b / a) (where a and b are the real and imag parts
of a complex number)

This generates a division by zero error in VB when the complex
operand is, for example, 0.0 + 1.0i but seems to work correctly
in the C routines. That is, I can calculate i ^ i correctly with
the C code but get a division by zero error when atan() is
evaluated in VB. Any thoughts on why this is happening? Is it
just "lucky" undefined behaviour?

There are two formats for arctangent. If your 'a' component can
be zero, you are supposed to use the format that takes two
arguments. Here is a VB howto for atan2:
http://vb-helper.com/howto_atan2.html

For C, you just use atan2(a,b) instead. Division by zero will
throw a floating point exception, so you probably have exceptions
turned off. Depending on the compiler it can change the behavior.
With only one arg, atan() cannot guess the right quadrant like
atan2() can.

Or the FP unit can return +-INF for the division operation, and the
atan routine can recognize that and return the appropriate value.
Not luck, just QOI.

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>


.



Relevant Pages

  • Re: DataColumn.Expression error handling
    ... Thus you might modify it a bit just to avoid an exception: ... classic division by zero error. ... being evaluated regardless of the condition. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: DataColumn.Expression error handling
    ... > This is vb behaviour where IIF is a procedure that you pass parameters to. ... > Thus you might modify it a bit just to avoid an exception: ... > classic division by zero error. ...
    (microsoft.public.dotnet.framework.adonet)
  • division by zero mystery
    ... I am writing a C library of functions that do operations on complex numbers. ... I am also porting this code to Visual Basic in the form of a calculator. ... This generates a division by zero error in VB when the complex operand is, for example, 0.0 + 1.0i but seems to work correctly in the C routines. ...
    (comp.programming)
  • JDBC and handling of errors
    ... generates a divide by zero error in QA, but does not produce an exception in the Java program. ...
    (microsoft.public.sqlserver.clients)