-
Notifications
You must be signed in to change notification settings - Fork 21
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
Better support for complex numbers #1043
Comments
I assume |
That's odd, works in SharpLab but not on my VS: open System.Numerics
let I = Complex.ImaginaryOne // worst thing in .Net complex numbers is this illogical name
let x = 5. + 4.*I Allowing more numeric literal types and using one for imaginary complex numbers is fine IMO. I don't think we are going to allow |
@charlesroddie
That PR will introduce an overload resolution rule of |
Apparently it will be possible after this RFC with user-defined overloads of (+): |
Programming with complex numbers is just super-rare in practice. I can't see us baking in support for this into the core of F# - programming with these is quite possible using helper functions and so on. |
Better support for complex numbers
F# has numeric literal suffixes for almost all numeric types, including the 8 types of fixed-size integers, 2 types of native integers, 1 type of arbitrary-sized integer, and 3 types of floating-point numbers. However, the cousin of
bigint
inSystem.Numerics
,Complex
doesn’t get much love.I propose we make this possible:
How does this work? First, FSharp.Core will define a
NumericLiterali
(like the existingNumericLiteralI
forbigint
s) with aFromFloat
is used (#445):This makes the example code equivalent to
Next, we apply the rule of implicit
int32 -> float
as added by dotnet/fsharp#10884:Now we arrive at currently compilable code.
The existing way of approaching this problem in F# is to
open System.Numerics
and awkwardly call the constructor despite not having to do this for its cousinbigint
. F# tries to compete in the data science and scripting space, but the competitors Python and Julia both have complex number literals built-in:https://docs.python.org/3/library/cmath.html
b = 1.2 + 2j
https://docs.julialang.org/en/v1/manual/complex-and-rational-numbers/
b = 1.2 + 2im
We should be on-par with them as well.
All these operators for
complex
are supported fine currently:But these error:
For
abs
, since theAbs
ofcomplex
returnsfloat
which is a different type fromcomplex
, this may need its own function calledabsc
(c
forcomplex
, just likef
forfloat32
. The generic ones and zeros should be fixed as well.For consistency with
nan
/nanf
andinfinity
/infinityf
,nanc
andinfinityc
should be added as well.Maybe
conj
andpolar
can be added as well for completeness?Pros and Cons
The advantages of making this adjustment to F# are
The disadvantages of making this adjustment to F# are that this is another thing to learn. However, these convenience features are already available for existing numeric types other than
complex
. Why leave this out?Extra information
Estimated cost (XS, S, M, L, XL, XXL): S
Related suggestions:
#445 - Extend custom numeric types to support floating point literals
#849 - FS-1093 Additional type directed conversions
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
For Readers
If you would like to see this issue implemented, please click the 👍 emoji on this issue. These counts are used to generally order the suggestions by engagement.
The text was updated successfully, but these errors were encountered: