From fe2dac13616e491316b82fb114d39ab5bf91639d Mon Sep 17 00:00:00 2001 From: Tom-on64 Date: Fri, 25 Oct 2024 12:03:58 +0200 Subject: [PATCH] Implemented float.h and improved math.h --- libc/include/float.h | 33 +++++++++++++++++++++++++++++++++ libc/math/const.c | 1 + libc/math/htrig.c | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 libc/include/float.h diff --git a/libc/include/float.h b/libc/include/float.h new file mode 100644 index 0000000..8615733 --- /dev/null +++ b/libc/include/float.h @@ -0,0 +1,33 @@ +#ifndef _FLOAT_H +#define _FLOAT_H + +#define FLT_DIG 6 // Float precision: 6 decimal digits +#define DBL_DIG 15 // Double precision: 15 decimal digits +#define LDBL_DIG 18 // Long double precision: 18 decimal digits + +#define FLT_MAX 3.40282347e+38F // Maximum float value +#define DBL_MAX 1.7976931348623157e+308 // Maximum double value +#define LDBL_MAX 1.189731495357231765e+4932L // Max long double + +#define FLT_MIN 1.17549435e-38F // Minimum positive normalized float +#define DBL_MIN 2.2250738585072014e-308 // Min positive double +#define LDBL_MIN 3.36210314311209350626e-4932L // Min positive long double + +#define FLT_EPSILON 1.19209290e-7F // Smallest float increment +#define DBL_EPSILON 2.2204460492503131e-16 // Smallest double increment +#define LDBL_EPSILON 1.08420217248550443401e-19L // Smallest long double increment + +#define FLT_MAX_EXP 128 // Max exponent for float +#define DBL_MAX_EXP 1024 // Max exponent for double +#define LDBL_MAX_EXP 16384 // Max exponent for long double + +#define FLT_MIN_EXP -125 // Min exponent for float +#define DBL_MIN_EXP -1021 // Min exponent for double +#define LDBL_MIN_EXP -16381 // Min exponent for long double + +#define FLT_RADIX 2 // Floating-point base (usually 2) +#define FLT_MANT_DIG 24 // Number of digits in float mantissa +#define DBL_MANT_DIG 53 // Number of digits in double mantissa +#define LDBL_MANT_DIG 64 // Number of digits in long double mantissa + +#endif diff --git a/libc/math/const.c b/libc/math/const.c index 4d19e36..69a4c8d 100644 --- a/libc/math/const.c +++ b/libc/math/const.c @@ -1,4 +1,5 @@ #include +#include double nan(const char* tagp) { (void)tagp; diff --git a/libc/math/htrig.c b/libc/math/htrig.c index 91e8dd9..ef94891 100644 --- a/libc/math/htrig.c +++ b/libc/math/htrig.c @@ -11,6 +11,6 @@ double cosh(double x) { double tanh(double x) { double ex = exp(x); double emx = exp(-x); - return (ex - exp) / (ex + exp); + return (ex - emx) / (ex + emx); }