Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #55206

Re: complex number in struct form in C

From "Osmium" <r124c4u102@comcast.net>
Newsgroups comp.lang.c
Subject Re: complex number in struct form in C
Date 2014-12-21 10:54 -0600
Message-ID <cfoc9cFtn8mU1@mid.individual.net> (permalink)
References <bb5abeac-08cd-43de-a54a-5efd27423d80@googlegroups.com> <m76sib$3i6$1@dont-email.me> <8a62bd37-0803-47ec-b52b-20e802f34f03@googlegroups.com>

Show all headers | View raw


"Dexterr" wrote:

> On Sunday, December 21, 2014 5:30:32 PM UTC+1, James Kuyper wrote:
>> On 12/21/2014 10:20 AM, Dexterr wrote:
>> > Hi there, I am thinking how to make one structure with complex number. 
>> > Complex number has a form (a+b*i) and i is an imaginary number i = 
>> > sqrt(-1).
>> >
>> > struct ComplexNumber {
>> >
>> >    int a;
>> >    int b;
>> >
>> > } CNumber;
>> >
>> > Do you have an idea how to do it properly ?
>>
>> If you need to have ints, you could do something like that, but
>> situations where using integers for that purpose would be useful are
>> extremely rare. If you think it's a good idea, you're probably (but not
>> necessarily) badly mistaken.
>>
>> It you take that route, you'll have to write routines to perform all of
>> the normal complex math operations on ComplexNumber objects. Complex
>> division is going to be especially difficult if you insist on using
>> integers.
>>
>> It's far more conventional to represent the components of a complex
>> number using floating point, which is why support for complex numbers
>> with floating point components was add in C99. Therefore, if you're
>> willing to adopt the more convenient, it's much simpler to use:
>>
>> _Complex double CNumber;
>>
>> and let the compiler worry about such things for me. Also, <cmath.h>
>> contains complexified versions of the routines in <math.h>.
>> -- 
>> James Kuyper
>
> Thx James,
>
> it should be something like this
>
> typedef struct _ComplexNumber_ {
>  float real_part;
>  float imaginary_part;
> } CNumber;
>
> CNumber complex;

The convention in C, is for real numbers to be represented by double, not 
float.  Look at the types of parameters and such in <math.h>

If "real number" is new to you, look it up, Wikipedia is good. 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

complex number in struct form in C Dexterr <dejangru@gmail.com> - 2014-12-21 07:20 -0800
  Re: complex number in struct form in C James Kuyper <jameskuyper@verizon.net> - 2014-12-21 11:30 -0500
    Re: complex number in struct form in C Dexterr <dejangru@gmail.com> - 2014-12-21 08:35 -0800
      Re: complex number in struct form in C "Osmium" <r124c4u102@comcast.net> - 2014-12-21 10:54 -0600
        Re: complex number in struct form in C James Kuyper <jameskuyper@verizon.net> - 2014-12-21 12:00 -0500
          Re: complex number in struct form in C "Osmium" <r124c4u102@comcast.net> - 2014-12-21 11:08 -0600
            Re: complex number in struct form in C Barry Schwarz <schwarzb@dqel.com> - 2014-12-21 10:12 -0800
              Re: complex number in struct form in C "Osmium" <r124c4u102@comcast.net> - 2014-12-21 13:22 -0600
                Re: complex number in struct form in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 22:21 +0000
              Re: complex number in struct form in C asetofsymbols@gmail.com - 2014-12-26 11:34 -0800
                Re: complex number in struct form in C "Osmium" <r124c4u102@comcast.net> - 2014-12-26 14:10 -0600
                Re: complex number in struct form in C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-28 07:04 +0000
                Re: complex number in struct form in C BartC <bc@freeuk.com> - 2014-12-28 13:14 +0000
                Re: complex number in struct form in C Robert Wessel <robertwessel2@yahoo.com> - 2014-12-28 10:45 -0600
                Re: complex number in struct form in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-28 08:56 -0800
                Re: complex number in struct form in C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-29 10:10 +0000
                Re: complex number in struct form in C Kaz Kylheku <kaz@kylheku.com> - 2014-12-29 19:40 +0000
                Re: complex number in struct form in C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-29 20:54 +0000
                Re: complex number in struct form in C BartC <bc@freeuk.com> - 2014-12-29 21:55 +0000
                Re: complex number in struct form in C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-30 03:07 +0000
                Re: complex number in struct form in C BartC <bc@freeuk.com> - 2014-12-30 10:51 +0000
                Re: complex number in struct form in C Rosario193 <Rosario@invalid.invalid> - 2014-12-30 12:15 +0100
                Re: complex number in struct form in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-30 03:31 -0800
                Re: complex number in struct form in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-30 12:17 +0000
                Re: complex number in struct form in C BartC <bc@freeuk.com> - 2014-12-30 13:18 +0000
                Re: complex number in struct form in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-30 05:28 -0800
                Re: complex number in struct form in C BartC <bc@freeuk.com> - 2014-12-30 13:53 +0000
                Re: complex number in struct form in C "Osmium" <r124c4u102@comcast.net> - 2014-12-30 08:53 -0600
                Re: complex number in struct form in C "Chris M. Thomasson" <no@spam.invalid> - 2014-12-30 20:05 -0800
                Re: complex number in struct form in C Jeff Higgins <jeff@invalid.invalid> - 2014-12-30 17:40 -0500
                Re: complex number in struct form in C Tim Rentsch <txr@alumni.caltech.edu> - 2015-03-24 08:58 -0700
                Re: complex number in struct form in C Les Cargill <lcargill99@comcast.com> - 2015-03-24 19:41 -0500
                Re: complex number in struct form in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-30 14:59 +0000
                Re: complex number in struct form in C David Thompson <dave.thompson2@verizon.net> - 2015-01-12 15:33 -0500
                Re: complex number in struct form in C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-01-12 22:51 +0000
                Re: complex number in struct form in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-01-12 23:34 +0000
                Re: complex number in struct form in C James Kuyper <jameskuyper@verizon.net> - 2014-12-26 15:25 -0500
                Re: complex number in struct form in C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-28 07:17 +0000
                Re: complex number in struct form in C BartC <bc@freeuk.com> - 2014-12-26 20:49 +0000
                Re: complex number in struct form in C asetofsymbols@gmail.com - 2014-12-27 00:22 -0800
                Re: complex number in struct form in C "Osmium" <r124c4u102@comcast.net> - 2014-12-27 08:25 -0600
                Re: complex number in struct form in C Geoff <geoff@invalid.invalid> - 2014-12-27 10:20 -0800
                Re: complex number in struct form in C "Osmium" <r124c4u102@comcast.net> - 2014-12-27 13:21 -0600
                Re: complex number in struct form in C Geoff <geoff@invalid.invalid> - 2014-12-27 13:11 -0800
                Re: complex number in struct form in C "Osmium" <r124c4u102@comcast.net> - 2014-12-27 15:36 -0600
                Re: complex number in struct form in C Geoff <geoff@invalid.invalid> - 2014-12-27 14:54 -0800
                Re: complex number in struct form in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-27 15:45 -0800
                Re: complex number in struct form in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-27 13:40 -0800
                Re: complex number in struct form in C James Kuyper <jameskuyper@verizon.net> - 2014-12-28 06:50 -0500
                Re: complex number in struct form in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-28 04:25 -0800
                Re: complex number in struct form in C James Kuyper <jameskuyper@verizon.net> - 2014-12-28 06:44 -0500
                Re: complex number in struct form in C Richard Damon <Richard@Damon-Family.org> - 2014-12-28 08:33 -0500
                Re: complex number in struct form in C James Kuyper <jameskuyper@verizon.net> - 2014-12-27 11:46 -0500
                Re: complex number in struct form in C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-28 07:28 +0000
                Re: complex number in struct form in C Kaz Kylheku <kaz@kylheku.com> - 2014-12-27 17:23 +0000
            Re: complex number in struct form in C James Kuyper <jameskuyper@verizon.net> - 2014-12-21 14:37 -0500
          Re: complex number in struct form in C Keith Thompson <kst-u@mib.org> - 2014-12-21 12:37 -0800
          Re: complex number in struct form in C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-12-22 00:54 +0000
      Re: complex number in struct form in C James Kuyper <jameskuyper@verizon.net> - 2014-12-21 11:54 -0500
      Re: complex number in struct form in C Vincenzo Mercuri <nomail@yahoo.it> - 2014-12-21 18:07 +0100
        Re: complex number in struct form in C Dexterr <dejangru@gmail.com> - 2014-12-21 10:37 -0800
          Re: complex number in struct form in C "Chris M. Thomasson" <no@spam.invalid> - 2014-12-21 10:55 -0800
          Re: complex number in struct form in C raltbos@xs4all.nl (Richard Bos) - 2014-12-22 12:15 +0000
            Re: complex number in struct form in C Dexterr <dejangru@gmail.com> - 2014-12-22 05:51 -0800
              Re: complex number in struct form in C BartC <bc@freeuk.com> - 2014-12-22 14:01 +0000
                Re: complex number in struct form in C Keith Thompson <kst-u@mib.org> - 2014-12-22 08:02 -0800
            Re: complex number in struct form in C Keith Thompson <kst-u@mib.org> - 2014-12-22 08:03 -0800
    Re: complex number in struct form in C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-12-21 22:27 +0000
  Re: complex number in struct form in C "Chris M. Thomasson" <no@spam.invalid> - 2014-12-21 10:51 -0800
    Re: complex number in struct form in C Dexterr <dejangru@gmail.com> - 2014-12-21 11:14 -0800
  Re: complex number in struct form in C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-12-21 17:17 -0800

csiph-web