/*
Jonathan Krafcik
EE220 Spring 2003 HW1
1/19/2003
*/

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

#define PI 3.14159265
#define LIMIT  1073741822 /* arbitrary limit: 2^30-2 */

/* Prints functions F1(x) through F5(x) given a
   common value of x */              
void PrintFunctions(double x); 

/* Prints F1(x)/F5(x) and F2(x)/F4(x) given a common value of x */
void PrintFunctionsDiv(double x);

/* F1 implementation */
double Func1(double x);

/* F2 implementation */
double Func2(double x);

/* F3 implementation */
double Func3(double x);

/* F4 implementation */
double Func4(double x);

/* F5 implementation */
double Func5(double x);

/* F6 implementation */
double Func6(double x);

void PrintFunctions(double x) {
  printf("x=%2d F1=%8.2e F2=%8.2e F3=%8.2e F4=%8.2e F5=%8.2e F6=%8.2e\n", 
         (int) x, Func1(x), Func2(x), Func3(x), Func4(x), Func5(x), Func6(x));
}

void PrintFunctionsDiv(double x) {
  printf("x=%2d F1/F5=%8.2e F2/F4=%8.2e\n", (int) x, Func1(x)/Func5(x), 
         Func2(x)/Func4(x));
}

double Func1(double x) {
  return (20.0 * pow(x, 5.0) + 400.0 * pow(x, 2.0));
}

double Func2(double x) {
  double maxValue;
  double logx = log(x);
  double log2 = log(2.0);

  if (1.0 >= logx) {
    maxValue = 1.0;
  } else {
    maxValue = logx;
  }
  return (logx / log2 + log(maxValue) / log2);
}

double Func3(double x) {
  return x;
}

double Func4(double x) {
  return pow(2.0, 0.5 * x - 5);
}

double Func5(double x) {
  return 1000.0 * pow(x, 3.0);
}

double Func6(double x) {
  return pow(2.0, cos(x * PI / 180.0));
}

int main(void) {
  int i;
  PrintFunctions(1.0);
  PrintFunctions(4.0);
  PrintFunctions(8.0);
  PrintFunctions(12.0);
  PrintFunctions(16.0);
  PrintFunctions(24.0);
  PrintFunctions(32.0);
  printf("Printing values of all F from 1 to %d\n", LIMIT);
  for (i = 1; i < LIMIT; i *= 2) { /* show values from 1 to 40 */
    PrintFunctions((float) i);
  }
  printf("\nPrinting values of F1/F5 and F2/F4 from 1 to 40\n");
  for (i = 1; i < LIMIT; i *= 2) { /* show values from 1 to 40 */
    PrintFunctionsDiv((float) i);
  }
}

