Comments (10)
So looking at this, I realized that I also haven't accounted for length < 0
. We can make sense of this mathematically, but I'm not sure that we actually want to do so from a UX perspective. I'm somewhat tempted to make this non-failable, but with preconditions that length >= 0
(hence non-nan, non-negative) and phase.isFinite
, because then the rules and behavior are trivially explainable to users (and if experts want some specific other behavior, it's easy to implement).
The only real downside I see is that you can't round-trip zero and infinity to polar and back this way, which is slightly unfortunate.
from swift-numerics.
We could enable round-tripping by slightly changing the preconditions to require finite phase unless length is 0 or infinite:
length >= 0
phase.isFinite || (length == 0 || !length.isFinite)
from swift-numerics.
.nan, .nan
is not a representation for invalid results; it's treated as another infinity.
This is somewhat surprising. The advantage of this choice is that it lets us use the obvious expression for multiplication, (rr-ii, ri+ri) rather than having to detect infinities and handle them specially (if you try to distinguish infinity and nan, then you have to deal with the fact that (inf,0) * (0, i) would give you (nan, inf), which ... is that a nan or an infinity? This is a mess for C and C++,it results in everyone who cares about performance using their own multiplication instead of the language-provided one).
There are also downsides to this approach, but it pushes some of the weirdness out of basic arithmetic and onto less-frequently-used operations. I'm open to changing it, but there's a definite tension.
from swift-numerics.
.nan, .nan
is not a representation for invalid results; it's treated as another infinity.
I understand that it is treated as an infinity, yet I maintain that it is a representation of invalid results. Or at least, Complex.infinity
in general is currently used to represent invalid results:
.infinity - .infinity
is undefined, but represented by .infinity
.infinity / .infinity
is undefined, but represented by .infinity
(0, 0) / (0, 0)
is undefined, but represented by…wait, that’s (0, 0)
? Is this intentional? I would expect .infinity
.
from swift-numerics.
(0, 0) / (0, 0)
is undefined, but represented by…wait, that’s(0, 0)
? Is this intentional? I would expect.infinity
.
Yeah, that's a bug (#54)
from swift-numerics.
Yeah, the only downside is that it makes the behavior slightly less easy to explain. I'll sleep on it.
from swift-numerics.
Do we need an explicit precondition
at all? We could do something like this:
init(length: RealType, phase: RealType) {
if length == 0 {
self = .zero
} else if !length.isFinite {
self = .infinity
} else {
self.init(length * .cos(phase), length * .sin(phase))
}
}
This leaves negative length behaving as it currently does. We don’t necessarily have to document that negative lengths are supported, but I see no benefit to trapping on them. This behavior is at least sensible and doesn’t entail any extra runtime checks.
from swift-numerics.
Yes, I think if someone asks for a Complex number with finite length but non-finite phase, that's a programming error we should detect via a precondition. I'm less worried about negative lengths, since there is a perfectly reasonable answer in that case, it's just maybe not the answer they expect.
from swift-numerics.
Sure, that makes sense.
from swift-numerics.
Nevin, please close unless you think further changes are needed.
from swift-numerics.
Related Issues (20)
- Meaning of pre‐semantic versioning? HOT 7
- Missing required module HOT 7
- Add a script to generate the CMakeLists files? HOT 1
- Feature request for "relaxed" floating-point semantics HOT 2
- Floating point exponent `pow` returns NaN for integer exponents HOT 2
- Possible improvement in 'pow' Elementary Function when operands are zero HOT 5
- Should complex multiplication use augmented arithmetic? HOT 1
- Warnings in `Double.testIntegerExponent()` HOT 1
- [BigInt] Using tests from “Violet - Python VM written in Swift” HOT 5
- Duplicate symbols on Windows
- "^=" xor function code was incorrect. HOT 1
- Improve String init() execution time and fix Violet bugs
- Change default branch from `Quaternions` to `main`
- `** ARCHIVE FAILED **` due to `_NumericsShims` missing HOT 4
- Crash in `DoubleWidth` HOT 4
- Variadic GCD and LCM
- Feature request: GCD for floating-point HOT 7
- Error in Swift Playgrounds HOT 1
- Issues with CMake support HOT 3
- [FR]: Support Apple Privacy Manifest HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swift-numerics.