Home > Blockchain >  Why Ruby doesn’t provide a `Complexe#modulo` method?
Why Ruby doesn’t provide a `Complexe#modulo` method?

Time:03-19

Ruby defines Numeric#%, aka Numeric#modulo to provide the remaining of receiver divided by an other Numeric. However, as the code bellow show, Complex is the only derived type which doesn’t provide a method for it:

ObjectSpace.each_object(Class){p "#{_1}: #{_1.method_defined?(:%)}" if _1.ancestors.include?(Numeric) unless _1 == Numeric}
"Complex: false"
"Rational: true"
"Float: true"
"Integer: true"
"Date::Infinity: true"

Is there a mathematical reason to avoid providing a reminder of complex numbers? In all cases, is there a documented reason for this engineering design choice?

Provision of an implementation of this operator for Complex, highlighting possible pitfall avoided if any, would also be welcome but more marginal for the topic of this question.

Side note: this as nothing to do with the notion of complex modulus, aka norm, which is also absent from Ruby standard library.

CodePudding user response:

Back in 2009, in Bug #1712, Matz says:

I don't think there's natural definition of modulo on complex numbers, so that we should undefine % method.

As a result, Complex doesn't have %, modulo, or divmod. (among others)

  • Related