-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemath.c
140 lines (119 loc) · 2.88 KB
/
emath.c
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <stdio.h>
#include <math.h>
#include "emath.h"
/*
* This is a library of C functions to caluculate electonic formulas such as
* Ohm's Law and power.
*
* These functions return -1 in the case of an error or incorrect calculation. Any other value is considered
* correct.
*/
#if defined(__cplusplus)
extern "C" // Use C linkage in C++ programs
#endif
int main(void) {
// This function is an anti-standalone shield, comment out if needed.
printf("This is not a standalone program.\n");
printf("Include this library into your C program to use it.\n");
return(-1);
}
/*
* These typedefs are here for quick changing of variable types
*/
typedef float voltage;
typedef float current;
typedef float power;
typedef float resistance;
/*
* ohmslaw - solves ohm's law for a NULL value
*/
float ohmslaw(float i, float v, float r) {
float retval;
if(i == NULL && v == NULL || i == NULL && r == NULL || v == NULL && r == NULL) {
// Can't solve for multiple values
return(-1);
}
if(i == NULL) {
retval = v / r;
return(retval);
}
if(v == NULL) {
retval = i / r;
return(retval);
}
if(r == NULL) {
retval = i / v;
return(retval);
}
else {
// No value to solve for
return(-1);
}
}
/*
* confohmslaw - confirms to see if an Ohm's Law calculation was correct
*/
float confohmslaw(float i, float v, float r) {
float retval;
float temp;
temp = v / r;
if(i == temp) {
retval = 0;
}
else {
retval = -1;
}
return(retval);
}
/*
* getvoltage - calculates voltage based upon a given current and resistance
*/
voltage getvoltage(current i, resistance r) {
voltage retval;
retval=ohmslaw(i, NULL, r);
return(retval);
}
/*
* getcurrent - calculates current based upon voltage and resistance
*/
current getcurrent(voltage v, resistance r) {
current retval;
retval=ohmslaw(NULL, v, r);
return(retval);
}
/*
* power - calculates power in watts based on current and voltage
*/
power power(current i, voltage v) {
power retval;
retval=v * i; // I have heard this called apparent power, or VA (volt-amps). Is this an AC-only thing?
return(retval);
}
/*
* dissipated - calculates dissipated energy (watts) with a given input vlots, amps and output current/volts.
*/
power dissipated(voltage v1, current i1, voltage v2, current i2) {
power retval, inpower, outpower;
inpower = power(i1, v1);
outpower = power(i2, v2);
retval = inpower - outpower;
return(retval);
}
/*
* rms_voltage - calculates RMS voltages in AC circuits
*/
voltage rms_voltage(voltage peak_hi, voltage peak_lo) {
voltage retval;
retval = sqrt(peak_hi + peak_lo / 2);
retval = retval * retval;
return(retval);
}
/*
* rms_current - calculates RMS current values in AC circuits
*/
current rms_current(current peak_hi, current peak_lo) {
current retval;
retval = sqrt(peak_hi + peak_lo / 2);
retval = retval * retval;
return(retval);
}