Complex<(MyNumeric Value a, MyNumeric negative Value b) displayed (a - (negative Value b)*i)

Please tell me how to inspect () and to_s () the negative imaginary part correctly in the class for making it an element of Complex.

–(1) Example that worked as expected;

p "Complex<Rational<Integer>,BigDecaimal.negative>>",Complex(Rational(-1,2), Rational(-34567890123456789,5))

 #=> ((-1/2)-(34567890123456789/5)*i)  <-- as well.

– (2) Examples of unexpected behavior;

p  Complex(MyNumeric.new(-10,1,5), MyNumeric.new(-9,1,3)), Complex(z.real.to_f, z.imag.to_f)
 #=>  ((-10 + 1/2 + 5*√(3)/2) - (-9 + 1/2 + 3*√(3)/2)*i)    <-- unexpected behavior!!
 ###  it means   (-5.169872981077807 +  5.901923788646684i)
  #-> (-5.169872981077807 - 5.901923788646684i) <-- expected behavior

  #=> ((-10 + 1/2 + 5*√(3)/2) + (-9 + 1/2 + 3*√(3)/2)*i) <-- expected behavior

Hi jumnichi kose,

It seems like you are not handling the negation of the imaginary part correctly in your class. In Ruby, you can manage this by defining or overriding the unary - method within your MyNumeric class.

You should implement it in such a way that it returns a new instance with its value negated. Here’s a basic example:

class MyNumeric
  # Your existing code ...

  def -@
    MyNumeric.new(-self.value)
  end
end

This will ensure that when the unary minus is employed on a MyNumeric object, it will return a new object with the negation of the original value.

If your MyNumeric class is more complex than just containing one value, make sure to modify the -@ method accordingly to correctly negate all pertinent internal state.

Let me know if this helps or you need more guidance.
-Bobby

Thank to Anser.
With the code addition you suggested,
I was able to get the expected display.

I have a slight dissatisfaction with the display contents in the latter part, but should I put up with it?

class MyNumeric < Numeric
    attr_accessor :num, :rt3h,:hf
        #######
 def -@
        MyNumeric.new(-@num, -@hf, -@rt3h)
    end
end

p z=Complex(MyNumeric.new(-10,1,5), MyNumeric.new(-9,1,3)),z.imag, z.imag.class
#=-> ((-10 + 1/2 + 5√(3)/2)-(8 + 1/2 + -3√(3)/2)*i)

" (-9 + 1/2 + 3*√(3)/2) "  value equvalence to  "-(8 + 1/2 + -3*√(3)/2)*i)"