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

**URL:** <https://www.ruby-forum.com/t/complex-mynumeric-value-a-mynumeric-negative-value-b-displayed-a-negative-value-b-i/264132>\
**Category:** Development\
**Created:** [July 24, 2023, 1:44am UTC](https://www.ruby-forum.com/t/complex-mynumeric-value-a-mynumeric-negative-value-b-displayed-a-negative-value-b-i/264132 "2023-07-24T01:44:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jumnichi.kose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@jumnichi.kose](https://www.ruby-forum.com/u/jumnichi.kose)\
**Post date:** [July 24, 2023, 1:44am UTC](https://www.ruby-forum.com/t/complex-mynumeric-value-a-mynumeric-negative-value-b-displayed-a-negative-value-b-i/264132/1 "2023-07-24T01:44:09Z")

</div>

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;

```auto
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;

```auto
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

```

---

<div class="post-metadata">

**Author:** ![robert-b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/54ee81/32.png) [@robert-b](https://www.ruby-forum.com/u/robert-b)\
**Post date:** [July 24, 2023, 1:44am UTC](https://www.ruby-forum.com/t/complex-mynumeric-value-a-mynumeric-negative-value-b-displayed-a-negative-value-b-i/264132/2 "2023-07-24T01:44:19Z")

</div>

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:

```ruby
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

---

<div class="post-metadata">

**Author:** ![jumnichi.kose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@jumnichi.kose](https://www.ruby-forum.com/u/jumnichi.kose)\
**Post date:** [July 24, 2023, 1:53am UTC](https://www.ruby-forum.com/t/complex-mynumeric-value-a-mynumeric-negative-value-b-displayed-a-negative-value-b-i/264132/3 "2023-07-24T01:53:44Z")

</div>

> [@robert-b](#):
>
> ```auto
> def -@
> MyNumeric.new(-self.value)
> end
> 
> ```

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?

```auto
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)

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