-
Notifications
You must be signed in to change notification settings - Fork 379
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
Syntax for complex numbers #1200
Comments
Wait, does it? The type of This seems more of a bug due to a weird thing the backend decides to |
Here are a few lines from a recent REPL session on the latest master branch: Main> :let x : Double
Main> :let x = sqrt(-1)
Main> x
0.0+1.0i I think it is weird that the Double type can do this, especially because of its correspondence with doubles in the C API. However I love that Idris supports complex numbers natively, because I write a lot of programs that use them. :)
Main> sqrt(sqrt(-1))
0.7071067811865476+0.7071067811865475i All of the other basic math functions work over the complex numbers as well, for example |
I think this is an undefined behaviour. The expected behaviour is probably Chez scheme seems to indeed specify that it'll try to make the best of it |
There we go: idris2 --cg node --no-banner
Main> :let main : IO ()
Main> :let main = print (sqrt (-1))
Main> :exec main
NaNMain> vs idris2 --no-banner
Main> :let main : IO ()
Main> :let main = print (sqrt (-1))
Main> :exec main
0.0+1.0iMain> |
Do you think it would be better to return NaN like If acos, log, sqrt and the other basic math functions weren't total, I would foresee either partiality cascading through programs, or a bunch of "just trust me" compiler directives. |
Returning Concerning proofs about floating point numbers: Alas, these will be very hard to do properly. For instance, even the following most basic axiom does not hold for Edit: Neither does the law of distributivity:
|
Yes, I agree. I think it would be better to return NaN for expressions like |
I support making import Syntax.WithProof
sqrtSafe : (d : Double) -> {auto 0 prf : d >= 0 = True} -> Double
sqrtSafe d = assert_total (sqrt d)
One : Double
One = sqrtSafe 1
{-
Error : Double
Error = sqrtSafe (-1)
-- While processing right hand side of Error. Can't find an implementation for False = True.
-}
Complicated : Double -> Double
Complicated x = case @@(x >= 0) of
(True ** prf) => sqrtSafe x
(False** _ ) => 0
Zero : Double
Zero = Complicated (-1)
|
|
IMHO as @Atomotron is saying, consistency is the most important factor. |
Another point to consider is that if there will ever be a codegen targeted at high performance, mirroring IEEE (i.e. whatever Intel believes should happen) will be the only reasonable option - as putting checks after every My understanding is not perfect, but I think that although hardware FP exception handling can be configured, it involves things like getting "SIGFPE" signals from the operating system[0]. We probably do not want to be getting signals from the operating system, and if we don't want to be emitting branches after every Lua, Javascript, C, and probably LLVM IR (since x86 does it) backends will all see simpler output if NaN is returned for out-of-domain calls to transcendental functions. Edit: I found this (flsqrt fl) alternate sqrt function in the Chez scheme documentation that always returns flonums. Maybe fixing this issue would be as simple as having the default codegen use these. [0] https://www.jayconrod.com/posts/33/trapping-floating-point-exceptions-in-linux |
… is returned instead of a complex type. See issue idris-lang#1200.
Yes, I think you're right: consistent, well-specified behaviour for the builtins |
Idris 2 supports complex numbers, but lacks an easy way to write them. Presently, the only way I know to get the imaginary unit
i
is with,sqrt (-1)
or,
log (-1) / pi
Since stringification will happily print
0.0 + 1.0i
after evaluatinglog (-1) / pi
, and sincethe Double (cast "0.0+1.0i")
will return successfully, it stands to reason that it should work the same way in source code. Most other types have a bijection between parsing and printing, and it stands to reason that the complex part of the double type should also.Of course it is possible to write expressions that evaluate to complex numbers, but that lacks the nice property of the printed form of types being valid source code. Idris would be best if it only had one Double parsing behavior, and not two subtly distinct behaviors, one for parsing source, and the other for
cast
.The text was updated successfully, but these errors were encountered: