-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Added generic Complex type, floating-point comparison, and ** exponentiation operator for floats and complex.
#4666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… exponentiation operator, made Complex a generic type
| Complex*[T] = tuple[re, im: T] | ||
| ## a complex number, consisting of a real and an imaginary part | ||
| Complex128* = tuple[re, im: float64] | ||
| Complex64* = tuple[re, im: float32] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isnt it better to define Complex128 and Complex64 as Complex[float64] and Complex[float32]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's indeed confusing.
|
The type converters that I put in to enable the complex numbers to play nicely with int literals and float literals of the wrong type are making it very hard to reason about the type of a result, and causing strange errors when I make the simple changes that were suggested. Furthermore, in certain cases, the code passes the Nim type checker and compiles to C/C++ successfully, but gcc throws errors from its type checker about the wrong type of struct being passed to a function. However, since the implementations of many of the functions in the module use float64 literals, I would either have to duplicate every proc with an implementation using float32 literals or prefix every literal with a conversion function (e.g. replace As a further note, I think it's a good idea to have the imaginary constant (0.0, 1.0) exported from the module, but input would be helpful on the exact symbol to use. Examples include: Your thoughts are much appreciated |
|
@mihirparadkar, tbh I can't really understand the problem you're describing. Could you please elaborate and come up with an example of code that doesn't work when you define the types as suggested. |
|
Right now, the following code works: var a64: Complex64 = (1.0f, 2.0f)
var numer = 1.0
var b = numer / a64However, the inferred type of One fix to this is to disallow dividing arcsech[T](z: Complex[T]): Complex[T]which has the implementation result = ln(1.0/z+sqrt(1.0/z+1.0)*sqrt(1.0/z-1.0))Those expressions of the form |
|
What is the rational for using |
That seems the best way to do it, yes. |
| result.re = s*cos(r) | ||
| result.im = s*sin(r) | ||
|
|
||
| proc `**` *[T](x, y: Complex[T]): Complex[T] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, Nim convention is to use ^ as exponentiation operator
|
this is superseeded by #9590 |
Made the Complex type generic to accommodate single-precision complex numbers, added a
=~operator as used in the Complex module and other outside numeric modules, and a convenience**operator as a wrapper tomath.pow, all of which also operate on complex numbers.