diff --git a/CHANGELOG.md b/CHANGELOG.md index d0ff15a6..16307c21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Add `math/int-min`, `math/int-max`, `math/int32-min`, and `math/int32-max` for getting integer limits. - The gc interval is now autotuned, to prevent very bad gc behavior. - Improvements to the bytecode compiler, Janet will now generate more efficient bytecode. - Add `peg/find`, `peg/find-all`, `peg/replace`, and `peg/replace-all` diff --git a/src/core/inttypes.c b/src/core/inttypes.c index e11bcdab..4d53ec70 100644 --- a/src/core/inttypes.c +++ b/src/core/inttypes.c @@ -20,18 +20,18 @@ * IN THE SOFTWARE. */ -#include -#include -#include -#include -#include - #ifndef JANET_AMALG #include "features.h" #include #include "util.h" #endif +#include +#include +#include +#include +#include + /* Conditional compilation */ #ifdef JANET_INT_TYPES diff --git a/src/core/math.c b/src/core/math.c index 5491c77d..808267cc 100644 --- a/src/core/math.c +++ b/src/core/math.c @@ -499,6 +499,14 @@ void janet_lib_math(JanetTable *env) { JDOC("The number representing positive infinity")); janet_def(env, "math/-inf", janet_wrap_number(-INFINITY), JDOC("The number representing negative infinity")); + janet_def(env, "math/int32-min", janet_wrap_number(INT32_MIN), + JDOC("The maximum contiguous integer representable by a 32 bit signed integer")); + janet_def(env, "math/int32-max", janet_wrap_number(INT32_MAX), + JDOC("The minimum contiguous integer represtenable by a 32 bit signed integer")); + janet_def(env, "math/int-min", janet_wrap_number(JANET_INTMIN_DOUBLE), + JDOC("The maximum contiguous integer representable by a double (2^53)")); + janet_def(env, "math/int-max", janet_wrap_number(JANET_INTMAX_DOUBLE), + JDOC("The minimum contiguous integer represtenable by a double (-(2^53))")); #ifdef NAN janet_def(env, "math/nan", janet_wrap_number(NAN), #else diff --git a/src/core/pp.c b/src/core/pp.c index 3a855536..13550ca9 100644 --- a/src/core/pp.c +++ b/src/core/pp.c @@ -39,11 +39,9 @@ static void number_to_string_b(JanetBuffer *buffer, double x) { janet_buffer_ensure(buffer, buffer->count + BUFSIZE, 2); - /* Use int32_t range for valid integers because that is the - * range most integer-expecting functions in the C api use. */ const char *fmt = (x == floor(x) && - x <= ((double) INT32_MAX) && - x >= ((double) INT32_MIN)) ? "%.0f" : "%g"; + x <= JANET_INTMAX_DOUBLE && + x >= JANET_INTMIN_DOUBLE) ? "%.0f" : "%g"; int count = snprintf((char *) buffer->data + buffer->count, BUFSIZE, fmt, x); buffer->count += count; } diff --git a/src/core/util.h b/src/core/util.h index e8eb9b81..10c40a70 100644 --- a/src/core/util.h +++ b/src/core/util.h @@ -31,6 +31,11 @@ #include #include +#if !defined(JANET_REDUCED_OS) || !defined(JANET_SINGLE_THREADED) +#include +#define JANET_GETTIME +#endif + /* Handle runtime errors */ #ifndef JANET_EXIT #include @@ -98,12 +103,7 @@ void janet_core_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cf #endif /* Clock gettime */ -#if !defined(JANET_REDUCED_OS) || !defined(JANET_SINGLE_THREADED) -#include -#ifndef JANET_WINDOWS -#include -#endif -#define JANET_GETTIME +#ifdef JANET_GETTIME int janet_gettime(struct timespec *spec); #endif diff --git a/src/include/janet.h b/src/include/janet.h index 38446a3a..86410d71 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -128,10 +128,10 @@ extern "C" { #endif /* Limits for converting doubles to 64 bit integers */ -#define JANET_INTMAX_DOUBLE 9007199254740991.0 -#define JANET_INTMIN_DOUBLE (-9007199254740991.0) -#define JANET_INTMAX_INT64 9007199254740991 -#define JANET_INTMIN_INT64 (-9007199254740991) +#define JANET_INTMAX_DOUBLE 9007199254740992.0 +#define JANET_INTMIN_DOUBLE (-9007199254740992.0) +#define JANET_INTMAX_INT64 9007199254740992 +#define JANET_INTMIN_INT64 (-9007199254740992) /* Check emscripten */ #ifdef __EMSCRIPTEN__