-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmath_lib.c
More file actions
106 lines (93 loc) · 2.01 KB
/
math_lib.c
File metadata and controls
106 lines (93 loc) · 2.01 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <math.h>
#include <stdio.h>
int
add_numbers(int a, int b)
{
printf("Adding numbers in shared library: %d + %d\n", a, b);
return a + b;
}
int
subtract_numbers(int a, int b)
{
printf("Subtracting numbers in shared library: %d - %d\n", a, b);
return a - b;
}
int
multiply_numbers(int a, int b)
{
printf("Multiplying numbers in shared library: %d * %d\n", a, b);
return a * b;
}
float
add_floats(float a, float b)
{
printf("Adding floats in shared library: %f + %f\n", a, b);
return a + b;
}
float
multiply_floats(float a, float b)
{
printf("Multiplying floats in shared library: %f * %f\n", a, b);
return a * b;
}
float
divide_floats(float a, float b)
{
if (b == 0) {
printf("Error: Division by zero!\n");
return 0;
}
printf("Dividing floats in shared library: %f / %f\n", a, b);
return a / b;
}
double
add_doubles(double a, double b)
{
printf("Adding doubles in shared library: %lf + %lf\n", a, b);
return a + b;
}
double
multiply_doubles(double a, double b)
{
printf("Multiplying doubles in shared library: %lf * %lf\n", a, b);
return a * b;
}
double
calculate_sqrt(double x)
{
if (x < 0) {
printf("Error: Cannot calculate square root of negative number!\n");
return 0;
}
printf("Calculating square root of %lf\n", x);
return sqrt(x);
}
double
calculate_power(double base, double exponent)
{
printf("Calculating power: %lf ^ %lf\n", base, exponent);
return pow(base, exponent);
}
float
calculate_hypotenuse(float a, float b)
{
printf("Calculating hypotenuse with a=%f, b=%f\n", a, b);
return sqrtf(a * a + b * b);
}
void
add_vectors(float* a, float* b, float* result, int size)
{
printf("Adding vectors of size %d\n", size);
for (int i = 0; i < size; i++) {
result[i] = a[i] + b[i];
}
}
void
multiply_matrices_2x2(float* a, float* b, float* result)
{
printf("Multiplying 2x2 matrices\n");
result[0] = a[0] * b[0] + a[1] * b[2];
result[1] = a[0] * b[1] + a[1] * b[3];
result[2] = a[2] * b[0] + a[3] * b[2];
result[3] = a[2] * b[1] + a[3] * b[3];
}