# Bug in ruby complex module:

**URL:** <https://rubytalk.org/t/bug-in-ruby-complex-module/43394>\
**Category:** ruby-talk\
**Created:** [29 December 2007 17:18 UTC](https://rubytalk.org/t/bug-in-ruby-complex-module/43394 "2007-12-29T17:18:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tommy\_Nordgren](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@Tommy\_Nordgren](https://rubytalk.org/u/Tommy_Nordgren)\
**Post date:** [29 December 2007 17:18 UTC](https://rubytalk.org/t/bug-in-ruby-complex-module/43394/1 "2007-12-29T17:18:36Z")

</div>

Transcript :  
emac$ irb  
\>\> require 'complex'  
=\> true  
\>\> cc = Complex(Float::MAX/2, Float::MAX / 2)  
=\> Complex(8.98846567431158e+307, 8.98846567431158e+307)  
\>\> cc / cc  
=\> Complex(NaN, NaN)  
\>\> require 'cpfixup'  
=\> true  
\>\> cc / cc  
=\> Complex(1.0, 0.0)  
\>\> exit  
Work Directory: ~ :  
emac$  
Th source of the fix (in a file called 'cpfixup.rb' in my home directory is

require 'complex'

class Complex \< Numeric

&nbsp;&nbsp;&nbsp;def / (other)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if other.kind\_of?(Complex)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;magn = other.abs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp = Complex.new(other.real/magn , other.image/magn)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self \* tmp.conjugate / magn  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif Complex.generic?(other)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Complex(@real/other, @image/other)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x, y = other.coerce(self)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x / y  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

> **···**
>
> ------------------------------------  
> This sig is dedicated to the advancement of Nuclear Power  
> Tommy Nordgren  
> [tommy.nordgren@comhem.se](mailto:tommy.nordgren@comhem.se)
