Skip to content

Commit

Permalink
Fix stringif. of Complex failing on negative zero in img. part
Browse files Browse the repository at this point in the history
A negative zero goes through <0e0 conditional, at which point it
ends up as -0 via p6box_s, concatenated with a '+'.

1/-0e0 == -Inf, so use that knowledge to figure out the sign of the
zero. The nqp::abs_n() op seemed to have no point in there to me
(and is slower), so I removed it.

Fixes RT#130329: https://rt.perl.org/Ticket/Display.html?id=130329
  • Loading branch information
zoffixznet committed Dec 12, 2016
1 parent 55cf6fa commit a3735af
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/core/Complex.pm
Expand Up @@ -48,10 +48,14 @@ my class Complex is Cool does Numeric {

method Complex() { self }
multi method Str(Complex:D:) {
my Str $i = nqp::isnanorinf($!im) ?? '\\i' !! 'i';
$!im < 0e0
?? nqp::p6box_s($!re) ~ '-' ~ nqp::p6box_s(nqp::abs_n($!im)) ~ $i
!! nqp::p6box_s($!re) ~ '+' ~ nqp::p6box_s($!im) ~ $i;
nqp::p6box_s($!re) # real part
~ ( # sign ('-' will be automagically given by p6box_s)
# second part is true if we have a negative zero
$!im < 0e0 || (
! $!im && nqp::islt_n(nqp::div_n(1e0, $!im), 0e0)
) ?? '' !! '+'
) ~ nqp::p6box_s($!im) # imaginary part
~ (nqp::isnanorinf($!im) ?? '\\i' !! 'i'); # separate NaN/Inf with \
}

multi method perl(Complex:D:) {
Expand Down

1 comment on commit a3735af

@lizmat
Copy link
Contributor

@lizmat lizmat commented on a3735af Dec 12, 2016

Choose a reason for hiding this comment

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

FWIW, this was my solution:

  •    my Str $i = nqp::isnanorinf($!im) ?? '\\i' !! 'i';
    
  •    $!im < 0e0
    
  •        ?? nqp::p6box_s($!re) ~ '-' ~ nqp::p6box_s(nqp::abs_n($!im)) ~ $i
    
  •        !! nqp::p6box_s($!re) ~ '+' ~ nqp::p6box_s($!im) ~ $i;
    
  •    nqp::concat(
    
  •      $!re,
    
  •      nqp::concat(
    
  •        nqp::if(nqp::iseq_i(nqp::ord($!im),45),'','+'),
    
  •        nqp::concat(
    
  •          $!im,
    
  •          nqp::if(nqp::isnanorinf($!im),'\\i','i')
    
  •        )
    
  •      )
    
  •    )
    

but that fails test #10 in t/spec/S32-num/negative-zero.t, but I think the test is wonky? Why not use "ok foo" instead of "is-deeply foo, True" ?

Please sign in to comment.