473,466 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Complex numbers

I understand that C will suport complex values under iso99c. Does
anyone know of some examples that will show how to implement complex
functions?

Nov 17 '05 #1
3 1513
Richard Harris wrote:
I understand that C will suport complex values under iso99c. Does
anyone know of some examples that will show how to implement complex
functions?


Here's an example of how to use the complex number support:

#include <stdio.h>
#include <complex.h>

int main(void)
{
double minx = -1.7, maxx = 0.7, sx = (maxx - minx) / 50;
double miny = -1.0, maxy = 1.0, sy = (maxy - miny) / 70;
int i, maxi = 64;
double bail = 4.0;
for(double x = minx; x < maxx; x += sx)
{
for(double y = miny; y < maxy; y += sy)
{
complex double c = x + I * y;
complex double z = c;
for(i = 0; i < maxi && cabs(z) < bail; i++)
{
z = z * z + c;
}
putchar(i == maxi ? ' '
: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i % 36]);
}
putchar('\n');
}
return 0;
}

Here is the output:

22222222222223333333333344444455678 8765544444433333333333222222222222
22222222222333333333333444444555678 8765554444443333333333332222222222
22222222223333333333344444445556679 9766555444444433333333333222222222
22222222233333333333444444455566679 9766655544444443333333333322222222
2222222233333333333444444455566678A A876665554444444333333333332222222
2222222333333333334444445555667789C C987766555544444433333333333222222
222222333333333344444455555668HBACH HCABH86655555444444333333333322222
2222233333333334444455556666789BJOL LOJB987666655554444433333333332222
222233333333334444556667777789ADO ODA98777776665544443333333333222
223333333333344444568BBB9889BFDGIR RIGDFB9889BBB86544444333333333332
23333333333334444555689AGBBCE ECBBGA9865554444333333333333
333333333333444445556789EMY T T YME98765554444433333333333
333333333334444445556679C2 2C97665554444443333333333
333333333344444445556789BHN NHB98765554444444333333333
3333333334444444555668EEGLY YLGEE866555444444433333333
333333334444444455566789DQ0 0QD98766555444444443333333
333333334444444555566779TK1 1KT97766555544444443333333
3333333444444455556667789BI2 2IB987766655554444444333333
3333334444444555566667789ACP PCA987766665555444444433333
3333344444455555666777899ABCEI7 7IECBA998777666555554444443333
3333444444555566667889ABBFEHL 7 7 LHEFBBA988766665555444444333
333444444555666778AGCBL L L LBCGA87766655544444433
334444445567778889Q Q Q Q98887776554444443
34444445567AHJCBBBFK KFBBBCJHA7655444444
44444455567APEHHVI1 1IVHHEPA7655544444
44444555667ABZ ZBA7665554444
4444555666789CGO OGC987666555444
445555666789DEW WED98766655554
45555667778B B8777665555
55566888899B X X B9988886655
66679DDCHDCD DCDHCDD9766
7778ADP MN NM PDA877
A99BFH HFB99
BOEJ JEO
BNBCQHW U U WHQCBN
K9889BF S VGR RGV S FB9889
56667HBBAAABG GBAAABBH7666
55556667778AD DA8777666555
44455556677B B7766555544
444445556678ACF FCA87665554444
44444455566789BEM MEB9876655544444
3344444555667AL B M B LA766555444443
3334444455678AZ KJ A JK ZA876554444433
33333444456CESQFCD99ECI 9 NF98889FN 9 ICE99DCFQSEC6544443333
33333344445568B8777789E SABLHAD876666678DAHLBAS E9877778B8655444433333
333333334444555555566AAC87777666555555566677778CAA 66555555544443333333
33333333334444444455555555555555555555555555555555 55544444444333333333
33333333333334444444444444444444444444444444444444 44444444333333333333
22333333333333333444444444444444444444444444444444 44443333333333333332
22222333333333333333333333333344444444444333333333 33333333333333332222

--
Simon.
Nov 17 '05 #2

"Richard Harris" <rh*****@kahuna.sdsu.edu> wrote

I understand that C will suport complex values under iso99c. Does
anyone know of some examples that will show how to implement complex
functions?

You have to understand the maths first.

Probably the most important thing for the maths library is the exponential
function, e^x. This can be expanded

e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! ...

Now simply plug an imaginary value into the series, and you understand what
it means to exponentiate by an imaginary power.

Once you understand the maths, you also have to understand the limits of
your floating point system, in order to produce results of known accuracy,
and the limits of the series expansions - some are acceptable to the
mathematician but in fact converge too slowly to be of practical use to the
computer programmer.

So to make a loose implementation is quite simple, but to code an industrial
strength complex maths library is more difficult.
Nov 23 '05 #3
In article <dm**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com> "Malcolm" <re*******@btinternet.com> writes:
Probably the most important thing for the maths library is the exponential
function, e^x. This can be expanded

e^x = 1 + x + x^2/2! + x^3/3! + x^4/4! ...

Now simply plug an imaginary value into the series, and you understand what
it means to exponentiate by an imaginary power.


Oh. I would simply go for:
exp(real_part(z)) * (cos(imag_part(z)) + i * sin(imag_part(z)))
or something like that. Industrial strength and all that stuff.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
5
by: Todd Steury | last post by:
Greetings Python'ers: I'm just an amature who occasionally uses Python for complex mathematical models. The current model I'm working with occasionally generates really large numbers that are...
17
by: Chris Travers | last post by:
Hi all; I just made an interesting discovery. Not sure if it is a good thing or not, and using it certainly breakes first normal form.... Not even sure if it really works. However, as I am...
6
by: gc | last post by:
Hi, Why didn't the committee propose a new type for complex numbers with integer components? thanks, gc
1
by: seia0106 | last post by:
Hello, I have an array X=, whose even and odd indices should represent real and imaginary parts of complex numbers. This I want to use in a routine that uses the typedef double Cx; for storing...
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
7
by: schaefer.mp | last post by:
To compute the absolute value of a negative base raised to a fractional exponent such as: z = (-3)^4.5 you can compute the real and imaginary parts and then convert to the polar form to get...
25
by: jacob navia | last post by:
The C99 standard forgot to define the printf equivalent for complex numbers Since I am revising the lcc-win implementation of complex numbers I decided to fill this hole with "Z" for...
9
by: void main | last post by:
I'm rather new to complex numbers in C and was wondering, how do I initialize a complex variable properly if the imaginary part is 0. I tried -------- #include <complex.h> float complex c...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.