1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
use warnings;
use strict;
use Test::More tests => 46;
BEGIN { use_ok "Data::Float", qw(
copysign
have_signed_zero have_infinite have_nan significand_bits float_is_nan
); }
ok copysign(+1.2, +5) == +1.2;
ok copysign(-1.2, +5) == +1.2;
ok copysign(+1.2, -5) == -1.2;
ok copysign(-1.2, -5) == -1.2;
sub zpat($) { my($z) = @_; my $nz = -$z; sprintf("%+.f%+.f%+.f",$z,$nz,-$nz) }
my($z, $r);
$z = 0; $r = copysign($z, +5);
is zpat($z), "+0+0+0"; is zpat($r), "+0+0+0"; ok $r == 0;
$z = 0; $r = copysign($z, -5);
is zpat($z), "+0+0+0"; is zpat($r), "+0+0+0"; ok $r == 0;
SKIP: {
skip "no signed zero", 12 unless have_signed_zero;
$z = +0.0; $r = copysign($z, +5);
is zpat($z), "+0-0+0"; is zpat($r), "+0-0+0"; ok $r == 0;
$z = -0.0; $r = copysign($z, +5);
is zpat($z), "-0+0-0"; is zpat($r), "+0-0+0"; ok $r == 0;
$z = +0.0; $r = copysign($z, -5);
is zpat($z), "+0-0+0"; is zpat($r), "-0+0-0"; ok $r == 0;
$z = -0.0; $r = copysign($z, -5);
is zpat($z), "-0+0-0"; is zpat($r), "-0+0-0"; ok $r == 0;
}
$z = 0; ok copysign(+1.2, $z) == +1.2; is zpat($z), "+0+0+0";
$z = 0; ok copysign(-1.2, $z) == +1.2; is zpat($z), "+0+0+0";
SKIP: {
skip "no signed zero", 8 unless have_signed_zero;
$z = +0.0; ok copysign(+1.2, $z) == +1.2; is zpat($z), "+0-0+0";
$z = +0.0; ok copysign(-1.2, $z) == +1.2; is zpat($z), "+0-0+0";
$z = -0.0; ok copysign(+1.2, $z) == -1.2; is zpat($z), "-0+0-0";
$z = -0.0; ok copysign(-1.2, $z) == -1.2; is zpat($z), "-0+0-0";
}
SKIP: {
skip "infinities not available", 8 unless have_infinite;
no strict "refs";
my $pinf = &{"Data::Float::pos_infinity"};
my $ninf = &{"Data::Float::neg_infinity"};
ok copysign($pinf, +5) == $pinf;
ok copysign($ninf, +5) == $pinf;
ok copysign($pinf, -5) == $ninf;
ok copysign($ninf, -5) == $ninf;
ok copysign(+1.2, $pinf) == +1.2;
ok copysign(-1.2, $pinf) == +1.2;
ok copysign(+1.2, $ninf) == -1.2;
ok copysign(-1.2, $ninf) == -1.2;
}
SKIP: {
skip "NaN not available", 3 unless have_nan;
no strict "refs";
my $nan = &{"Data::Float::nan"};
ok float_is_nan(copysign($nan, +5));
ok float_is_nan(copysign($nan, $nan));
ok abs(copysign(+1.2, $nan)) == 1.2;
}
1;
|