Crystal: Suggestion: move some Complex鈥檚 instance methods into Math module

Created on 5 Sep 2020  路  4Comments  路  Source: crystal-lang/crystal

Some Complex鈥檚 methods have the same name as Math鈥檚 methods, so the syntax of calling mathematical methods depends on the type:

Math.sqrt(2)           # Ok with Int
Math.sqrt(2.0)         # Ok with Float 
Math.sqrt(2.0 + 1.0.i) # Error with Complex
(2.0 + 1.0.i).sqrt     # Current syntax for Complex

I think it would be better to have the same namespace for all mathematical methods.

These methods have their Math counterpart:
Complex#exp => Math#exp(value)
Complex#log => Math#log(value)
Complex#log2 => Math#log2(value)
Complex#log10 => Math#log10(value)
Complex#sqrt => Math#sqrt(value)

Most helpful comment

Keeping them only in the Math module is a breaking change that we can tolerate right now I think. A PR is welcome in that direction.

All 4 comments

I think is fine to add overloads for those methods in Math module to handle complex numbers.

You think it鈥檚 better to move them, which will lead to a breaking change, or to create aliases? Moving them seems more logical to me because Int and Float don鈥檛 have these instances methods.

I think we should copy Ruby. These methods only exist in Math so in my opinion we should do the same.

Keeping them only in the Math module is a breaking change that we can tolerate right now I think. A PR is welcome in that direction.

Was this page helpful?
0 / 5 - 0 ratings