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