# How to create an Interval object with specific end points?

When creating an Interval object, the end points are automatically widened when an end point is an inexact number. For instance:

Interval[{.22, 1.22}] //InputForm


Interval[{0.196093751.9914337561691888, 1.2156252.0056183769581764}]

How can I create an Interval object and avoid this automatic widening? That is, can a function PreciseInterval be defined such that:

PreciseInterval[{.22, 1.22}] //FullForm


gives

Interval[{.22, 1.22}]

• So when are you going to tell us the answer ...? :-) Sep 23, 2018 at 18:19

I will present two solutions, a hard (and not quite complete) one and an easy (but undocumented) one. First, the hard one.

1. Use Interval[real] to determine the default widening.

Consider:

r = 1.232;
(int = Interval[r]) //InputForm


Interval[{1.2143751.9944477065044612, 1.2456252.0054822046051672}]

Note how an Interval object is created that includes the input r. Now, for the key observation: Repeat the above procedure with the end points:

{left, right} = First @ int;
Interval[left] //InputForm
Interval[right] //InputForm


Interval[{1.198751.988823508739323, 1.232.}]

Interval[{1.232., 1.261252.0108960678055694}]

Notice how the original input r reappears. So, the basic algorithm to create the desired output is:

PreciseInterval[{l_, r_}] := Interval[{Interval[l][[1,2]], Interval[r][[1,1]]}]


For example:

PreciseInterval[{1.23, 3.35}] //InputForm


Interval[{1.23., 3.35.}]

The only problem is that this algorithm doesn't work when the end point generated by Interval is an arbitrary precision 0. For example:

Interval[.1.2] //InputForm


Interval[{01.2, 0.2250.5521825181113625}]

In this case, PreciseInterval gives:

PreciseInterval[{-1, .1.2}] //InputForm


Interval[{-1, 0.10000000000000000560.4259697322722812}]

which isn't bad. It is possible to do slightly better, but instead, let's turn to the easy method.

1. The easy (but undocumented) option

The issue would be moot if one could figure out a way to create an "evaluated" Interval object without having the Interval object actually evaluate. And, there is a way to do this! The needed function is SystemPrivateHoldSetValid. The function SystemPrivateHoldSetValid tells Mathematica that its argument has already been evaluated, so Mathematica need not try to evaluate it again. Using this function:

Clear[PreciseInterval]
PreciseInterval[{a_, b_}] := SystemPrivateHoldSetValid[Interval[{a, b}]]


Let's check the previous example:

PreciseInterval[{-1, .1.2}]//InputForm


Interval[{-1, 0.10.2}]

• HoldSetValid[] is neat! On the default widening for floats: this is basically determining the nearest floating point numbers that straddle the input float; I exploited that property for this answer. Sep 25, 2018 at 3:48
• I'd seen SetValid, ValidQ, and friends before but never realized they were this powerful. That is very useful. How old are they? (10.0 or earlier?) Sep 25, 2018 at 22:13
• What functions make use of Valid? I've been struggling to find other objects for which this works. Sep 26, 2018 at 8:22
• It is worth to include this useful function into this answer: mathematica.stackexchange.com/a/139974/280 Oct 22, 2018 at 7:14

Given the discrete nature of numbers, there are probably edge cases where the problem is unsolvable. Here's a start:

ClearAll[preciseInterval];
preciseInterval[int_] := Module[{obj, res, x, y, px, py},
obj[x_?NumericQ, y_?NumericQ, px_?NumericQ, py_?NumericQ, wp_] :=
Block[{(*$$MaxPrecision,$$MinPrecision*)},
SetPrecision[
Flatten@{#, Precision /@ #} &@
First@Interval[{SetPrecision[x, px], SetPrecision[y, py]}],
wp]
];
With[{prec = 2 Max[Precision[int], $MachinePrecision]}, res = FindRoot[ obj[x, y, px, py, prec] == SetPrecision[Flatten@{int, Precision /@ int}, prec], {{x, First@int, (1 + 5 10^-Precision[int]) First@int}, {y, Last@int, (1 + 5 10^-Precision[int]) Last@int}, {px, Precision@First@int, (1 + 5 10^-Precision[int]) Precision@First@int}, {py, Precision@Last@int, (1 + 5 10^-Precision[int]) Precision@Last@int}}, WorkingPrecision -> prec ] ]; (Interval[Quiet@{SetPrecision[x, px], SetPrecision[y, py]}] /. res) /; FreeQ[res, FindRoot] ]; preciseInterval[{0.22., 1.22.}] // InputForm (* Interval[{0.22., 1.22.}] *)  There are four parameters to solve for, the two numbers and their precisions. The above does not work for infinite-precision numbers nor, regrettably, zero. I wasn't sure whether FindRoot uses MaxPrecision,$MinPrecision and I would need to protect the obj[] function. Apparently it's unnecessary to block them, but I left it in, just in case.

• Thanks, this is a nice answer. Sep 25, 2018 at 3:33

I'm not sure what you are trying to achieve here - this doesn't look very useful to me. Mathematica gives us two ways of expressing the uncertainty in a number, either by giving its precision or by giving an interval in which it lies: you are trying to use both at once.

By specifying a precision for the limits of the interval, you are saying that you are uncertain about exactly what those limits are. From the help for Interval`:

In operations on intervals that involve approximate numbers, the Wolfram Language always rounds lower limits down and upper limits up.

In this way, Mathematica attempts to explain all the uncertainty in the interval, rather than the precision of the limits.