Browse Source

* Implemented a few math functions in our kernel: cos(), sin(), sqrt().

tags/v0.99.beta14
Sam Hocevar sam 19 years ago
parent
commit
d73e4337bf
2 changed files with 64 additions and 0 deletions
  1. +58
    -0
      kernel/kernel.c
  2. +6
    -0
      kernel/kernel.h

+ 58
- 0
kernel/kernel.c View File

@@ -211,4 +211,62 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
return 0;
}

/* math.h functions */
double cos(double x)
{
double ret = 0.0;
double x2;
double num = 1.0;
double fact = 1.0;
int i;

x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
x2 = x * x;

/* cos(x) = 1/0! - x^2/2! + x^4/4! - x^6/6! ... */
for(i = 0; i < 10; i++)
{
ret += num / fact;
num *= - x2;
fact *= (2 * i + 1) * (2 * i + 2);
}

return ret;
}

double sin(double x)
{
double ret = 0.0;
double x2;
double num;
double fact = 1.0;
int i;

x = x - ((double)(int)(x / (2 * M_PI))) * (2 * M_PI);
x2 = x * x;
num = x;

/* sin(x) = x/1! - x^3/3! + x^5/5! - x^7/7! ... */
for(i = 0; i < 10; i++)
{
ret += num / fact;
num *= - x2;
fact *= (2 * i + 2) * (2 * i + 3);
}

return ret;
}

double sqrt(double x)
{
double ret = x;
int i;

/* This is Newton's method */
for(i = 0; i < 10; i++)
ret = (ret * ret + x) / (ret * 2.0);

return ret;
}

#endif /* __KERNEL__ */

+ 6
- 0
kernel/kernel.h View File

@@ -24,6 +24,7 @@
#define BUFSIZ 4096
#define RAND_MAX ((unsigned int)0x8000000)
#define INT_MAX ((int)0x7fffffff)
#define M_PI 3.14159265358979323846
#define __BYTE_ORDER 1
#define __BIG_ENDIAN 2

@@ -110,3 +111,8 @@ void usleep(unsigned long usec);
/* time.h functions */
int gettimeofday(struct timeval *tv, struct timezone *tz);

/* math.h functions */
double cos(double x);
double sin(double x);
double sqrt(double x);


Loading…
Cancel
Save