This Gitlab instance will require Two-Factor-Authentication from 2021-10-01. Read how to enable 2FA.

basicmath_small.c 2.41 KB
Newer Older
kuhnm's avatar
kuhnm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "snipmath.h"
#include <math.h>

/* The printf's may be removed to isolate just the math calculations */

int main(void)
{
  double  a1 = 1.0, b1 = -10.5, c1 = 32.0, d1 = -30.0;
  double  a2 = 1.0, b2 = -4.5, c2 = 17.0, d2 = -30.0;
  double  a3 = 1.0, b3 = -3.5, c3 = 22.0, d3 = -31.0;
  double  a4 = 1.0, b4 = -13.7, c4 = 1.0, d4 = -35.0;
  double  x[3];
  double X;
  int     solutions;
  int i;
  unsigned long l = 0x3fed0169L;
  struct int_sqrt q;
  long n = 0;

  /* solve soem cubic functions */
kuhnm's avatar
kuhnm committed
21
  /* printf("********* CUBIC FUNCTIONS ***********\n"); */
kuhnm's avatar
kuhnm committed
22 23
  /* should get 3 solutions: 2, 6 & 2.5   */
  SolveCubic(a1, b1, c1, d1, &solutions, x);  
kuhnm's avatar
kuhnm committed
24 25 26 27
  /* printf("Solutions:"); */
  /* for(i=0;i<solutions;i++) */
  /*   printf(" %f",x[i]); */
  /* printf("\n"); */
kuhnm's avatar
kuhnm committed
28 29
  /* should get 1 solution: 2.5           */
  SolveCubic(a2, b2, c2, d2, &solutions, x);  
kuhnm's avatar
kuhnm committed
30 31 32 33
  /* printf("Solutions:"); */
  /* for(i=0;i<solutions;i++) */
  /*   printf(" %f",x[i]); */
  /* printf("\n"); */
kuhnm's avatar
kuhnm committed
34
  SolveCubic(a3, b3, c3, d3, &solutions, x);
kuhnm's avatar
kuhnm committed
35 36 37 38
  /* printf("Solutions:"); */
  /* for(i=0;i<solutions;i++) */
  /*   printf(" %f",x[i]); */
  /* printf("\n"); */
kuhnm's avatar
kuhnm committed
39
  SolveCubic(a4, b4, c4, d4, &solutions, x);
kuhnm's avatar
kuhnm committed
40 41 42 43
  /* printf("Solutions:"); */
  /* for(i=0;i<solutions;i++) */
  /*   printf(" %f",x[i]); */
  /* printf("\n"); */
kuhnm's avatar
kuhnm committed
44 45 46 47 48 49
  /* Now solve some random equations */
  for(a1=1;a1<10;a1++) {
    for(b1=10;b1>0;b1--) {
      for(c1=5;c1<15;c1+=0.5) {
	for(d1=-1;d1>-11;d1--) {
	  SolveCubic(a1, b1, c1, d1, &solutions, x);  
kuhnm's avatar
kuhnm committed
50 51 52 53
	  /* printf("Solutions:"); */
	  /* for(i=0;i<solutions;i++) */
	  /*   printf(" %f",x[i]); */
	  /* printf("\n"); */
kuhnm's avatar
kuhnm committed
54 55 56 57 58
	}
      }
    }
  }
  
kuhnm's avatar
kuhnm committed
59
  /* printf("********* INTEGER SQR ROOTS ***********\n"); */
kuhnm's avatar
kuhnm committed
60 61 62 63 64 65
  /* perform some integer square roots */
  for (i = 0; i < 1001; ++i)
    {
      usqrt(i, &q);
			// remainder differs on some machines
     // printf("sqrt(%3d) = %2d, remainder = %2d\n",
kuhnm's avatar
kuhnm committed
66 67
     /* printf("sqrt(%3d) = %2d\n", */
     /* 	     i, q.sqrt); */
kuhnm's avatar
kuhnm committed
68 69 70
    }
  usqrt(l, &q);
  //printf("\nsqrt(%lX) = %X, remainder = %X\n", l, q.sqrt, q.frac);
kuhnm's avatar
kuhnm committed
71
  /* printf("\nsqrt(%lX) = %X\n", l, q.sqrt); */
kuhnm's avatar
kuhnm committed
72 73


kuhnm's avatar
kuhnm committed
74
  /* printf("********* ANGLE CONVERSION ***********\n"); */
kuhnm's avatar
kuhnm committed
75 76
  /* convert some rads to degrees */
  for (X = 0.0; X <= 360.0; X += 1.0)
kuhnm's avatar
kuhnm committed
77 78
    /* printf("%3.0f degrees = %.12f radians\n", X, deg2rad(X)); */
    /* puts(""); */
kuhnm's avatar
kuhnm committed
79
  for (X = 0.0; X <= (2 * PI + 1e-6); X += (PI / 180))
kuhnm's avatar
kuhnm committed
80
    /* printf("%.12f radians = %3.0f degrees\n", X, rad2deg(X)); */
kuhnm's avatar
kuhnm committed
81 82 83 84
  
  
  return 0;
}