Skip to content

Add is_inf and is_nan functions to builtin:: #22059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: blead
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ XS(XS_builtin_inf)
XSRETURN_NV(NV_INF);
}

XS(XS_builtin_is_inf);
XS(XS_builtin_is_inf)
{
dXSARGS;
SV *val = ST(0);
if(items != 1)
croak_xs_usage(cv, "val");
SvGETMAGIC(val);
if(SvNOK(val) && Perl_isinf(SvNV(val)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NOK check seems non-perlish to me, it also breaks overloads:

tony@venus:.../git/perl6$ ./perl -Ilib ../22059.pl 
Built-in function 'builtin::inf' is experimental at ../22059.pl line 6.
Built-in function 'builtin::is_inf' is experimental at ../22059.pl line 11.
Built-in function 'builtin::is_inf' is experimental at ../22059.pl line 13.

1

1
tony@venus:.../git/perl6$ cat ../22059.pl
use v5.38;
use builtin qw(is_inf inf);
use POSIX();

package Foo {
  use overload '0+' => sub { inf };
}

my $x = bless {}, "Foo";

say is_inf($x);
say POSIX::isinf($x);
say is_inf("Inf");
say POSIX::isinf("Inf");

XSRETURN_YES;
else
XSRETURN_NO;
}

XS(XS_builtin_nan);
XS(XS_builtin_nan)
{
Expand All @@ -109,6 +123,20 @@ XS(XS_builtin_nan)
XSRETURN_NV(NV_NAN);
}

XS(XS_builtin_is_nan);
XS(XS_builtin_is_nan)
{
dXSARGS;
SV *val = ST(0);
if(items != 1)
croak_xs_usage(cv, "val");
SvGETMAGIC(val);
if(SvNOK(val) && Perl_isnan(SvNV(val)))
XSRETURN_YES;
else
XSRETURN_NO;
}

enum {
BUILTIN_CONST_FALSE,
BUILTIN_CONST_TRUE,
Expand Down Expand Up @@ -550,6 +578,8 @@ static const struct BuiltinFuncDescriptor builtins[] = {

/* unary functions */
{ "is_bool", NO_BUNDLE, &XS_builtin_func1_scalar, &ck_builtin_func1, OP_IS_BOOL, true },
{ "is_inf", NO_BUNDLE, &XS_builtin_is_inf, &ck_builtin_func1, 0, true },
{ "is_nan", NO_BUNDLE, &XS_builtin_is_nan, &ck_builtin_func1, 0, true },
{ "weaken", SHORTVER(5,39), &XS_builtin_func1_void, &ck_builtin_func1, OP_WEAKEN, false },
{ "unweaken", SHORTVER(5,39), &XS_builtin_func1_void, &ck_builtin_func1, OP_UNWEAKEN, false },
{ "is_weak", SHORTVER(5,39), &XS_builtin_func1_scalar, &ck_builtin_func1, OP_IS_WEAK, false },
Expand Down
23 changes: 22 additions & 1 deletion lib/builtin.pm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package builtin 0.014;
package builtin 0.015;

use strict;
use warnings;
Expand Down Expand Up @@ -154,6 +154,17 @@ This function is currently B<experimental>.

Returns the floating-point infinity value.

=head2 is_inf

$inf = is_inf($val);

This function is currently B<experimental>.

Returns true when given the postive or negative floating-point infinity values,
false if not. A floating-point infinity value can be generated when certain
floating-point operations are performed, based on platform-specific floating
point handling.

=head2 nan

$num = nan;
Expand All @@ -162,6 +173,16 @@ This function is currently B<experimental>.

Returns the floating-point "Not-a-Number" value.

=head2 is_nan

$nan = is_nan($val);

This function is currently B<experimental>.

Returns true when given the floating-point "Not-A-Number" value, false if not.
The floating-point NaN value can be generated when certain floating-point
operations are performed, based on platform-specific floating point handling.

=head2 weaken

weaken($ref);
Expand Down
36 changes: 34 additions & 2 deletions lib/builtin.t
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,17 @@ package FetchStoreCounter {
is(prototype(\&builtin::is_bool), '$', 'is_bool prototype');
}

# float constants
# floats
{
use builtin qw( inf nan );
use builtin qw( inf nan is_inf is_nan is_bool );

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(inf != -inf, 'inf is not equal to -inf');

# Invoke the real XSUB
my $inf = ( \&builtin::inf )->();
ok($inf == $inf + 1, 'inf returned by real xsub');
Expand All @@ -68,6 +70,36 @@ package FetchStoreCounter {

my $nan = ( \&builtin::nan )->();
ok($nan != $nan, 'NaN returned by real xsub');

# Test is_inf and is_nan from various sources
ok(is_inf(inf), 'is_inf(inf) is true');
ok(is_inf(-inf), 'is_inf(-inf) is true');
ok(is_nan(nan), 'is_nan(nan) is true');

ok(is_inf($inf), 'is_inf(inf) from xsub is true');
ok(is_inf($inf), 'is_inf(inf) from xsub is true');
ok(is_nan($nan), 'is_nan(nan) from xsub is true');

ok(is_nan(1*(inf-inf)), 'is_nan is true for calculated nan');
ok(is_inf(9**9**9), 'is_inf is true for calculated inf');

# Try some obvious variations that should fail.
ok(!is_nan($inf), 'is_nan(inf) is false');
ok(!is_inf($nan), 'is_inf(nan) is false');

# Strings that look like stringified floats should not pass
ok(!is_nan('NaN'), 'is_nan(\'NaN\') is false');
ok(!is_inf('inf'), 'is_inf(\'inf\') is false');

ok(!is_nan(0), 'is_nan(0) is false');
ok(!is_inf(0), 'is_inf(0) is false');

ok(!is_nan(0.0), 'is_nan(0.0) is false');
ok(!is_inf(0.0), 'is_inf(0.0) is false');

# Ensure that is_nan and is_inf return real bools
ok(is_bool(is_nan($nan)), 'is_nan returns bool');
ok(is_bool(is_inf($inf)), 'is_inf returns bool');
}

# weakrefs
Expand Down