ft_math
ft_math copied to clipboard
The entire <math.h> library in scratch.
ft_math
ft_math.h is a remake library from original <math.h> library.
Includes C23, C11 and C99 functions + their float types, can compilable and work all versions + compilers.
Setup:
Windows:
You can double click
MAKE.batfile and directly install it.Or in console, write
maketo compile the stup.
Linux, macOS:
In console, write
maketo compile the setup.
PHONYS
Here's the arguments MAKE.bat and Makefile using:
fc,fclean,fclear-> Clear both object and created products.
c,clean,clear-> Clear only object files
re-> Remake everything
Then, in console: you need to include your binary compiler library (.a file) via parameter while compiling your file.
GCC main.c mathft.a
Trigonometric functions:
| Function Name | Function Parameter(s) | Description |
|---|---|---|
| ft_sin ft_sinf | double x |
Returns the sine of x |
| ft_cos ft_cosf | double x |
Returns the cosine of x |
| ft_tan ft_tanf | double x |
Returns the tangent of x. [sin(x) / cos(x)] |
| ft_asin ft_asinf | double x |
Returns the arc sine in radians. (1 ≥ x ≥ -1) [Aka: sin⁻¹(x)] |
| ft_acos ft_acosf | double x |
Returns the arc cosine in radians. (1 ≥ x ≥ -1) [Aka: cos⁻¹(x)] |
| ft_atan ft_atanf | double x |
Returns the arc tangent of x |
| ft_atan2 ft_atan2f | double x, double y |
Calculates the angle in radians for the quadrant |
| ft_csc ft_cscf | double x |
Returns the cosecant of x |
| ft_sec ft_secf | double x |
Returns the secant of x |
| ft_cot ft_cotf | double x |
Returns the cotangent of x |
| ft_sincos ft_sincosf | double x, double * sine, double * cosine |
Split x to given variables as sine and cosine |
| ft_sinpi ft_sinpif | double x |
sin(x * M_PI) |
| ft_cospi ft_cospif | double x |
cos(x * M_PI) |
| ft_tanpi ft_tanpif | double x |
tan(x * M_PI) |
Hyperbolic functions:
| Function Name | Function Parameter(s) | Description |
|---|---|---|
| ft_sinh ft_sinhf | double x |
Computes the hyperbolic sine of x |
| ft_cosh ft_coshf | double x |
Computes the hyperbolic cosine of x |
| ft_tanh ft_tanhf | double x |
Computes the hyperbolic tangent of x |
| ft_asinh ft_asinhf | double x |
Computes the arc hyperbolic sine of x |
| ft_acosh ft_acoshf | double x |
Computes the arc hyperbolic cosine of x |
| ft_atanh ft_atanhf | double x |
Computes the arc hyperbolic tangent of x |
Exponential and Logarithmic functions:
| Function Name | Function Parameter(s) | Description |
|---|---|---|
| ft_exp ft_expf | double x |
Computes e(2.71828) raised to the power of the given x |
| ft_exp2 ft_exp2f | double x |
Computes 2 raised to the given power x |
| ft_exp10 ft_exp10f | double x |
Calculate the exponent of 10 by x |
| ft_expm1 ft_expm1f | double x |
Computes the Euler's number raised to the given x, (-1.0 x) |
| ft_frexp ft_frexpf | double x, int * exponent |
Returns value of mantissa, and pointed to by exponent |
| ft_ilogb ft_ilogbf | double x |
Extracts the value of the unbiased exponent from the x as int |
| ft_ldexp ft_ldexpf | double x, int exponent |
Returns x multiplied by 2 raised to the power of exponent |
| ft_log ft_logf | double x |
Calculation of the logarithm of x |
| ft_log2 ft_log2f | double x |
Calculation of the base-2 logarithm of x |
| ft_log10 ft_log10f | double x |
Calculation of the logarithm of x to the base of 10 |
| ft_log1p ft_log1pf | double x |
Calculates the logarithm of one plus of x |
| ft_logb ft_logbf | double x |
Extracts the unbiased radix-independent exponent from the x |
| ft_scalbln ft_scalblnf | double x, long int exponent |
Returns x multiplied by 2 raised to the power of exponent |
| ft_scalbn ft_scalbnf | double x, int exponent |
Returns x multiplied by 2 raised to the power of exponent |
Power functions:
| Function Name | Function Parameter(s) | Description |
|---|---|---|
| ft_cbrt ft_cbrtf | double x |
Computes the cube root of x |
| ft_hypot ft_hypotf | double p, double b |
Find hypotenuse of two sides. h = √(p²+b²) |
| ft_invsqrt ft_invsqrtf | double x |
Fast inverse square root |
| ft_pow ft_powf | double base, double power |
Calculates the angle in radians for the quadrant |
| ft_pow10 ft_pow10f | double x |
Calculate the exponent of 10 by x |
| ft_sqrt ft_sqrtf | double x |
Square root calculation of x |
Rounding and Remainder functions:
| Function Name | Function Parameter(s) | Description |
|---|---|---|
| ft_ceil ft_ceilf | double x |
Computes the nearest integer greater than x |
| ft_copysign ft_copysignf | double x, double y |
Composes a result with magnitude of x and the sign of y |
| ft_fdim ft_fdimf | double x, double y |
Returns the positive difference between x and y |
| ft_floor ft_floorf | double x |
Returns the largest integer that is smaller than or equal to x |
| ft_fmod ft_fmodf | double x, double y |
Gets the modalities of 2 numbers in "float" |
| ft_lrint ft_lrintf | double x |
Roundoff the floating-point x to a long |
| ft_lround ft_lroundf | double x |
Computes the nearest integer value to x |
| ft_nan ft_nanf | const char * tag_pointer |
Just returns NAN (Not A Number) |
| ft_nearbyint ft_nearbyintf | double x |
Rounds the floating-point x to an integer value |
| ft_nextafter ft_nextafterf | double x, double y |
Returns the next representable value of from in the direction of to |
| ft_nexttoward ft_nexttowardf | double x, long double y |
(Same with ft_nextafter) |
| ft_remainder ft_remainderf | double x, double y |
Computes the IEEE remainder of floating point division operation |
| ft_remquo ft_remquof | double x, double y, int * quotient |
Computes the floating-point remainder of the division operation |
| ft_rint ft_rintf | double x |
Roundoff the floating-point x to an integer |
| ft_round ft_roundf | double x |
Returns the nearest integer value (rounded value) of the given x |
| ft_trunc ft_truncf | double x |
Truncates the x by removing the fraction |
Floating-point Classification functions:
| Function Name | Function Parameter(s) | Description |
|---|---|---|
| ft_fpclassify ft_fpclassifyf | double x |
Categorizes floating point value x into the following categories |
| ft_isfinite | double x |
Check if number is not infinity |
| ft_isinf | double x |
Check if float number is inf number (Will also return -1 if "-inf") |
| ft_isnan | double x |
Check if float number is non number |
| ft_isnormal | double x |
Check if number is normal to in use |
| ft_signbit | double x |
Checking if the float number is minus or not |
| ft_significand ft_significandf | double x |
Retrieves the fractional part of a double-precision floating-point number |
Bessel functions:
| Function Name | Function Parameter(s) | Description |
|---|---|---|
| ft_j0 ft_j0f | double x |
Compute the Bessel function of the first kind of order zero (j0) |
| ft_j1 ft_j1f | double x |
Compute the Bessel function of the first kind of order one (j1) |
| ft_jn ft_jnf | int n, double x |
Compute the Bessel function of the first kind of order n (jn) |
| ft_y0 ft_y0f | double x |
Compute the Bessel function of the second kind of order zero (y0) |
| ft_y1 ft_y1f | double x |
Compute the Bessel function of the second kind of order one (y1) |
| ft_yn ft_ynf | int n, double x |
Compute the Bessel function of the second kind of order n (yn) |
Other functions:
| Function Name | Function Parameter(s) | Description |
|---|---|---|
| ft_erf ft_erff | double x |
Computes the error function of the given x |
| ft_erfc ft_erfcf | double x |
Computes the complementary error of x |
| ft_fibonacci | unsigned long long number |
Every number in the sequence is the sum of two numbers preceding it in the sequence |
| ft_fma ft_fmaf | double x, double y, double z |
(x * y + z) |
| ft_fabs ft_fabsf | double x |
Get the absolute value of a x |
| ft_fmax ft_fmaxf | double x |
If (x > y) return x, else return y |
| ft_fmin ft_fminf | double x |
If (x < y) return x, else return y |
| ft_lerp ft_lerpf | double x, double y, double f |
Linear interpolation. |
| ft_lgamma ft_lgammaf | double x |
Computes the logarithm of the absolute x of the gamma function |
| ft_modf ft_modff | double x, double * integer |
Splits fraction an integer in float number (Returns fraction) |
| ft_tgamma ft_tgammaf | double x |
Compute the gamma function of x |
Neural Network functions:
| Function Name | Function Parameter(s) | Description |
|---|---|---|
| ft_elu ft_eluf | double x, double aplha |
"Exponential Linear Unit" Determines the slope for negative input |
| ft_leakyrelu ft_leakyreluf | double x, double aplha |
ReLU, But has a small slope for negative values instead of a flat slope |
| ft_mish ft_mishf | double x |
Hyperbolic tangent function, and softplus is the smooth approximation |
| ft_prelu ft_preluf | double x, double aplha |
ReLU, But has a small slope for negative values instead of a flat slope |
| ft_relu ft_reluf | double x |
"The Rectified Linear Unit" Returns 0 if x is negative |
| ft_sigmoid ft_sigmoidf | double x |
Performs the role of an activation function in machine learning |
| ft_softmax ft_softmaxf | double * x, int size |
Normalizes the input array x by exponentiating each element |
| ft_softmedian ft_softmedianf | double * x, int size, double alpha |
Defined as the weighted average of the elements in a vector x |
| ft_softmin ft_softminf | double * x, int size, double alpha |
Aka: softmax(-x) |
| ft_swish ft_swishf | double x |
Allows for a more flexible range of x |
Constants:
| Define Name | Value |
|---|---|
| M_E M_E_F | 2.71828182845904523536 (Aka: Euler's number) |
| M_LOG2E M_LOG2E_F | 1.44269504088896340736 (Aka: log2(e)) |
| M_LOG10E M_LOG10E_F | 0.43429448190325182765 (Aka: log10(e)) |
| M_LN2 M_LN2_F | 0.69314718055994528623 (Aka: ln(2)) |
| M_LN10 M_LN10_F | 2.30258509299404568402 (Aka: ln(10)) |
| M_PIX2 M_PIX2_F | 6.28318530717958647692 (Aka: M_PI * 2) |
| M_PIX2_3 M_PIX2_3_F | 4.71238898038400589261 (Aka: M_PI * 2 / 3) |
| M_PI M_PI_F | 3.14159265358979323846 (π) |
| M_PI_2 M_PI_2_F | 1.57079632679489661923 (Aka: M_PI / 2) |
| M_PI_4 M_PI_4_F | 0.78539816339744830961 (Aka: M_PI / 4) |
| M_1_PI M_1_PI_F | 0.31830988618379067153 (Aka: 1 / M_PI) |
| M_2_PI M_2_PI_F | 0.63661977236758134307 (Aka: 2 / M_PI) |
| M_SQRT2 M_SQRT2_F | 1.41421356237309504880 (Aka: sqrt(2)) |
| M_SQRT1_2 M_SQRT1_2_F | 0.70710678118654752440 (Aka: 1 / sqrt(2)) |
| M_SQRTPI M_SQRTPI_F | 1.77245385090551588191 (Aka: sqrt(PI)) |
| M_2_SQRTPI M_2_SQRTPI_F | 1.12837916709551257390 (Aka: 2 / sqrt(pi)) |
| M_INVSQRTPI M_INVSQRTPI_F | 0.56418958354775627928 (Aka: invsqrt(pi)) |
| M_TPI M_TPI_F | 0.63661977236758138243 (Aka: 2 / PI) |
Trigonometric Constants:
| Define Name | Value |
|---|---|
| M_0DEG M_0DEG_F | 0 |
| M_1DEG M_1DEG_F | 0.0174532925199 |
| M_45DEG M_45DEG_F | π / 4 |
| M_90DEG M_90DEG_F | π / 2 |
| M_180DEG M_180DEG_F | π |
| M_270DEG M_270DEG_F | π * 2 / 3 |
| M_360DEG M_360DEG_F | π * 2 |
<float.h> Constants:
| Define Name | Value |
|---|---|
| FLT_MAX | 3.40282347E+38F |
| FLT_MIN | 1.17549435E-38F |
| FLT_EPSILON | 1.19209290E-7F |
| DBL_MAX | 1.7976931348623158E+308 |
| DBL_MIN | 2.2250738585072014E-308 |
| DBL_EPSILON | 2.2204460492503131E-16 |
| LDBL_MAX | 1.18973149535723176502E+4932L |
| LDBL_MIN | 3.36210314311209350626E-4932L |
| LDBL_EPSILON | 1.08420217248550443401E-19L |
Environments for ft_fpclassify function:
| Define Name | Value |
|---|---|
| FP_NAN | 0 |
| FP_INFINITE | 1 |
| FP_ZERO | 2 |
| FP_SUBNORMAL | 3 |
| FP_NORMAL | 4 |
A Few Example(s):
#include <stdio.h> /*
* int printf(char *str, ...);
*/
#include <math.h>
#include "ft_math.h"
int
main(void)
{
double x = 42;
double y = 5;
printf("Trigonometric functions:\n");
printf("[ sin(%d) = %f]\n", x, sin(x));
printf("[ft_sin(%d) = %f]\n", x, ft_sin(x));
printf("\n");
printf("[ cos(%d) = %f]\n", x, cos(x));
printf("[ft_cos(%d) = %f]\n", x, ft_cos(x));
printf("\n");
printf("[ tan(%d) = %f]\n", x, tan(x));
printf("[ft_tan(%d) = %f]\n", x, ft_tan(x));
printf("\n");
printf("[ asin(%d) = %f]\n", x, asin(x));
printf("[ft_asin(%d) = %f]\n", x, ft_asin(x));
printf("\n");
printf("[ acos(%d) = %f]\n", x, acos(x));
printf("[ft_acos(%d) = %f]\n", x, ft_acos(x));
printf("\n");
printf("[ atan(%d) = %f]\n", x, atan(x));
printf("[ft_atan(%d) = %f]\n", x, ft_atan(x));
printf("\n");
printf("[ atan2(%d, %d) = %f]\n", x, y, atan2(x, y));
printf("[ft_atan2(%d, %d) = %f]\n", x, y, ft_atan2(x, y));
printf("\n\n");
printf("[ log(%d) = %f]\n", x, log(x));
printf("[ft_log(%d) = %f]\n", x, ft_log(x));
printf("\n");
printf("[ log1p(%d) = %f]\n", x, log1p(x));
printf("[ft_log1p(%d) = %f]\n", x, ft_log1p(x));
printf("\n");
printf("[ log10(%d) = %f]\n", x, log10(x));
printf("[ft_log10(%d) = %f]\n", x, ft_log10(x));
printf("\n\n");
printf("Power functions:\n");
printf("[ pow(%d, %d) = %f]\n", x, y, pow(x, y));
printf("[ft_pow(%d, %d) = %f]\n", x, y, ft_pow(x, y));
printf("\n");
printf("[ sqrt(%d) = %f]\n", x, sqrt(x));
printf("[ft_sqrt(%d) = %f]\n", x, ft_sqrt(x));
printf("\n\n");
double nan = (0.0 / 0.0);
double inf = (1.0 / 0.0);
printf("[ isnan = %f]\n", isnan(nan));
printf("[ft_isnan = %f]\n", ft_isnan(nan));
printf("\n");
printf("[ isnan = %f]\n", isnan(-nan));
printf("[ft_isnan = %f]\n", ft_isnan(-nan));
printf("\n");
printf("[ ininf = %f]\n", isinf(inf));
printf("[ft_isinf = %f]\n", ft_isinf(inf));
printf("\n");
printf("[ ininf = %f]\n", isinf(-inf));
printf("[ft_ininf = %f]\n", ft_isinf(-inf));
printf("\n\n");
printf("Other functions:\n");
double pi = 3.1415;
double temp = 0;
printf("[ fabs(%d) = %f]\n", -x, fabs(-x));
printf("[ft_fabs(%d) = %f]\n", -x, ft_fabs(-x));
printf("\n");
printf("[ fmod(%d, %d) = %f]\n", x, y, fmod(x, y));
printf("[ft_fmod(%d, %d) = %f]\n", x, y, ft_fmod(x, y));
printf("\n");
printf("[ modf = %f] {modf(pi, &temp)}\n", modf(pi, &temp));
printf("[ modf temp = %f] {temp}\n", temp);
printf("[ ft_modf = %f] {ft_modf(pi, &temp)}\n", ft_modf(pi, &temp));
printf("[ft_modf temp = %f] {temp}\n", temp);
return (0);
}