cvsuser     04/10/15 00:48:26

  Modified:    classes  complex.pmc
               t/pmc    complex.t
  Log:
  Re: [perl #31949] [TODO] finhish complex PMC
  
  > The Complex PMC arithmetic MMD vtables (add, subtract, divide) blindly
  > assume that the RHS argument C<value> is complex too. This needs fixing
  > and tests.
  
  Fixed, tested. (the tests were there, only commented out)
  
  I only added implementations for MMD_Complex and MMD_DEFAULT.
  
  Courtesy of Ion Alexandru Morega <[EMAIL PROTECTED]>
  
  Revision  Changes    Path
  1.12      +24 -4     parrot/classes/complex.pmc
  
  Index: complex.pmc
  ===================================================================
  RCS file: /cvs/public/parrot/classes/complex.pmc,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -w -r1.11 -r1.12
  --- complex.pmc       2 Oct 2004 15:22:01 -0000       1.11
  +++ complex.pmc       15 Oct 2004 07:48:19 -0000      1.12
  @@ -1,6 +1,6 @@
   /*
   Copyright: 2004 The Perl Foundation.  All Rights Reserved.
  -$Id: complex.pmc,v 1.11 2004/10/02 15:22:01 jrieks Exp $
  +$Id: complex.pmc,v 1.12 2004/10/15 07:48:19 leo Exp $
   
   =head1 NAME
   
  @@ -361,8 +361,7 @@
       }
   
       FLOATVAL get_number () {
  -        internal_exception(1, "Complex: unimp get_number");
  -        return (FLOATVAL)0;
  +        return sqrt(RE(SELF)*RE(SELF) + IM(SELF)*IM(SELF));
       }
   
       STRING* get_string () {
  @@ -598,10 +597,17 @@
   */
   
       void add (PMC* value, PMC* dest) {
  +MMD_Complex: {
           VTABLE_morph(INTERP, dest, enum_class_Complex);
           RE(dest) = RE(SELF) + RE(value);
           IM(dest) = IM(SELF) + IM(value);
       }
  +MMD_DEFAULT: {
  +        VTABLE_morph(INTERP, dest, enum_class_Complex);
  +        RE(dest) = RE(SELF) + VTABLE_get_number(INTERP, value);
  +        IM(dest) = IM(SELF);
  +        }
  +    }
   
       void add_int (INTVAL value, PMC* dest) {
           VTABLE_morph(INTERP, dest, enum_class_Complex);
  @@ -630,10 +636,17 @@
   */
   
       void subtract (PMC* value, PMC* dest) {
  +MMD_Complex: {
           VTABLE_morph(INTERP, dest, enum_class_Complex);
           RE(dest) = RE(SELF) - RE(value);
           IM(dest) = IM(SELF) - IM(value);
       }
  +MMD_DEFAULT: {
  +        VTABLE_morph(INTERP, dest, enum_class_Complex);
  +        RE(dest) = RE(SELF) - VTABLE_get_number(INTERP, value);
  +        IM(dest) = IM(SELF);
  +        }
  +    }
   
       void subtract_int (INTVAL value, PMC* dest) {
           VTABLE_morph(INTERP, dest, enum_class_Complex);
  @@ -719,6 +732,7 @@
   */
   
       void divide (PMC* value, PMC* dest) {
  +MMD_Complex: {
           FLOATVAL mod, re, im;
   
           VTABLE_morph(INTERP, dest, enum_class_Complex);
  @@ -733,6 +747,12 @@
           RE(dest) = re;
           IM(dest) = im;
       }
  +MMD_DEFAULT: {
  +        VTABLE_morph(INTERP, dest, enum_class_Complex);
  +        RE(dest) = RE(SELF) / VTABLE_get_number(INTERP, value);
  +        IM(dest) = IM(SELF) / VTABLE_get_number(INTERP, value);
  +        }
  +    }
   
       void divide_int (INTVAL value, PMC* dest) {
           VTABLE_morph(INTERP, dest, enum_class_Complex);
  
  
  
  1.7       +37 -34    parrot/t/pmc/complex.t
  
  Index: complex.t
  ===================================================================
  RCS file: /cvs/public/parrot/t/pmc/complex.t,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -w -r1.6 -r1.7
  --- complex.t 1 Oct 2004 21:16:52 -0000       1.6
  +++ complex.t 15 Oct 2004 07:48:26 -0000      1.7
  @@ -1,6 +1,6 @@
   #! perl -w
   # Copyright: 2001-2003 The Perl Foundation.  All Rights Reserved.
  -# $Id: complex.t,v 1.6 2004/10/01 21:16:52 jrieks Exp $
  +# $Id: complex.t,v 1.7 2004/10/15 07:48:26 leo Exp $
   
   =head1 NAME
   
  @@ -180,11 +180,11 @@
       print P1
       print "\n"
   
  -#    set P0, "2 + i"
  -#    set P2, 3.3
  -#    add P1, P0, P2
  -#    print P1
  -#    print "\n"
  +    set P0, "2 + i"
  +    set P2, 3.3
  +    add P1, P0, P2
  +    print P1
  +    print "\n"
   
       set P0, "3 + 5i"
       add P1, P0, 2
  @@ -203,6 +203,7 @@
   1+1i
   0-1i
   1-1i
  +5.3+1i
   5+5i
   0+2i
   OUTPUT
  @@ -233,11 +234,11 @@
       print P1
       print "\n"
   
  -#    set P0, "1 - 4i"
  -#    set P2, -1.0
  -#    sub P1, P0, P2
  -#    print P1
  -#    print "\n"
  +    set P0, "1 - 4i"
  +    set P2, -1.0
  +    sub P1, P0, P2
  +    print P1
  +    print "\n"
   
       set P0, "- 2 - 2i"
       sub P1, P0, -4
  @@ -256,6 +257,7 @@
   1+1i
   0-1i
   -1-1i
  +2-4i
   2-2i
   1.8+1i
   OUTPUT
  @@ -286,11 +288,11 @@
       print P1
       print "\n"
   
  -#    set P0, "2 - 2i"
  -#    set P2, 0.5
  -#    mul P1, P0, P2
  -#    print P1
  -#    print "\n"
  +    set P0, "2 - 2i"
  +    set P2, 0.5
  +    mul P1, P0, P2
  +    print P1
  +    print "\n"
   
       set P0, "1 - i"
       mul P1, P0, 2
  @@ -309,6 +311,7 @@
   5+2i
   0+3i
   3+6i
  +1-1i
   2-2i
   1-1i
   OUTPUT
  @@ -339,11 +342,11 @@
       print P1
       print "\n"
   
  -#    set P0, "-3 + 6i"
  -#    set P2, 3.0
  -#    div P1, P0, P2
  -#    print P1
  -#    print "\n"
  +    set P0, "-3 + 6i"
  +    set P2, 3.0
  +    div P1, P0, P2
  +    print P1
  +    print "\n"
   
       set P0, "-2 + 3i"
       div P1, P0, 2
  @@ -362,44 +365,44 @@
   5-3i
   25+0i
   3-4i
  +-1+2i
   -1+1.5i
   4-6i
   OUTPUT
   
  -SKIP: {
  -  skip("modulus not implemented", 1);
  -output_is(<<'CODE', <<'OUTPUT', "get int/num/bool");
  +output_is(<<"CODE", <<'OUTPUT', "get int/num/bool");
  [EMAIL PROTECTED] $fp_equality_macro ]}
           new P0, .Complex
  -        set P0, "1.6 - .9i"
  +        set P0, "2 - 1.5i"
           print P0
  -        print "\n"
  +        print "\\n"
   
           set I0, P0
           print I0
  -        print "\n"
  +        print "\\n"
   
           set N0, P0
  -        print N0
  -        print "\n"
  +        .fp_eq( N0, 2.5, OK )
  +        print "not "
  +OK:     print "ok\\n"
   
           if P0, TRUE
           print "not "
  -TRUE:   print "true\n"
  +TRUE:   print "true\\n"
   
           set P0, "0"
           unless P0, FALSE
           print "not "
  -FALSE:  print "false\n"
  +FALSE:  print "false\\n"
   
           end
   CODE
  -1.6-0.9i
  +2-1.5i
   2
  -2.5
  +ok
   true
   false
   OUTPUT
  -}
   
   output_is(<<"CODE", <<'OUTPUT', "get keyed");
   @{[ $fp_equality_macro ]}
  
  
  

Reply via email to