Skip to content

Commit

Permalink
[Update] - Add math complex numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
vvaucoul committed Jul 28, 2024
1 parent 736e071 commit 103362e
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 29 deletions.
3 changes: 2 additions & 1 deletion Hephaistos.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"math.h": "c",
"array.h": "c",
"ctype.h": "c",
"limits.h": "c"
"limits.h": "c",
"stdint.h": "c"
}
}
}
45 changes: 33 additions & 12 deletions Hephaistos/include/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,72 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 23:21:41 by vvaucoul #+# #+# */
/* Updated: 2024/07/26 21:59:38 by vvaucoul ### ########.fr */
/* Updated: 2024/07/28 02:01:32 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef _MATH_H
#define _MATH_H

/**
* @file math.h
* @brief Header file for mathematical operations.
*/

#include <stdint.h>

/**
* @brief Return the absolute value of a number
*/
inline int abs(int nb) {
return (nb < 0 ? -nb : nb);
return (nb < 0 ? -nb : nb);
}

/**
* @brief Return the absolute value of a number
*/
inline float fabs(float nb) {
return (nb < 0 ? -nb : nb);
return (nb < 0 ? -nb : nb);
}

/**
* @brief Return the maximum of two numbers
*/
inline int max(int a, int b) {
return (a > b ? a : b);
return (a > b ? a : b);
}

/**
* @brief Return the minimum of two numbers
*/
inline int min(int a, int b) {
return (a < b ? a : b);
return (a < b ? a : b);
}

/**
* @brief Round a floating-point number to the nearest integer
*/
inline int round(float nb) {
return (nb >= 0) ? (int)(nb + 0.5) : -(int)(nb - 0.5);
return (nb >= 0) ? (int)(nb + 0.5) : -(int)(nb - 0.5);
}

/**
* @brief Calculate the power of a number
*/
inline float pow(float nb, float power) {
float res = 1;
float res = 1;

for (uint32_t i = 0; i < (uint32_t)power; i++) {
res *= nb;
}
return res;
for (uint32_t i = 0; i < (uint32_t)power; i++) {
res *= nb;
}
return res;
}

/**
* @brief Calculate the square root of a number
*/
inline float sqrt(float nb) {
return pow(nb, 0.5);
return pow(nb, 0.5);
}

extern float ln(float x);
Expand All @@ -78,4 +83,20 @@ extern int clamp(int value, int min, int max);
extern float hypot(float x, float y);
extern int signum(float x);

/**
* @struct Complex
* @brief Represents a complex number with real and imaginary parts.
*/
typedef struct {
float real;
float imag;
} Complex;

Complex complex_create(float real, float imag);
Complex complex_add(Complex a, Complex b);
Complex complex_sub(Complex a, Complex b);
Complex complex_mul(Complex a, Complex b);
Complex complex_div(Complex a, Complex b);
Complex complex_conj(Complex a);

#endif /* !_MATH_H */
2 changes: 1 addition & 1 deletion Hephaistos/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/20 11:31:06 by vvaucoul #+# #+# */
/* Updated: 2024/07/27 22:14:09 by vvaucoul ### ########.fr */
/* Updated: 2024/07/28 01:52:14 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
6 changes: 3 additions & 3 deletions Hephaistos/include/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 14:10:07 by vvaucoul #+# #+# */
/* Updated: 2024/07/27 16:12:48 by vvaucoul ### ########.fr */
/* Updated: 2024/07/28 01:55:15 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,8 +20,8 @@ typedef struct s_counter {
uint32_t end;
} counter_t;

extern void counter_start(counter_t *counter);
extern void counter_stop(counter_t *counter);
extern int counter_start(counter_t *counter);
extern int counter_stop(counter_t *counter);
extern uint32_t counter_get_start(counter_t *counter);
extern uint32_t counter_get_end(counter_t *counter);
extern uint32_t counter_get_time(counter_t *counter);
Expand Down
91 changes: 91 additions & 0 deletions Hephaistos/src/math/complex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* complex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/28 01:58:36 by vvaucoul #+# #+# */
/* Updated: 2024/07/28 02:01:33 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

#include <math.h>

/**
* @brief Create a complex number
* @param real The real part of the complex number
* @param imag The imaginary part of the complex number
* @return The created complex number
*/
Complex complex_create(float real, float imag) {
Complex c;
c.real = real;
c.imag = imag;
return c;
}

/**
* @brief Add two complex numbers
* @param a The first complex number
* @param b The second complex number
* @return The sum of the two complex numbers
*/
Complex complex_add(Complex a, Complex b) {
Complex result;
result.real = a.real + b.real;
result.imag = a.imag + b.imag;
return result;
}

/**
* @brief Subtract one complex number from another
* @param a The first complex number
* @param b The second complex number
* @return The difference of the two complex numbers
*/
Complex complex_sub(Complex a, Complex b) {
Complex result;
result.real = a.real - b.real;
result.imag = a.imag - b.imag;
return result;
}

/**
* @brief Multiply two complex numbers
* @param a The first complex number
* @param b The second complex number
* @return The product of the two complex numbers
*/
Complex complex_mul(Complex a, Complex b) {
Complex result;
result.real = a.real * b.real - a.imag * b.imag;
result.imag = a.real * b.imag + a.imag * b.real;
return result;
}

/**
* @brief Divide one complex number by another
* @param a The numerator complex number
* @param b The denominator complex number
* @return The quotient of the two complex numbers
*/
Complex complex_div(Complex a, Complex b) {
Complex result;
float denominator = b.real * b.real + b.imag * b.imag;
result.real = (a.real * b.real + a.imag * b.imag) / denominator;
result.imag = (a.imag * b.real - a.real * b.imag) / denominator;
return result;
}

/**
* @brief Calculate the conjugate of a complex number
* @param a The complex number
* @return The conjugate of the complex number
*/
Complex complex_conj(Complex a) {
Complex result;
result.real = a.real;
result.imag = -a.imag;
return result;
}
File renamed without changes.
2 changes: 1 addition & 1 deletion Hephaistos/src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/20 11:55:48 by vvaucoul #+# #+# */
/* Updated: 2024/07/27 17:40:13 by vvaucoul ### ########.fr */
/* Updated: 2024/07/28 01:52:10 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
33 changes: 25 additions & 8 deletions Hephaistos/src/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/21 14:11:09 by vvaucoul #+# #+# */
/* Updated: 2024/07/27 17:23:51 by vvaucoul ### ########.fr */
/* Updated: 2024/07/28 01:56:10 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,19 +19,27 @@
*
* @param counter Pointer to the counter structure to be started.
*/
void counter_start(counter_t *counter) {
assert(counter != NULL);
int counter_start(counter_t *counter) {
if (counter == NULL) {
return -EINVAL;
}

counter->start = pit_get_ticks();
return 0;
}

/**
* @brief Stops the time counter by recording the current timer ticks as the end time.
*
* @param counter Pointer to the counter structure to be stopped.
*/
void counter_stop(counter_t *counter) {
assert(counter != NULL);
int counter_stop(counter_t *counter) {
if (counter == NULL) {
return -EINVAL;
}

counter->end = pit_get_ticks();
return 0;
}

/**
Expand All @@ -41,7 +49,10 @@ void counter_stop(counter_t *counter) {
* @return The start time of the counter.
*/
uint32_t counter_get_start(counter_t *counter) {
assert(counter != NULL);
if (counter == NULL) {
return -EINVAL;
}

return counter->start;
}

Expand All @@ -52,7 +63,10 @@ uint32_t counter_get_start(counter_t *counter) {
* @return The end time of the counter.
*/
uint32_t counter_get_end(counter_t *counter) {
assert(counter != NULL);
if (counter == NULL) {
return -EINVAL;
}

return counter->end;
}

Expand All @@ -63,6 +77,9 @@ uint32_t counter_get_end(counter_t *counter) {
* @return The elapsed time between the start and end times of the counter.
*/
uint32_t counter_get_time(counter_t *counter) {
assert(counter != NULL);
if (counter == NULL) {
return -EINVAL;
}

return counter->end - counter->start;
}
4 changes: 1 addition & 3 deletions workflows/workflows.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vvaucoul <vvaucoul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/09 14:55:33 by vvaucoul #+# #+# */
/* Updated: 2024/07/27 19:07:58 by vvaucoul ### ########.fr */
/* Updated: 2024/07/28 01:49:47 by vvaucoul ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -38,6 +38,4 @@ extern int workflow_hephaistos_a_avl(void);

extern int hephaistos_workflow(void);

extern void hephaistos_off_tests(void);

#endif /* !__H_WORKFLOWS_H */

0 comments on commit 103362e

Please sign in to comment.