aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/nums.h
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-10-20 00:36:29 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-10-20 00:36:29 -0400
commitc4585d7cfd92a295e17500a0d3ddfedf95d92cdd (patch)
tree1241d96ac7c10cbf5bfe47d700bb3479088e2ac7 /src/stdlib/nums.h
parent43a28773cf1ddc52d5ab605d056092eb47ee3b0c (diff)
Deduplicate Num code using the same templating technique as integers
Diffstat (limited to 'src/stdlib/nums.h')
-rw-r--r--src/stdlib/nums.h134
1 files changed, 7 insertions, 127 deletions
diff --git a/src/stdlib/nums.h b/src/stdlib/nums.h
index 303aa362..8cea9e84 100644
--- a/src/stdlib/nums.h
+++ b/src/stdlib/nums.h
@@ -2,132 +2,12 @@
#pragma once
-#include <stdbool.h>
-#include <stdint.h>
-
-#include "datatypes.h"
-#include "stdlib.h"
-#include "types.h"
-#include "util.h"
-
-#define OptionalNum_t double
-#define OptionalNum32_t float
-#define N32(n) ((float)(n))
#define N64(n) ((double)(n))
+#define OptionalNum_t double
+#define NUMX_H__BITS 64
+#include "numX.h"
-Text_t Num$as_text(const void *x, bool colorize, const TypeInfo_t *type);
-Text_t Num$value_as_text(double x);
-PUREFUNC int32_t Num$compare(const void *x, const void *y, const TypeInfo_t *type);
-PUREFUNC bool Num$equal(const void *x, const void *y, const TypeInfo_t *type);
-CONSTFUNC bool Num$near(double a, double b, double ratio, double absolute);
-Text_t Num$percent(double x, double precision);
-double CONSTFUNC Num$with_precision(double num, double precision);
-double Num$mod(double num, double modulus);
-double Num$mod1(double num, double modulus);
-CONSTFUNC bool Num$isinf(double n);
-CONSTFUNC bool Num$finite(double n);
-CONSTFUNC bool Num$isnan(double n);
-bool Num$is_none(const void *n, const TypeInfo_t *info);
-double Num$nan(Text_t tag);
-CONSTFUNC double Num$mix(double amount, double x, double y);
-OptionalNum_t Num$parse(Text_t text, Text_t *remainder);
-CONSTFUNC bool Num$is_between(const double x, const double low, const double high);
-CONSTFUNC double Num$clamped(double x, double low, double high);
-MACROLIKE CONSTFUNC double Num$from_num32(Num32_t n) { return (double)n; }
-#ifdef __GNUC__
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wfloat-equal"
-#endif
-MACROLIKE CONSTFUNC double Num$from_int(Int_t i, bool truncate) {
- if likely (i.small & 0x1) {
- double ret = (double)(i.small >> 2);
- if unlikely (!truncate && (int64_t)ret != (i.small >> 2))
- fail("Could not convert integer to 64-bit floating point without losing precision: ", i.small >> 2);
- return ret;
- } else {
- double ret = mpz_get_d(*i.big);
- if (!truncate) {
- mpz_t roundtrip;
- mpz_init_set_d(roundtrip, ret);
- if unlikely (mpz_cmp(*i.big, roundtrip) != 0)
- fail("Could not convert integer to 64-bit floating point without losing precision: ", i);
- }
- return ret;
- }
-}
-#ifdef __GNUC__
-#pragma GCC diagnostic pop
-#endif
-MACROLIKE CONSTFUNC double Num$from_int64(Int64_t i, bool truncate) {
- double n = (double)i;
- if unlikely (!truncate && (Int64_t)n != i)
- fail("Could not convert integer to 64-bit floating point without losing precision: ", i);
- return n;
-}
-MACROLIKE CONSTFUNC double Num$from_int32(Int32_t i) { return (double)i; }
-MACROLIKE CONSTFUNC double Num$from_int16(Int16_t i) { return (double)i; }
-MACROLIKE CONSTFUNC double Num$from_int8(Int8_t i) { return (double)i; }
-MACROLIKE CONSTFUNC double Num$from_byte(Byte_t i) { return (double)i; }
-
-extern const TypeInfo_t Num$info;
-
-Text_t Num32$as_text(const void *x, bool colorize, const TypeInfo_t *type);
-Text_t Num32$value_as_text(float x);
-PUREFUNC int32_t Num32$compare(const void *x, const void *y, const TypeInfo_t *type);
-PUREFUNC bool Num32$equal(const void *x, const void *y, const TypeInfo_t *type);
-CONSTFUNC bool Num32$near(float a, float b, float ratio, float absolute);
-Text_t Num32$percent(float x, float precision);
-float CONSTFUNC Num32$with_precision(float num, float precision);
-float Num32$mod(float num, float modulus);
-float Num32$mod1(float num, float modulus);
-CONSTFUNC bool Num32$isinf(float n);
-CONSTFUNC bool Num32$finite(float n);
-CONSTFUNC bool Num32$isnan(float n);
-CONSTFUNC bool Num32$is_none(const void *n, const TypeInfo_t *info);
-CONSTFUNC float Num32$mix(float amount, float x, float y);
-OptionalNum32_t Num32$parse(Text_t text, Text_t *remainder);
-float Num32$nan(Text_t tag);
-CONSTFUNC bool Num32$is_between(const float x, const float low, const float high);
-CONSTFUNC float Num32$clamped(float x, float low, float high);
-MACROLIKE CONSTFUNC float Num32$from_num(Num_t n) { return (float)n; }
-#ifdef __GNUC__
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wfloat-equal"
-#endif
-MACROLIKE CONSTFUNC float Num32$from_int(Int_t i, bool truncate) {
- if likely (i.small & 0x1) {
- float ret = (float)(i.small >> 2);
- if unlikely (!truncate && (int64_t)ret != (i.small >> 2))
- fail("Could not convert integer to 32-bit floating point without losing precision: ", i.small >> 2);
- return ret;
- } else {
- float ret = (float)mpz_get_d(*i.big);
- if (!truncate) {
- mpz_t roundtrip;
- mpz_init_set_d(roundtrip, (double)ret);
- if unlikely (mpz_cmp(*i.big, roundtrip) != 0)
- fail("Could not convert integer to 32-bit floating point without losing precision: ", i);
- }
- return ret;
- }
-}
-#ifdef __GNUC__
-#pragma GCC diagnostic pop
-#endif
-MACROLIKE CONSTFUNC float Num32$from_int64(Int64_t i, bool truncate) {
- float n = (float)i;
- if unlikely (!truncate && (Int64_t)n != i)
- fail("Could not convert integer to 32-bit floating point without losing precision: ", i);
- return n;
-}
-MACROLIKE CONSTFUNC float Num32$from_int32(Int32_t i, bool truncate) {
- float n = (float)i;
- if unlikely (!truncate && (Int32_t)n != i)
- fail("Could not convert integer to 32-bit floating point without losing precision: ", i);
- return n;
-}
-MACROLIKE CONSTFUNC float Num32$from_int16(Int16_t i) { return (float)i; }
-MACROLIKE CONSTFUNC float Num32$from_int8(Int8_t i) { return (float)i; }
-MACROLIKE CONSTFUNC float Num32$from_byte(Byte_t i) { return (float)i; }
-
-extern const TypeInfo_t Num32$info;
+#define N32(n) ((float)(n))
+#define OptionalNum32_t float
+#define NUMX_H__BITS 32
+#include "numX.h"