Skip to content

Commit

Permalink
math: add isnan() function
Browse files Browse the repository at this point in the history
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 <jo@mein.io>
  • Loading branch information
jow- committed Nov 29, 2022
1 parent eef83d3 commit 8366102
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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)
Expand Down

0 comments on commit 8366102

Please sign in to comment.