Re: division by zero mystery
- From: "user923005" <dcorbit@xxxxxxxxx>
- Date: 28 Dec 2006 16:35:18 -0800
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?
JS
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.
See also:
http://www.hmug.org/man/3/atan2.php
http://www.hmug.org/man/3/atan.php
.
- Follow-Ups:
- Re: division by zero mystery
- From: CBFalconer
- Re: division by zero mystery
- References:
- division by zero mystery
- From: John Smith
- division by zero mystery
- Prev by Date: division by zero mystery
- Next by Date: Re: division by zero mystery
- Previous by thread: division by zero mystery
- Next by thread: Re: division by zero mystery
- Index(es):