Skip to content

Commit

Permalink
Add builtin::inf and builtin::nan
Browse files Browse the repository at this point in the history
  • Loading branch information
leonerd committed Jan 25, 2024
1 parent 48d3826 commit a9fa593
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
31 changes: 31 additions & 0 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,42 @@ XS(XS_builtin_false)
XSRETURN_NO;
}

XS(XS_builtin_inf);
XS(XS_builtin_inf)
{
dXSARGS;
if(items)
croak_xs_usage(cv, "");
EXTEND(SP, 1);
mPUSHn(NV_INF);
XSRETURN(1);
}

XS(XS_builtin_nan);
XS(XS_builtin_nan)
{
dXSARGS;
if(items)
croak_xs_usage(cv, "");
EXTEND(SP, 1);
mPUSHn(NV_NAN);
XSRETURN(1);
}

enum {
BUILTIN_CONST_FALSE,
BUILTIN_CONST_TRUE,
BUILTIN_CONST_INF,
BUILTIN_CONST_NAN,
};

static OP *ck_builtin_const(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
{
const struct BuiltinFuncDescriptor *builtin = NUM2PTR(const struct BuiltinFuncDescriptor *, SvUV(ckobj));

if(builtin->is_experimental)
warn_experimental_builtin(builtin->name);

SV *prototype = newSVpvs("");
SAVEFREESV(prototype);

Expand All @@ -107,6 +134,8 @@ static OP *ck_builtin_const(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
switch(builtin->ckval) {
case BUILTIN_CONST_FALSE: constval = &PL_sv_no; break;
case BUILTIN_CONST_TRUE: constval = &PL_sv_yes; break;
case BUILTIN_CONST_INF: constval = newSVnv(PL_inf.nv); break;
case BUILTIN_CONST_NAN: constval = newSVnv(PL_nan.nv); break;
default:
DIE(aTHX_ "panic: unrecognised builtin_const value %" IVdf,
builtin->ckval);
Expand Down Expand Up @@ -516,6 +545,8 @@ static const struct BuiltinFuncDescriptor builtins[] = {
/* constants */
{ "true", SHORTVER(5,39), &XS_builtin_true, &ck_builtin_const, BUILTIN_CONST_TRUE, false },
{ "false", SHORTVER(5,39), &XS_builtin_false, &ck_builtin_const, BUILTIN_CONST_FALSE, false },
{ "inf", NO_BUNDLE, &XS_builtin_inf, &ck_builtin_const, BUILTIN_CONST_INF, true },
{ "nan", NO_BUNDLE, &XS_builtin_nan, &ck_builtin_const, BUILTIN_CONST_NAN, true },

/* unary functions */
{ "is_bool", NO_BUNDLE, &XS_builtin_func1_scalar, &ck_builtin_func1, OP_IS_BOOL, true },
Expand Down
19 changes: 18 additions & 1 deletion lib/builtin.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package builtin 0.012;
package builtin 0.013;

use strict;
use warnings;
Expand All @@ -18,6 +18,7 @@ builtin - Perl pragma to import built-in utility functions
use builtin qw(
true false is_bool
inf nan
weaken unweaken is_weak
blessed refaddr reftype
created_as_string created_as_number
Expand Down Expand Up @@ -143,6 +144,22 @@ or any variable containing one of these results.
This function used to be named C<isbool>. A compatibility alias is provided
currently but will be removed in a later version.
=head2 inf
$num = inf;
This function is currently B<experimental>.
Returns the floating-point infinity value.
=head2 nan
$num = nan;
This function is currently B<experimental>.
Returns the floating-point "Not-a-Number" value.
=head2 weaken
weaken($ref);
Expand Down
12 changes: 12 additions & 0 deletions lib/builtin.t
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ package FetchStoreCounter {
is(prototype(\&builtin::is_bool), '$', 'is_bool prototype');
}

# float constants
{
use builtin qw( inf nan );

ok(inf, 'inf is true');
ok(inf > 1E10, 'inf is bigger than 1E10');
ok(inf == inf, 'inf is equal to inf');
ok(inf == inf + 1, 'inf is equal to inf + 1');

ok(nan != nan, 'NaN is not equal to NaN');
}

# weakrefs
{
use builtin qw( is_weak weaken unweaken );
Expand Down

0 comments on commit a9fa593

Please sign in to comment.