Giter Site home page Giter Site logo

Comments (10)

stephentyrone avatar stephentyrone commented on May 19, 2024 1

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.

NevinBR avatar NevinBR commented on May 19, 2024 1

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.

stephentyrone avatar stephentyrone commented on May 19, 2024

.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.

NevinBR avatar NevinBR commented on May 19, 2024

.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.

stephentyrone avatar stephentyrone commented on May 19, 2024

(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.

stephentyrone avatar stephentyrone commented on May 19, 2024

Yeah, the only downside is that it makes the behavior slightly less easy to explain. I'll sleep on it.

from swift-numerics.

NevinBR avatar NevinBR commented on May 19, 2024

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.

stephentyrone avatar stephentyrone commented on May 19, 2024

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.

NevinBR avatar NevinBR commented on May 19, 2024

Sure, that makes sense.

from swift-numerics.

stephentyrone avatar stephentyrone commented on May 19, 2024

Nevin, please close unless you think further changes are needed.

from swift-numerics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.