Show HN: I made a calculator that works over disjoint sets of intervals

(victorpoughon.github.io)

140 points | by fouronnes3 8 hours ago ago

22 comments

  • fouronnes3 7 hours ago

    Author here. Outward rounding to combat precision issues is what interval arithmetic is most known for (try 0.1+0.2 with "full precision mode" enabled), but that's really a shame in my opinion. Outward rounding is cool, but the "inclusion property", as it's known in research papers, works at every scale! This is what enables things like:

         50 * (10 + [-1, 1])
        [450, 550]
    
    which is lovely, I think. Adding the union layer to it enables even cooler things, like the true inverse of the square function. Did you know it's not sqrt? Try 'sqinv(64)'.

    I made interval calculator actually mostly as a way to test my implementation of interval union arithmetic [0], which I needed for another project: a backwards updating spreadsheet [1][2].

    [0] https://github.com/victorpoughon/not-so-float

    [1] https://victorpoughon.github.io/bidicalc/

    [2] https://news.ycombinator.com/item?id=46234734

  • akst 19 minutes ago

    Very cool! I don't entirely understand some of the operations, but for what I do understand its pretty neat.

    I wish in classes we were introduced to a notion of arithmetic on intervals as it comes up. Like in basic statistics with confidence intervals there's ±, as well as in the quadratic equation. It found some what dissatisfying we couldn't chain the resulting a series of operations and instead repeat the operations for the 2 seperate values of the ±. I get a teacher would rather not get hung up on this because they want to bring it back to the application generally, like solving a more complicated equation or hypothesis testing in basic stats. I just wish they hinted at the idea we can do arithmetic on these kinds of things more generally.

    I realise what you've got here is well beyond this, but seeing this was some level of validation that treating the interval as a piece of data with its own behaviour of certain operations does make some sense.

  • iamwil 4 hours ago

    This is great. You might be interested in Matt Keeter's work on Implicit surfaces, and using interval math for its optimization:

    https://youtu.be/UxGxsGnbyJ4?si=Oo6Lmc4ACaSr5Dk6&t=1006

  • memalign 4 hours ago

    You might be interested in this graphing calculator I made using interval arithmetic:

    https://memalign.github.io/m/formulagraph/index.html

    Some detail on how this works, including links to the relevant interval math code:

    https://memalign.github.io/p/formulagraph.html

  • _Microft 3 hours ago

    Very nice, thanks for sharing! Maybe show which upper or lower values are included in the intervals? A notation I am familiar with uses outward facing brackets if the value is not included in the interval. That always applies to infinity.

    Applied to the cases here:

    ]-āˆž, -1] U [0.5, +āˆž[

    The excluded interval in between becomes ]-1, 0.5[ then.

    That’s how min (and analogously max) works, right? min(A, B) = [lo(A,B), lo (hi(A), hi(B))].

    Edit: idea: copy a formula from the results section to the input field if the user clicks/taps on it.

    • adito 2 hours ago

      From reading the linked paper[0], It explains closed interval only. "An interval union is a set of closed and disjoint intervals where the bounds of the extreme interval can be Ā±āˆž".

      [0]: https://www.ime.usp.br/~montanhe/unions.pdf

    • fouronnes3 2 hours ago

      It's possible to support that but it makes the code very very much more complicated. I've decided early on to not support it. Would be a cool addition though!

    • globular-toast 3 hours ago

      I was also a bit confused by this. I thought the standard notation was round brackets, but maybe doesn't work well in ASCII?

      • qbit42 2 hours ago

        Round brackets are standard in the US but that notation is used in France and some other places.

      • meindnoch an hour ago

          (0, 1)
        
        Is this an twice-open interval or a 2D vector?

        See, this is why Bourbaki introduced the ]0,1[ notation.

  • dnnddidiej 31 minutes ago

    Interals can be used to model errors and uncertainty and this lets you see how they conpound in calculations like speed = distance over time.

  • JSR_FDED 2 hours ago

    I just read up on interval arithmetic. I understand its desirable properties. Where in practice have you applied it? What’s a real world application for interval arithmetic?

    • ngruhn 2 hours ago

      It can be used in static analysis or type checking. E.g.

          if (x >= 0) {
            x += 10
            if (x =< 9) {
              // unreachable 
            }
          }
      
      By maintaining an interval of possible values of x, you can detect the unreachable branch, because the interval becomes empty:

          initial: [-oo, oo]
          x >= 0 : [0, oo]
          x += 10: [10, oo]
          x =< 9 : [10, 9] (empty)
    • nickcw 36 minutes ago

      In physics, whenever you make a measurement it has a precision. Usually you represent this as a normal distribution, but for calculations it can be easier to represent this as an interval.

      The police measure the distance my car travelled [ 99.9, 100.1 ] m and the time it took [ 3.3, 3.4 ] s - how fast was my car going? [29.38, 30.33] m/s according to the interval calculator.

      Physics students learn exactly this method before they move on to more sophisticated analysis with error distributions.

    • nicolodev an hour ago

      It’s astonishing how nobody hasn’t mentioned abstract interpretation yet. Under classical static analysis, if you can ā€œproveā€ that a variable does not have values in some unsound zones, you can e.g. ā€œproveā€ soundness or apply further optimizations.

      The interval abstract domain works under interval analysis with an algebra that’s the same of this calculator. It’s funny to implement something like that on source/binary level :)

  • anematode 2 hours ago

    Excellent!! I love interval arithmetic and also wrote a TS implementation for a graphing calculator project. Agree that it's very underrated, and I wish that directed rounding was exposed in more languages.

    • fouronnes3 2 hours ago

      Yeah it's super interesting. Like you said, I learned that the IEEE 754 spec actually requires that complete implementations of floating point numbers expose a way to programmatically choose the rounding mode. As far as I know only C allows you to do that, and even then it depends on hardware support. For JS I had to use ugly typedarray casts. Which kinda only accidentally work due to endianess. But technically there should be an API for it!

      There's other unused stuff in IEEE 754 like that: the inexact bit or signaling NaNs!

  • teiferer 2 hours ago

    The last point in your intro description can't be stressed enough: this allows for safe handling of rounding errors in floating point operations.

    Though you are inherently losing precision: there are values in the output interval which don't have a corresponding input that causes this output.

  • petters 2 hours ago

    You could add a feature where it will compute the global optimum of any function of a small number of variables. Branch and bound with interval arithmetic works well for a small number of variables.

    Disjoint unions of intervals seems like a nice thing to have

  • boobsbr 2 hours ago

    Neat.

  • LXforever 40 minutes ago

    Very cool. This feels like one of those ideas that makes interval arithmetic go from ā€œinteresting but frustratingā€ to actually useful. I’d be curious how you handle the growth in the number of disjoint intervals over repeated operations, since that seems like the practical bottleneck.

    • fouronnes3 14 minutes ago

      I don't handle it, ahah. You are right that if you take any classical numerical computing algorithm and replace the floating point reals by interval unions, most of the time the number of intervals in the unions in each of your variables will grow very fast. This is one of the problems of unions and as far as I'm aware it's a topic of active academic research.