From 83661021edbeeeef56d5687b44619e557e09e568 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 29 Nov 2022 10:29:49 +0100 Subject: math: add isnan() function Add a new `isnan()` convenience function to the math library which can be used to test if a given value is a NaN double. The same test can be realized without the math library by using a function similar to the following one: function isNaN(x) { return x != x; } Signed-off-by: Jo-Philipp Wich --- lib/math.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/math.c') diff --git a/lib/math.c b/lib/math.c index f16d309..cbacb7e 100644 --- a/lib/math.c +++ b/lib/math.c @@ -169,6 +169,14 @@ uc_srand(uc_vm_t *vm, size_t nargs) return NULL; } +static uc_value_t * +uc_isnan(uc_vm_t *vm, size_t nargs) +{ + uc_value_t *v = uc_fn_arg(0); + + return ucv_boolean_new(ucv_type(v) == UC_DOUBLE && isnan(ucv_double_get(v))); +} + static const uc_function_list_t math_fns[] = { { "abs", uc_abs }, { "atan2", uc_atan2 }, @@ -180,6 +188,7 @@ static const uc_function_list_t math_fns[] = { { "pow", uc_pow }, { "rand", uc_rand }, { "srand", uc_srand }, + { "isnan", uc_isnan }, }; void uc_module_init(uc_vm_t *vm, uc_value_t *scope) -- cgit v1.2.3