From thomasmoore at speakeasy.net Tue Nov 9 23:38:43 2004 From: thomasmoore at speakeasy.net (Thomas Moore) Date: Tue, 09 Nov 2004 23:38:43 -0800 Subject: [OCLUG-devel] math functions in C Message-ID: <4191C583.20401@speakeasy.net> Hi all, I have only been on this list for a short time but have never seen a posting. But I will anyway. I am not a programmer but do write small programs for my amusement. Here is a short C program to illustrate my problem: #include #include int main(void) { double x = .7854; double y; y = sin(x); printf("sin(%f) = %f\n\n", x, y); } Then doing gcc -o prog prog.c I get the following message: /tmp/ccZW5o7k.o(.text+0x1a): In function `main': : undefined reference to `sin' collect2: ld returned 1 exit status Now, math.h refers to mathcall.h which contains all the function prototypes. Why will this not compile? Or more to the point, what am I doing wrong? Thanks. Tom From bao at hacom.net Tue Nov 9 23:12:53 2004 From: bao at hacom.net (Bao C. Ha) Date: Tue, 9 Nov 2004 23:12:53 -0800 (PST) Subject: [OCLUG-devel] math functions in C In-Reply-To: <4191C583.20401@speakeasy.net> References: <4191C583.20401@speakeasy.net> Message-ID: <38013.192.168.0.19.1100070773.squirrel@shopping.hacom.net> gcc -o prog prog.c -lm You need "-lm" to link to the math library. Bao On Tue, November 9, 2004 11:38 pm, Thomas Moore said: > Hi all, > > I have only been on this list for a short time but > have never seen a posting. But I will anyway. I am > not a programmer but do write small programs for > my amusement. > > Here is a short C program to illustrate my > problem: > > > > #include > #include > > int main(void) > { > double x = .7854; > double y; > > y = sin(x); > printf("sin(%f) = %f\n\n", x, y); > } > > Then doing > > gcc -o prog prog.c > > I get the following message: > > > /tmp/ccZW5o7k.o(.text+0x1a): In function `main': > : undefined reference to `sin' > collect2: ld returned 1 exit status > > > Now, math.h refers to mathcall.h which contains > all the function prototypes. Why will this not > compile? Or more to the point, what am I doing > wrong? > > Thanks. > > Tom > > > _______________________________________________ > OCLUG-devel mailing list -- OCLUG-devel at oclug.org > http://mailman.oclug.org/mailman/listinfo/oclug-devel > -- Best Regards. Bao C. Ha Hacom OpenBrick Distributor USA http://www.hacom.net voice: (714) 530-8817 fax: (714) 530-8818 8D66 6672 7A9B 6879 85CD 42E0 9F6C 7908 ED95 6B38 From thomasmoore at speakeasy.net Wed Nov 10 19:12:02 2004 From: thomasmoore at speakeasy.net (Thomas Moore) Date: Wed, 10 Nov 2004 19:12:02 -0800 Subject: [OCLUG-devel] math functions in C In-Reply-To: <38013.192.168.0.19.1100070773.squirrel@shopping.hacom.net> References: <4191C583.20401@speakeasy.net> <38013.192.168.0.19.1100070773.squirrel@shopping.hacom.net> Message-ID: <4192D882.7040500@speakeasy.net> Thanks, that worked great. But, I was unable dig that fact up from the docs. Where is it hidden? Tom Bao C. Ha wrote: >gcc -o prog prog.c -lm > >You need "-lm" to link to the math library. > >Bao > > From cbsmith at gmail.com Wed Nov 10 18:19:50 2004 From: cbsmith at gmail.com (Chris Smith) Date: Wed, 10 Nov 2004 18:19:50 -0800 Subject: [OCLUG-devel] math functions in C In-Reply-To: <4192D882.7040500@speakeasy.net> References: <4191C583.20401@speakeasy.net> <38013.192.168.0.19.1100070773.squirrel@shopping.hacom.net> <4192D882.7040500@speakeasy.net> Message-ID: <9ae343f5041110181948af432a@mail.gmail.com> On Wed, 10 Nov 2004 19:12:02 -0800, Thomas Moore wrote: > Thanks, that worked great. But, I was unable > dig that fact up from the docs. Where is it > hidden? In man pages from other systems. ;-) Deep withing the glibc package. When in doubt use "strings" and "grep" to figure out which library has what you need. ;-) -- Chris From james at colannino.org Fri Nov 19 17:51:48 2004 From: james at colannino.org (James Colannino) Date: Fri, 19 Nov 2004 17:51:48 -0800 Subject: [OCLUG-devel] Limiting range of random numbers Message-ID: <419EA334.6050906@colannino.org> Hey everyone, I'm using the following code snippet to generate suedo-random numbers: #include #include int random() { char seed; /* random number seed */ /* open /dev/urandom for binary read-only */ FILE *filename; filename = fopen ("/dev/urandom", "rb"); fread (&seed, 1, 1, filename); /* read 1 byte from /dev/urandom into seed */ fclose (filename); srand (seed); /* seeds rand() */ int random_number = rand(); return number; } It works great for me. The only problem is, all the numbers I get are quite large, something like 19348382 (as an example.) I'd like to restrict the possible numbers to a given range, perhaps from 0 to 10 or some much smaller number like that. How would I go about this? Thanks in advance. James From james at colannino.org Fri Nov 19 18:09:55 2004 From: james at colannino.org (James Colannino) Date: Fri, 19 Nov 2004 18:09:55 -0800 Subject: [OCLUG-devel] Limiting range of random numbers In-Reply-To: <419EA334.6050906@colannino.org> References: <419EA334.6050906@colannino.org> Message-ID: <419EA773.1060305@colannino.org> James Colannino wrote: > Hey everyone, > > I'm using the following code snippet to generate suedo-random numbers[...] Ok, well, after playing around a bit and trying a few random things, I think I managed to stumble across a possible solution. If I return (number % 20) instead of just number, it seems to always give me a value between 0 and 19. I'm not quite sure why this works (if someone could tell me how that would be great) but hey, at least it's working :) James From tthelin at sbcglobal.net Fri Nov 19 18:17:45 2004 From: tthelin at sbcglobal.net (Tim Thelin) Date: Fri, 19 Nov 2004 18:17:45 -0800 Subject: [OCLUG-devel] Limiting range of random numbers In-Reply-To: <419EA773.1060305@colannino.org> References: <419EA334.6050906@colannino.org> <419EA773.1060305@colannino.org> Message-ID: <419EA949.4050309@sbcglobal.net> James Colannino wrote: > James Colannino wrote: > >> Hey everyone, >> >> I'm using the following code snippet to generate suedo-random >> numbers[...] > > > > Ok, well, after playing around a bit and trying a few random things, I > think I managed to stumble across a possible solution. If I return > (number % 20) instead of just number, it seems to always give me a > value between 0 and 19. I'm not quite sure why this works (if someone > could tell me how that would be great) but hey, at least it's working :) > > James That's because x % y is modulo math. The expression returns the remainder of x / y. So 4 % 2 == 0 5 % 2 == 1 6 % 2 == 0 9 % 3 == 0 11 % 3 == 2 And any number X in the expression X % 20 will yield a number between 0 and 19. Tim