APS105 L05 - Math and Random Numbers

#APS105 Slides Lecture Link to web notes
Function prototypes, or function declarations, dictate the inputs and outputs of a function.
Format: output_type function_name(input_name)

e.g.

double foo(double);
void foo(double);
double foo(double x, double y);

Math.h functional prototype

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main(void) {
	double y = 4.0;
	double y_sqrt = sqrt(y); // square root function
	printf("sqrt(%,1lf) = %.1lf\n", y, y_sqrt);
	return 0;
}

Creating your own header file:

// Create a file called sqrt.h with the following content:
double sqrt(double x);
#include "sqrt.h"
int main(void) {
	double y = 4.0;
	double y_sqrt = sqrt(y); // square root function
	printf("sqrt(%,1lf) = %.1lf\n", y, y_sqrt);
	return 0;
}

Other functions in math.h library:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
	double power = pow(double 2.0, double 3.0); // exponent function, returns 8.0
	double sin(double 2_pi); // sine function, returns 0
	double log(double x); // e logarithm function, returns value of log(e);
	double log10(double 0); // logarithm function with base 10, returns 1;
	double fmod(5.0, 2.3); // modulo but for doubles
	double fmin(5.0, 2.3); // returns smaller of two values
	double fmin(5.0, 2.3); //returns larger of two values
	double round(5.3); // rounds double input
	// in halfway cases, rounds up if positive and down if negative.
	double rint(2.5); // rounds double input
	// in halfway cases, rounds to even number

	double ceil(2.4); // always rounds up to next highest integer
	double floor (2.6); // always rounds down to next lowest integer
	double trunc(3.4); // chops off the decimal

	// note that these are <stdlib.h> functions, not <math.h> functions.
	int rand(void); // outputs random number between 0 and 2^31 - 1 inclusive
	// note that rand is deterministic, uses a seed.
	void srand(signed int seed); // changes the seed value that rand uses
	int rand() % 10; // returns a number between 1 and 9 inclusive.
	
}