Re: division by zero mystery
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 29 Dec 2006 01:20:00 -0500
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>
.
- Follow-Ups:
- Re: division by zero mystery
- From: user923005
- Re: division by zero mystery
- References:
- division by zero mystery
- From: John Smith
- Re: division by zero mystery
- From: user923005
- division by zero mystery
- Prev by Date: Re: division by zero mystery
- Next by Date: How to program an enigma cipher?
- Previous by thread: Re: division by zero mystery
- Next by thread: Re: division by zero mystery
- Index(es):
Relevant Pages
|
|