Have lib/interval use [m ..= n] syntax

This commit is contained in:
Nigel Tao committed 2019-10-30 16:43:52 +11:00
1 parent 645d17fb20
commit de807374d2
2 files changed
+21 -19

No files matched your search

+15 -13
View File
@@ -17,17 +17,19 @@
// Package interval provides interval arithmetic on big integers. Big means
// arbitrary-precision, as per the standard math/big package.
//
// For example, if x is in the interval [3, 6] and y is in the interval [10,
// 15] then x+y is in the interval [13, 21].
// For example, if x is in the interval [3 ..= 6] and y is in the interval [10
// ..= 15] then x+y is in the interval [13 ..= 21].
//
// Such intervals may have infinite bounds. For example, if x is in the
// interval [3, +∞) and y is in the interval [-4, -2], then x*y is in the
// interval (-∞, -6].
// As in Rust, the [m ..= n] syntax means all integers i such that (m <= i) and
// (i <= n). Unlike Rust, neither bound can be omitted, but they may be
// infinite. For example, if x is in the interval [3 ..= +∞] and y is in the
// interval [-4 ..= -2], then x*y is in the interval [-∞ ..= -6].
//
// As a motivating example, if a compiler knows that the integer-typed
// variables i and j are in the intervals [0, 255] and [0, 3], and that the
// array a has 1024 elements, then it can prove that the array-index expression
// a[4*i + j] is memory-safe without needing an at-runtime bounds check.
// variables i and j are in the intervals [0 ..= 255] and [0 ..= 3], and that
// the array a has 1024 elements, then it can prove that the array-index
// expression a[4*i + j] is memory-safe without needing an at-runtime bounds
// check.
//
// This package depends only on the standard math/big package.
package interval
@@ -146,19 +148,19 @@ type IntRange [2]*big.Int
// String returns a string representation of x.
func (x IntRange) String() string {
if x.Empty() {
return "<empty>"
return "[empty]"
}
buf := []byte(nil)
if x[0] == nil {
buf = append(buf, "(-∞"...)
buf = append(buf, "[-∞ ..= "...)
} else {
buf = append(buf, '[')
buf = x[0].Append(buf, 10)
buf = append(buf, " ..= "...)
}
if x[1] == nil {
buf = append(buf, " .. +∞)"...)
buf = append(buf, "+∞]"...)
} else {
buf = append(buf, " ..= "...)
buf = x[1].Append(buf, 10)
buf = append(buf, ']')
}
@@ -243,7 +245,7 @@ func (x IntRange) Empty() bool {
return x[0] != nil && x[1] != nil && x[0].Cmp(x[1]) > 0
}
// justZero returns whether x is the [0, 0] interval, containing exactly one
// justZero returns whether x is the [0 ..= 0] interval, containing exactly one
// element: the integer zero.
func (x IntRange) justZero() bool {
return x[0] != nil && x[1] != nil && x[0].Sign() == 0 && x[1].Sign() == 0
+6 -6
View File
@@ -36,14 +36,14 @@ package interval
// - radialOutput has a radius R of 16 << 16 (which equals 1048576).
//
// If x and y are "small" radialInput values or one of the two "smallest large"
// radialInput values, i.e. x and y are in the range [-16, +16], then (x op y)
// will always be a "small" radialOutput value, for the common binary
// radialInput values, i.e. x and y are in the range [-16 ..= +16], then (x op
// y) will always be a "small" radialOutput value, for the common binary
// operators: add, subtract, multiply, divide, left-shift, right-shift, and,
// or.
//
// Both of these radialInput and radialOutput types are encoded as an int32:
// - math.MinInt32 (which equals -1 << 31) encodes a NaN.
// - "small" numbers (within the interval [-R, +R]) encode themselves.
// - "small" numbers (within the interval [-R ..= +R]) encode themselves.
// - any other negative int32 encodes "less than -R".
// - any other positive int32 encodes "greater than +R".
//
@@ -51,10 +51,10 @@ package interval
// integers greater than +15.
//
// Binary operators take two radialInput values and produce a pair of
// radialOutput values: either a [min, max] interval, or [NaN, NaN]. For
// radialOutput values: either a [min ..= max] interval, or [NaN ..= NaN]. For
// example, adding the "-3" box to the "greater than +15" box would produce the
// radialOutPair ["13", "greater than +16 << 16"], or in more conventional
// notation, the half-open interval [13, +∞).
// radialOutPair ["13" ..= "greater than +16 << 16"], or in more conventional
// notation, the half-open interval [13 ..= +∞].
//
// These radial number types are not exported by package interval, as the
// radius values (15 and 16 << 16) are somewhat arbitrary and not so generally