* Re: complex variable
[not found] <20040906134525.33692.qmail@web52908.mail.yahoo.com>
@ 2004-09-06 16:25 ` Ankit Jain
2004-09-06 16:37 ` Jan-Benedict Glaw
0 siblings, 1 reply; 14+ messages in thread
From: Ankit Jain @ 2004-09-06 16:25 UTC (permalink / raw)
To: linux-c-programming
> 1 #include<stdlib.h>
> 2 #include<complex.h>
> 3 #include<stdio.h>
> 4
> 5 int main()
> 6 {
> 7 FILE *fp;
> 8 fp=fopen("test_data.dat","rb");
> 9 int N=32,i=0;
> 10 complex x,*in;
> 11 in=malloc(sizeof(complex)*N);
> 12 if (fread(in,16,32,fp) != 32)
> 13 printf("error in reading from file
> failed\n");
> 14 else{
> 15 for(i=0;i<N;i++)
> 16 printf("%lf\t",*(in+i+0));
> 17 }
> 18 return 0;
> 19 }
>
> i am sorry but this code also gives the same error
>
> error in reading from file failed
>
> ankit
________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-06 16:25 ` complex variable Ankit Jain
@ 2004-09-06 16:37 ` Jan-Benedict Glaw
0 siblings, 0 replies; 14+ messages in thread
From: Jan-Benedict Glaw @ 2004-09-06 16:37 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 2257 bytes --]
On Mon, 2004-09-06 17:25:32 +0100, Ankit Jain <ankitjain1580@yahoo.com>
wrote in message <20040906162532.22345.qmail@web52904.mail.yahoo.com>:
> > 1 #include<stdlib.h>
> > 2 #include<complex.h>
> > 3 #include<stdio.h>
> > 4
> > 5 int main()
> > 6 {
> > 7 FILE *fp;
> > 8 fp=fopen("test_data.dat","rb");
> > 9 int N=32,i=0;
> > 10 complex x,*in;
> > 11 in=malloc(sizeof(complex)*N);
You need to check the "in" pointer -- malloc() may fail!
> > 12 if (fread(in,16,32,fp) != 32)
"16" should read "sizeof (complex)". If you don't do that, you loose any
portability (for no added value). Additionally, both "32" should be "N",
since that's what you really want to read, what you really malloc()ed.
Also (from personal experience), I can only strongly advise you not to
read in (or write out) numbers like that. All the data types may look
different on different CPUs or operating systems, so better stick to an
encoding that's common to all of them.
Define an own encoding for those values (eg. printf() out their real and
imaginary part as decimal floating-point values and translate these back
to "complex" values). It's a real *nightmare*.
> > 13 printf("error in reading from file
> > failed\n");
> > 14 else{
> > 15 for(i=0;i<N;i++)
> > 16 printf("%lf\t",*(in+i+0));
Others would have used that as in[i] ... Easier to read:-)
> > 17 }
> > 18 return 0;
> > 19 }
> >
> > i am sorry but this code also gives the same error
> >
> > error in reading from file failed
Even while your programs "features" a good number of typical no-no's,
it should probably work. Are you sure the file is long enough? Could you
provide a strace from this program being run?
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* RE: complex variable
@ 2004-09-07 15:37 Huber, George K RDECOM CERDEC STCD SRI
2004-09-07 16:05 ` Jan-Benedict Glaw
2004-09-08 9:59 ` Charlie Gordon
0 siblings, 2 replies; 14+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-07 15:37 UTC (permalink / raw)
To: linux-c-programming
> 1 #include<stdlib.h>
> 2 #include<complex.h>
> 3 #include<stdio.h>
> 4
> 5 int main()
> 6 {
> 7 FILE *fp;
> 8 fp=fopen("test_data.dat","rb");
> 9 int N=32,i=0;
> 10 complex x,*in;
> 11 in=malloc(sizeof(complex)*N);
> 12 if (fread(in,16,32,fp) != 32)
> 13 printf("error in reading from file
> failed\n");
> 14 else{
> 15 for(i=0;i<N;i++)
> 16 printf("%lf\t",*(in+i+0));
> 17 }
> 18 return 0;
> 19 }
>
> i am sorry but this code also gives the same error
>
Hi,
In addition to the previously given suggestions, I would suggest checking
the return
value of fopen (it can fail), as well as printing out the error codes. For
example,
I would recode the above as (note - this is uncompiled/tested code but is
should
give an example of what I mean):
#include<stdlib.h>
#include<complex.h>
#include<stdio.h>
#include<errno.h>
int main()
{
FILE* fp=NULL;
char szName[] = "test_data.dat";
if(NULL != (fp = fopen(szName, "rb")))
{
int N=32, i=0;
complex x, *in = NULL;
if(NULL != (in=malloc(sizeof(complex)*N)))
{
if(N == fread(in, sizeof(complex), N, fp))
{
for(i=0; i< N; i++)
printf("%lf\t", in[i]);
}
else
{
fprintf(stderr, "Read failed. error: %d\n%s\n", errno,
strerror(errno));
}
}
else
{
fprintf(stderr, "Failed to allocate space for buffer\n");
}
}
else
{
fprintf(stderr, "Failed to open %s. Error %d\n%s\n",
szName, errno, strerror(errno));
}
return 0;
}
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-07 15:37 Huber, George K RDECOM CERDEC STCD SRI
@ 2004-09-07 16:05 ` Jan-Benedict Glaw
2004-09-08 9:59 ` Charlie Gordon
1 sibling, 0 replies; 14+ messages in thread
From: Jan-Benedict Glaw @ 2004-09-07 16:05 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1404 bytes --]
On Tue, 2004-09-07 11:37:06 -0400, Huber, George K RDECOM CERDEC STCD SRI <George.K.Huber@us.army.mil>
wrote in message <E3E30069B061524E90BCEE4417E30661138525@monm207.monmouth.army.mil>:
> In addition to the previously given suggestions, I would suggest checking
> the return
> value of fopen (it can fail), as well as printing out the error codes. For
> example,
> I would recode the above as (note - this is uncompiled/tested code but is
> should
> give an example of what I mean):
Error checking is always a good idea :-)
> #include<stdlib.h>
> #include<complex.h>
> #include<stdio.h>
> #include<errno.h>
>
> int main()
> {
> FILE* fp=NULL;
No need to initialize *fp to NULL because you assign a value before it's
read for the first time. That'll only bloat the program :)
> char szName[] = "test_data.dat";
>
> if(NULL != (fp = fopen(szName, "rb")))
> {
> int N=32, i=0;
> complex x, *in = NULL;
> if(NULL != (in=malloc(sizeof(complex)*N)))
Same here -- no need to initialize *in.
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-07 15:37 Huber, George K RDECOM CERDEC STCD SRI
2004-09-07 16:05 ` Jan-Benedict Glaw
@ 2004-09-08 9:59 ` Charlie Gordon
2004-09-08 12:06 ` Jan-Benedict Glaw
1 sibling, 1 reply; 14+ messages in thread
From: Charlie Gordon @ 2004-09-08 9:59 UTC (permalink / raw)
To: linux-c-programming
"Huber, George K RDECOM CERDEC STCD SRI" <George.K.Huber@us.army.mil> wrote
in message
news:E3E30069B061524E90BCEE4417E30661138525@monm207.monmouth.army.mil...
> #include<stdlib.h>
> #include<complex.h>
> #include<stdio.h>
> #include<errno.h>
>
> int main()
> {
> FILE* fp=NULL;
The extra initialization to NULL is useless, as JBG points out, as a few
other ones (i, in) and even some unused variables (x)
> char szName[] = "test_data.dat";
This is quite inefficient : declaring a local initialized array will produce
equivalent code to:
char szName[sizeof("test_data.dat")];
memcpy(szName, "test_data.dat", sizeof("test_data.dat"));
Whereas declaring a const char* fileName = "test_data.dat" will likely get
optimized by the compiler as not even storing the address into a local
variable.
>
> if(NULL != (fp = fopen(szName, "rb")))
This style is really ugly! there is really no advantage at combining the
assignment and the test:
fp = fopen(fileName, "rb");
if (fp != NULL) {
> {
> int N=32, i=0;
> complex x, *in = NULL;
Are you sure about the complex type implementation ?
Are the real and imaginary parts float, double, of even long double ?
Reading them in bulk from the file is definitely not advisable, because of
this and because of byte ordering issues.
> if(NULL != (in=malloc(sizeof(complex)*N)))
N as the uppercase name implies seems to be a constant in this code...
why not make it a const outside this block and declare in as a local array
instead of allocating it off the heap.
> {
> if(N == fread(in, sizeof(complex), N, fp))
> {
> for(i=0; i< N; i++)
> printf("%lf\t", in[i]);
What does this mean ?
C99 makes no difference between %f and %lf
I'm not sure what passing a complex to printf will actually do. Most lilely
not what you want and definitely not portable.
> }
> else
> {
> fprintf(stderr, "Read failed. error: %d\n%s\n", errno,
> strerror(errno));
> }
> }
> else
> {
> fprintf(stderr, "Failed to allocate space for buffer\n");
> }
> }
> else
> {
> fprintf(stderr, "Failed to open %s. Error %d\n%s\n",
> szName, errno, strerror(errno));
> }
>
> return 0;
> }
Cheers,
Chqrlie.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-08 9:59 ` Charlie Gordon
@ 2004-09-08 12:06 ` Jan-Benedict Glaw
2004-09-08 16:25 ` Ankit Jain
2004-09-08 21:54 ` Charlie Gordon
0 siblings, 2 replies; 14+ messages in thread
From: Jan-Benedict Glaw @ 2004-09-08 12:06 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 3064 bytes --]
On Wed, 2004-09-08 11:59:48 +0200, Charlie Gordon <gmane@chqrlie.org>
wrote in message <chml6d$kri$1@sea.gmane.org>:
> > char szName[] = "test_data.dat";
>
> This is quite inefficient : declaring a local initialized array will produce
> equivalent code to:
>
> char szName[sizeof("test_data.dat")];
> memcpy(szName, "test_data.dat", sizeof("test_data.dat"));
>
> Whereas declaring a const char* fileName = "test_data.dat" will likely get
> optimized by the compiler as not even storing the address into a local
> variable.
But remember there's a subtle difference between
char filename[] = "/path/to/file";
and
char *filename = "/path/to/file";
You may write to the first one (eg. later on use snprintf and friends
with it), but you may not write to the 2nd one!
> > if(NULL != (fp = fopen(szName, "rb")))
>
> This style is really ugly! there is really no advantage at combining the
> assignment and the test:
> fp = fopen(fileName, "rb");
> if (fp != NULL) {
That's somewhat personal style. Also notice that he's got the NULL at
the beginnnig of the comparison, not at the end. That's something that
seems to be tought these days, because assignments (if you mis-write
"!=" as "=!" or "|=") will produce compile-time errors (NULL isn't a
lvalue).
> > {
> > int N=32, i=0;
> > complex x, *in = NULL;
>
> Are you sure about the complex type implementation ?
> Are the real and imaginary parts float, double, of even long double ?
> Reading them in bulk from the file is definitely not advisable, because of
> this and because of byte ordering issues.
For now, ignore the fact that in[] is wrongly accessed. It'll somewhat
work (while being non-portable), I guess this code is here to only have
a useful test program :-)
> > if(NULL != (in=malloc(sizeof(complex)*N)))
>
> N as the uppercase name implies seems to be a constant in this code...
> why not make it a const outside this block and declare in as a local array
> instead of allocating it off the heap.
A #define would have served, too, as well as a const int. Test code
*needs* to be a bit ugliysh to qualify as test code :-)
> > {
> > if(N == fread(in, sizeof(complex), N, fp))
> > {
> > for(i=0; i< N; i++)
> > printf("%lf\t", in[i]);
>
> What does this mean ?
> C99 makes no difference between %f and %lf
> I'm not sure what passing a complex to printf will actually do. Most lilely
> not what you want and definitely not portable.
Works for now, see the ugliness clause above. (Sure, you don't want that
code in your program's final version, though...)
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-08 12:06 ` Jan-Benedict Glaw
@ 2004-09-08 16:25 ` Ankit Jain
2004-09-08 21:54 ` Charlie Gordon
1 sibling, 0 replies; 14+ messages in thread
From: Ankit Jain @ 2004-09-08 16:25 UTC (permalink / raw)
To: Jan-Benedict Glaw; +Cc: linux prg
thanks....
but sorry sir i could not understand one thing which u
also clearly detected. it is "in" variable....its a
complex variable. this is somewhat not correct
complex x, *in = NULL;
but i wanted to ask if i want to use in as a 2D array
how to do that?
Ankit
--- Jan-Benedict Glaw <jbglaw@lug-owl.de> wrote:
> On Wed, 2004-09-08 11:59:48 +0200, Charlie Gordon
> <gmane@chqrlie.org>
> wrote in message <chml6d$kri$1@sea.gmane.org>:
>
> > > char szName[] = "test_data.dat";
> >
> > This is quite inefficient : declaring a local
> initialized array will produce
> > equivalent code to:
> >
> > char szName[sizeof("test_data.dat")];
> > memcpy(szName, "test_data.dat",
> sizeof("test_data.dat"));
> >
> > Whereas declaring a const char* fileName =
> "test_data.dat" will likely get
> > optimized by the compiler as not even storing the
> address into a local
> > variable.
>
> But remember there's a subtle difference between
>
> char filename[] = "/path/to/file";
>
> and
> char *filename = "/path/to/file";
>
>
> You may write to the first one (eg. later on use
> snprintf and friends
> with it), but you may not write to the 2nd one!
>
> > > if(NULL != (fp = fopen(szName, "rb")))
> >
> > This style is really ugly! there is really no
> advantage at combining the
> > assignment and the test:
> > fp = fopen(fileName, "rb");
> > if (fp != NULL) {
>
> That's somewhat personal style. Also notice that
> he's got the NULL at
> the beginnnig of the comparison, not at the end.
> That's something that
> seems to be tought these days, because assignments
> (if you mis-write
> "!=" as "=!" or "|=") will produce compile-time
> errors (NULL isn't a
> lvalue).
>
> > > {
> > > int N=32, i=0;
> > > complex x, *in = NULL;
> >
> > Are you sure about the complex type implementation
> ?
> > Are the real and imaginary parts float, double, of
> even long double ?
> > Reading them in bulk from the file is definitely
> not advisable, because of
> > this and because of byte ordering issues.
>
> For now, ignore the fact that in[] is wrongly
> accessed. It'll somewhat
> work (while being non-portable), I guess this code
> is here to only have
> a useful test program :-)
>
> > > if(NULL != (in=malloc(sizeof(complex)*N)))
> >
> > N as the uppercase name implies seems to be a
> constant in this code...
> > why not make it a const outside this block and
> declare in as a local array
> > instead of allocating it off the heap.
>
> A #define would have served, too, as well as a const
> int. Test code
> *needs* to be a bit ugliysh to qualify as test code
> :-)
>
> > > {
> > > if(N == fread(in, sizeof(complex),
> N, fp))
> > > {
> > > for(i=0; i< N; i++)
> > > printf("%lf\t", in[i]);
> >
> > What does this mean ?
> > C99 makes no difference between %f and %lf
> > I'm not sure what passing a complex to printf will
> actually do. Most lilely
> > not what you want and definitely not portable.
>
> Works for now, see the ugliness clause above. (Sure,
> you don't want that
> code in your program's final version, though...)
>
> MfG, JBG
>
> --
> Jan-Benedict Glaw jbglaw@lug-owl.de .
> +49-172-7608481 _ O _
> "Eine Freie Meinung in einem Freien Kopf | Gegen
> Zensur | Gegen Krieg _ _ O
> fuer einen Freien Staat voll Freier Bürger" | im
> Internet! | im Irak! O O O
> ret = do_actions((curr | FREE_SPEECH) &
> ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
>
> ATTACHMENT part 2 application/pgp-signature
name=signature.asc
________________________________________________________________________
Yahoo! Messenger - Communicate instantly..."Ping"
your friends today! Download Messenger Now
http://uk.messenger.yahoo.com/download/index.html
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 14+ messages in thread
* RE: complex variable
@ 2004-09-08 18:40 Huber, George K RDECOM CERDEC STCD SRI
2004-09-10 20:09 ` Jan-Benedict Glaw
0 siblings, 1 reply; 14+ messages in thread
From: Huber, George K RDECOM CERDEC STCD SRI @ 2004-09-08 18:40 UTC (permalink / raw)
To: linux-c-programming
Hi,
First (for Charlie), I attempted to keep as much of Ankit's code consistent
from his posting to my sample code - including unused variables,
non-portable
constructs, use of N instead of a #define
As both Charlie and Jan-Benedict both noted doing an declaration and
defination
of the pointers is not necessary - I plead old habits developed when working
with various microsoft compilers. What can I say - old habits die hard.
Now for Ankits question:
>but sorry sir i could not understand one thing which u
>also clearly detected. it is "in" variable....its a
>complex variable. this is somewhat not correct
>complex x, *in = NULL;
>but i wanted to ask if i want to use in as a 2D array
>how to do that?
Assuming that you want to create a two dimensional array of complex types
(with
the added assumption that number of rows is not known at compile time), I
would
do something along these lines. To create a dynamic two-dimensional array,
you
allocate enough memory to hold all of the elements and manage the space
yourself.
For example, if you wanted to create a 3 rows by 2 column array you would
need
to allocate space for six elements. Because C lays out it arrays in
row-major
for, the space would be:
+-----+-----+-----+-----+-----+-----+
| 0,0 | 0,1 | 1,0 | 1,1 | 2,0 | 2,1 |
+-----+-----+-----+-----+-----+-----+
0 1 2 3 4 5
with the corresponding index shown below. To make life easy, you need to
create
a function that will take the a two-dimensional index, i.e. (row, col), and
convert
it to the corresponding one-dimensional index. I tend to use a macro to do
this,
#define MakeIndex(r,c) (r)*NUM_COLS + (y)
Now if I wanted to create the above two-dimensional array, I would allocate
space
for it like this (I have used fixed values for number of columns and number
of
rows for demonstration purposes only. If these values were set at compile
time it
would be easier to do complex in[3][2])
complex* in;
int nRows = 3;
int nCols = 2;
in = malloc(sizeof(complex)*3*2);
Now having created a region in memory to hold by two dimensional array we
can do things
like:
complex var1, var2;
in[MakeIndex(0,0)] = var1; // assign a value into the 2D array at row
0 column 0
var2 = in[MakeIndex(1,1)]; // access a vlue in the 2D array at row 1
column 1
Note that error checking has not been performed. It is up to the
application to insure
that bounds are not overstepped. For example, element (1,2) does not exist
as this would
imply the array having three columns.
As I said earlier, if both the number of rows and number of columns are know
at compile
time things become much easier. You can use the normal square-bracket
notation to access
elements.
Hope this helps
George
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-08 12:06 ` Jan-Benedict Glaw
2004-09-08 16:25 ` Ankit Jain
@ 2004-09-08 21:54 ` Charlie Gordon
2004-09-10 20:05 ` Jan-Benedict Glaw
1 sibling, 1 reply; 14+ messages in thread
From: Charlie Gordon @ 2004-09-08 21:54 UTC (permalink / raw)
To: linux-c-programming
"Jan-Benedict Glaw" <jbglaw@lug-owl.de> wrote in message
news:20040908120658.GZ6985@lug-owl.de...
> > > for(i=0; i< N; i++)
> > > printf("%lf\t", in[i]);
> > What does this mean ?
> Works for now, see the ugliness clause above. (Sure,
> you don't want that
> code in your program's final version, though...)
Enlighten me: does it just print the real part of each complex in the array
? Ankit: was that the intent ?
it's ugly because the whole complex number in whatever storage format it's
in gets passed to printf.
In my understanding, this may fail because there is no guaranty about which
part of the structure will be printed and the format specifier may be
incorrect for the actual type thus passed.
Chqrlie.
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-08 21:54 ` Charlie Gordon
@ 2004-09-10 20:05 ` Jan-Benedict Glaw
2004-09-13 19:32 ` Charlie Gordon
0 siblings, 1 reply; 14+ messages in thread
From: Jan-Benedict Glaw @ 2004-09-10 20:05 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1443 bytes --]
On Wed, 2004-09-08 23:54:45 +0200, Charlie Gordon <gmane@chqrlie.org>
wrote in message <chnv2v$j6n$1@sea.gmane.org>:
> "Jan-Benedict Glaw" <jbglaw@lug-owl.de> wrote in message
> news:20040908120658.GZ6985@lug-owl.de...
>
> > > > for(i=0; i< N; i++)
> > > > printf("%lf\t", in[i]);
> it's ugly because the whole complex number in whatever storage format it's
> in gets passed to printf.
Right.
> In my understanding, this may fail because there is no guaranty about which
> part of the structure will be printed and the format specifier may be
> incorrect for the actual type thus passed.
Right, too. Here's how it ought to work:
-----------------------------------------------------------
#include <complex.h>
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char *argv[])
{
complex co;
co = 1.3 + I * 2.9;
printf ("%lf %lf\n", creal (co), cimag (co));
return EXIT_SUCCESS;
}
-----------------------------------------------------------
So you basically use creal() and cimag() to access the two numbers.
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-08 18:40 Huber, George K RDECOM CERDEC STCD SRI
@ 2004-09-10 20:09 ` Jan-Benedict Glaw
0 siblings, 0 replies; 14+ messages in thread
From: Jan-Benedict Glaw @ 2004-09-10 20:09 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 747 bytes --]
On Wed, 2004-09-08 14:40:35 -0400, Huber, George K RDECOM CERDEC STCD SRI <George.K.Huber@us.army.mil>
wrote in message <E3E30069B061524E90BCEE4417E30661138526@monm207.monmouth.army.mil>:
> #define MakeIndex(r,c) (r)*NUM_COLS + (y)
Don't forgetan additional pair of braces around the statement. Consider
MakeIndex(a,b) * 4
In this case, only "y" would be 4'ed, but not the whole expression.
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-10 20:05 ` Jan-Benedict Glaw
@ 2004-09-13 19:32 ` Charlie Gordon
2004-09-13 21:57 ` Jan-Benedict Glaw
0 siblings, 1 reply; 14+ messages in thread
From: Charlie Gordon @ 2004-09-13 19:32 UTC (permalink / raw)
To: linux-c-programming
"Jan-Benedict Glaw" <jbglaw@lug-owl.de> wrote in message
news:20040910200519.GJ19967@lug-owl.de...
> Right, too. Here's how it ought to work:
-----------------------------------------------------------
#include <complex.h>
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char *argv[])
{
complex co;
co = 1.3 + I * 2.9;
printf ("%lf %lf\n", creal (co), cimag (co));
return EXIT_SUCCESS;
}
-----------------------------------------------------------
> So you basically use creal() and cimag() to access the two numbers.
Well it is still too vague :
- what is the floating type of complex variable co ?
- You did not specify float, double or long double, so what is the default ?
- Reading tens of pages from C99 leaves that question open (!)
- The creal and cimag macros apply to all 3 complex types.
- if it is float, then real and imaginary parts will be converted to double
when passed to printf.
- If it is long double, they will be passed as such, but the format
specifier will be incorrect.
- Why do you use %lf ? %lf means double in fscanf, it is meaningless in
printf.
- If you meant long double, you should specify %Lf.
Consequently, I think it should read:
double complex co; ...
printf ("%f %f\n", creal(co), cimag(co));
or
complex co;
printf ("%f %f\n", (double)creal(co), (double)cimag(co));
just to be safe.
C is not very user friendly for floating point stuff. A lot of confusion
arises from historical lack of consistency in the libraries. A lot of
newbie C programmers will naturally use the float type instead of double.
Yet it is both less precise than even 32 bit ints, less efficient because of
the extra conversions, and slower on today's processors... Aside from hard
core SIMD and 3D afficionados, that will hand code their stuff in assembly,
'float' is pretty much obsolete.
Chqrlie.
PS: I have always hated floating point stuff ;-)
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-13 19:32 ` Charlie Gordon
@ 2004-09-13 21:57 ` Jan-Benedict Glaw
2004-09-14 8:16 ` Charlie Gordon
0 siblings, 1 reply; 14+ messages in thread
From: Jan-Benedict Glaw @ 2004-09-13 21:57 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1669 bytes --]
On Mon, 2004-09-13 21:32:06 +0200, Charlie Gordon <gmane@chqrlie.org>
wrote in message <ci4sid$kgu$1@sea.gmane.org>:
> "Jan-Benedict Glaw" <jbglaw@lug-owl.de> wrote in message
> news:20040910200519.GJ19967@lug-owl.de...
[...]
> > So you basically use creal() and cimag() to access the two numbers.
>
> Well it is still too vague :
> - what is the floating type of complex variable co ?
Not specified, I guess GCC "assumes" something there. But that doesn't
seem (from my reading) to match with C99 rules.
> - You did not specify float, double or long double, so what is the default ?
Hmmm. You're probably right. I've digget into C99, but to be honest, my
reading is that a simple "complex" (without a real floating type) is
just illegal...
> - Reading tens of pages from C99 leaves that question open (!)
Right. At least from my point of view :-)
> - The creal and cimag macros apply to all 3 complex types.
Well, there has been some type-conversion in between :-)
-----------
7.3.9.2 The cimag functions
Sysnopsis
#include <complex.h>
double cimag(double complex z);
float cimagf(float complex z);
long double cimagl(long double complex z);
-----------
> PS: I have always hated floating point stuff ;-)
/me too, but sometimes, it helps to achieve more correct numbers :-)
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: complex variable
2004-09-13 21:57 ` Jan-Benedict Glaw
@ 2004-09-14 8:16 ` Charlie Gordon
0 siblings, 0 replies; 14+ messages in thread
From: Charlie Gordon @ 2004-09-14 8:16 UTC (permalink / raw)
To: linux-c-programming
"Jan-Benedict Glaw" <jbglaw@lug-owl.de> wrote in message
news:20040913215742.GZ19967@lug-owl.de...
>> - The creal and cimag macros apply to all 3 complex types.
> Well, there has been some type-conversion in between :-)
-----------
7.3.9.2 The cimag functions
Sysnopsis
#include <complex.h>
double cimag(double complex z);
float cimagf(float complex z);
long double cimagl(long double complex z);
-----------
Well the truth is elsewhere ;-)
I have seen this paragraph, but it is a smoke screen, as there are also
creal and cimag *macros* mentioned in C99 that are described as applicable
to all three complex types and extract the corresponding part without
intermediary or final conversion (!)
C99 has created such a mine field around complex numbers and floating point
in general...
Chqrlie.
^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2004-09-14 8:16 UTC | newest]
Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20040906134525.33692.qmail@web52908.mail.yahoo.com>
2004-09-06 16:25 ` complex variable Ankit Jain
2004-09-06 16:37 ` Jan-Benedict Glaw
2004-09-07 15:37 Huber, George K RDECOM CERDEC STCD SRI
2004-09-07 16:05 ` Jan-Benedict Glaw
2004-09-08 9:59 ` Charlie Gordon
2004-09-08 12:06 ` Jan-Benedict Glaw
2004-09-08 16:25 ` Ankit Jain
2004-09-08 21:54 ` Charlie Gordon
2004-09-10 20:05 ` Jan-Benedict Glaw
2004-09-13 19:32 ` Charlie Gordon
2004-09-13 21:57 ` Jan-Benedict Glaw
2004-09-14 8:16 ` Charlie Gordon
-- strict thread matches above, loose matches on Subject: below --
2004-09-08 18:40 Huber, George K RDECOM CERDEC STCD SRI
2004-09-10 20:09 ` Jan-Benedict Glaw
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).