Math functions like random and sin

Sometimes you need a random number.

Strange enough: this math function is  found in the stdlib.h: rand() with datatype int

http://www.cs.cf.ac.uk/Dave/C/node16.html#SECTION001600000000000000000

Other math functions are found in the math.h:

like abs, cos, sin, tan, atan

http://en.wikipedia.org/wiki/Math.h

you use the libs like this:

<stdio.h>

<stdlib.h>

<math.h>

int main(){

printf( “this is a random number between 0 and 10 %d \n”, rand() % 10 );

printf( “this is a sin %f \n”, sin(.157) );

while(1) {}

}

Also in the makefile -lm should be added, if it isn’t. You get funny bugs if it isn’t.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: