-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.c
More file actions
88 lines (60 loc) · 1.51 KB
/
Copy pathmath.c
File metadata and controls
88 lines (60 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "rpncalc.h"
#include <math.h>
#include <stdio.h>
double rpn_factorial(double number)
{
int num = (int)round(number);
if(num == 1)
return num;
else
return num * rpn_factorial(num - 1);
}
double rpn_choose(double n, double r)
{
int ni = (int)round(n);
int ri = (int)round(r);
if(ni - ri < ri)
return rpn_choose(ni, ni - ri);
int top = 1;
int i = 0;
int top_next_multiplier = ni;
for(; i < ri; i++)
{
top = top * top_next_multiplier;
top_next_multiplier--;
}
return top / rpn_factorial(ri);
}
double rpn_permute(double n, double r)
{
int start = 1;
int ri = (int)round(r);
int ni = (int)round(n);
int i = 0;
for(; i < ri; i++)
{
start = start * ni;
ni--;
}
return start;
}
double rpn_nderiv(struct rpn_stack *func, struct rpn_stack *stack, struct rpn_stack *stat_stack, struct rpn_mode *mode, double x, double dv)
{
if(func == NULL || stack == NULL || stat_stack == NULL || mode == NULL)
return 0;
double rise = 0;
mode->x = x + dv;
rise = rpn_eval_stack(func, stack, stat_stack, mode);
mode->x = x;
rise = rise - rpn_eval_stack(func, stack, stat_stack, mode);
return (rise/dv);
}
int rpn_dequals(double d1, double d2, double precision)
{
double eps1 = fabs(d1), eps2 = fabs(d2), eps;
eps = (eps1 > eps2) ? eps1 : eps2;
if (eps == 0.0)
return 1;
eps *= precision;
return (fabs(d1 - d2) < eps);
}