Skip to content

Commit

Permalink
implement is_inf_or_nan operator
Browse files Browse the repository at this point in the history
  • Loading branch information
moritz committed Dec 6, 2011
1 parent 002173a commit b8b9be6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions include/parrot/datatypes.h
Expand Up @@ -160,13 +160,16 @@ const struct _data_types data_types[] = {
# define PARROT_FLOATVAL_IS_POSINF(x) (isinf(x) && (x) > 0)
# define PARROT_FLOATVAL_IS_NEGINF(x) (isinf(x) && (x) < 0)
# define PARROT_FLOATVAL_IS_NAN(x) isnan(x)
# define PARROT_FLOATVAL_IS_INF_OR_NAN(x) (isnan(x) || isinf(x))
#else
# define PARROT_FLOATVAL_INF_POSITIVE Parrot_dt_divide_floatval_by_zero(interp, 1.0)
# define PARROT_FLOATVAL_INF_NEGATIVE Parrot_dt_divide_floatval_by_zero(interp, -1.0)
# define PARROT_FLOATVAL_NAN_QUIET Parrot_dt_divide_floatval_by_zero(interp, 0.0)
# define PARROT_FLOATVAL_IS_POSINF(x) ((x) == PARROT_FLOATVAL_INF_POSITIVE)
# define PARROT_FLOATVAL_IS_NEGINF(x) ((x) == PARROT_FLOATVAL_INF_NEGATIVE)
# define PARROT_FLOATVAL_IS_NAN(x) ((x) != (x))
# define PARROT_FLOATVAL_IS_INF_OR_NAN(x) (PARROT_FLOATVAL_IS_POSINF(x) \
|| PARROT_FLOATVAL_IS_NEGINF(x) || PARROT_FLOATVAL_IS_NAN(x))
#endif

#define PARROT_CSTRING_INF_POSITIVE "Inf"
Expand Down
10 changes: 10 additions & 0 deletions src/ops/math.ops
Expand Up @@ -722,6 +722,16 @@ inline op sqrt(out NUM, in NUM) :base_core {
$1 = sqrt((FLOATVAL)$2);
}

=item B<is_inf_or_nan>(out INT, in NUM)

Set $1 to 1 if $2 is Inf, -Inf or NaN, and 0 otherwise

=cut

inline op is_inf_or_nan(out INT, in NUM) :base_core {
$1 = PARROT_FLOATVAL_IS_INF_OR_NAN($2)
}

=back

=cut
Expand Down
23 changes: 22 additions & 1 deletion t/op/inf_nan.t
Expand Up @@ -17,12 +17,13 @@ Tests for mathematical operations with Inf and Nan.

.sub main :main
.include 'test_more.pir'
plan(37)
plan(42)

test_basic_arith()
test_sqrt()
test_neg()
test_mix_nan_inf()
test_is_inf_or_nan()
test_rounding_n()
test_rounding_i()
test_nan_complex()
Expand Down Expand Up @@ -118,6 +119,26 @@ Tests for mathematical operations with Inf and Nan.
is($N2, '-Inf', '... ceil -Inf')
.end

.sub test_is_inf_or_nan
$N0 = 'NaN'
$I0 = is_inf_or_nan $N0
ok($I0, 'is_inf_or_nan NaN')
$N0 = 'Inf'
$I0 = is_inf_or_nan $N0
ok($I0, 'is_inf_or_nan Inf')
$N0 = '-Inf'
$I0 = is_inf_or_nan $N0
ok($I0, 'is_inf_or_nan -Inf')
$N0 = 0
$I0 = is_inf_or_nan $N0
$I1 = not $I0
ok($I1, 'is_inf_or_nan 0')
$N0 = 123.4e5
$I0 = is_inf_or_nan $N0
$I1 = not $I0
ok($I1, 'is_inf_or_nan 123.4e5')
.end

#pir_output_is(<<'CODE',<<OUTPUT, "TT #370 Rounding inf/nan");
.sub test_rounding_i
$N0 = 'Inf'
Expand Down

0 comments on commit b8b9be6

Please sign in to comment.