sqroot

package module
v3.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2025 License: BSD-3-Clause Imports: 14 Imported by: 1

Documentation

Overview

Package sqroot computes square roots and cube roots to arbitrary precision.

Number is the main type in this package. It represents a lazily evaluated non negative real number that generally has an infinite number of digits, but a Number can also have a finite number of digits.

A FiniteNumber works like Number except that it always has a finite number of digits. A *FiniteNumber can be used anywhere a Number type is expected but not the other way around.

A Sequence is a view of a contiguous subset of digits of a Number. For example, A Sequence could represent everything past the 1000th digit of the square root of 3. Because Sequences are views, they are cheap to create. Note that Number and *FiniteNumber can be used anywhere a Sequence type is expected. A Sequence can be either infinite or finite in length.

A FiniteSequence works like Sequence except unlike Sequence, a FiniteSequence is always finite in length. A FiniteSequence can be used anywhere a Sequence is expected, and a *FiniteNumber can be used anywhere a FiniteSequence is expected. However, a Number or Sequence cannot be used where a FiniteSequence is expected because they can have an infinite number of digits. A FiniteSequence must have a finite number of digits.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsString added in v3.8.0

func AsString(s FiniteSequence) string

AsString returns all the digits in s as a string.

Example
// sqrt(3) = 0.1732050807... * 10^1
n := sqroot.Sqrt(3)

fmt.Println(sqroot.AsString(n.WithStart(2).WithEnd(10)))
Output:

32050807

func BackwardMatches added in v3.5.0

func BackwardMatches(s FiniteSequence, pattern []int) iter.Seq[int]

BackwardMatches returns all the 0 based positions in s where pattern is found from last to first.

Example
n := sqroot.Sqrt(2)
count := 0
iterator := sqroot.BackwardMatches(n.WithEnd(1000), []int{1, 4})
for index := range iterator {
	fmt.Println(index)
	count++
	if count == 3 {
		break
	}
}
Output:

945
916
631

func DigitsToString deprecated added in v3.2.0

func DigitsToString(s FiniteSequence) string

Deprecated: Use AsString instead.

func Find deprecated

func Find(s Sequence, pattern []int) func() int

Find returns a function that returns the next zero based index of the match for pattern in s. If s has a finite number of digits and there are no more matches for pattern, the returned function returns -1. Pattern is a sequence of digits between 0 and 9.

Deprecated: Use Matches instead.

func FindAll deprecated

func FindAll(s FiniteSequence, pattern []int) []int

Deprecated: This is equivalent to slices.Collect(sqroot.Matches(s, pattern))

func FindFirst

func FindFirst(s Sequence, pattern []int) int

FindFirst finds the zero based index of the first match of pattern in s. FindFirst returns -1 if pattern is not found only if s has a finite number of digits. If s has an infinite number of digits and pattern is not found, FindFirst will run forever. pattern is a sequence of digits between 0 and 9.

Example
// sqrt(3) = 0.1732050807... * 10^1
n := sqroot.Sqrt(3)

fmt.Println(sqroot.FindFirst(n, []int{0, 5, 0, 8}))
Output:

4

func FindFirstN deprecated

func FindFirstN(s Sequence, pattern []int, n int) []int

Deprecated: Use golang iterators with the github.com/keep94/itertools library. This is equivalent to slices.Collect(itertools.Take(sqroot.Matches(s, pattern), n))

func FindLast

func FindLast(s FiniteSequence, pattern []int) int

FindLast finds the zero based index of the last match of pattern in s. FindLast returns -1 if pattern is not found in s. pattern is a sequence of digits between 0 and 9.

Example
n := sqroot.Sqrt(2)
fmt.Println(sqroot.FindLast(n.WithEnd(1000), []int{1, 4}))
Output:

945

func FindLastN deprecated

func FindLastN(s FiniteSequence, pattern []int, n int) []int

Deprecated: Use golang iterators with the github.com/keep94/itertools library. This is equivalent to slices.Collect(itertools.Take(sqroot.BackwardMatches(s, pattern), n))

func FindR deprecated

func FindR(s FiniteSequence, pattern []int) func() int

FindR returns a function that starts at the end of s and returns the previous zero based index of the match for pattern in s with each call. If there are no more matches for pattern, the returned function returns -1. pattern is a sequence of digits between 0 and 9.

Deprecated: Use BackwardMatches instead.

func Fprint

func Fprint(w io.Writer, s Sequence, p Positions, options ...Option) (
	written int, err error)

Fprint prints digits of s to w. Unless using advanced functionality, prefer Fwrite, Write, and Swrite to Fprint, Print, and Sprint. Fprint returns the number of bytes written and any error encountered. p contains the positions of the digits to print. For options, the default is 50 digits per row, 5 digits per column, show digit count, period (.) for missing digits, don't write a trailing line feed, and show the leading decimal point.

func Fwrite added in v3.3.0

func Fwrite(w io.Writer, s FiniteSequence, options ...Option) (
	written int, err error)

Fwrite writes all the digits of s to w. Fwrite returns the number of bytes written and any error encountered. For options, the default is 50 digits per row, 5 digits per column, show digit count, period (.) for missing digits, write a trailing line feed, and don't show the leading decimal point.

func Matches added in v3.5.0

func Matches(s Sequence, pattern []int) iter.Seq[int]

Matches returns all the 0 based positions in s where pattern is found.

Example
// sqrt(2) = 0.14142135... * 10^1
n := sqroot.Sqrt(2)

// '14' matches at index 0, 2, 144, ...
count := 0
for index := range sqroot.Matches(n, []int{1, 4}) {
	fmt.Println(index)
	count++
	if count == 3 {
		break
	}
}
Output:

0
2
144

func Print

func Print(s Sequence, p Positions, options ...Option) (
	written int, err error)

Print works like Fprint and prints digits of s to stdout.

Example
// Find the square root of 2.
n := sqroot.Sqrt(2)

fmt.Printf("10^%d *\n", n.Exponent())
sqroot.Print(n, sqroot.UpTo(1000))
fmt.Println()
Output:

10^1 *
   0.14142 13562 37309 50488 01688 72420 96980 78569 67187 53769
 50  48073 17667 97379 90732 47846 21070 38850 38753 43276 41572
100  73501 38462 30912 29702 49248 36055 85073 72126 44121 49709
150  99358 31413 22266 59275 05592 75579 99505 01152 78206 05714
200  70109 55997 16059 70274 53459 68620 14728 51741 86408 89198
250  60955 23292 30484 30871 43214 50839 76260 36279 95251 40798
300  96872 53396 54633 18088 29640 62061 52583 52395 05474 57502
350  87759 96172 98355 75220 33753 18570 11354 37460 34084 98847
400  16038 68999 70699 00481 50305 44027 79031 64542 47823 06849
450  29369 18621 58057 84631 11596 66871 30130 15618 56898 72372
500  35288 50926 48612 49497 71542 18334 20428 56860 60146 82472
550  07714 35854 87415 56570 69677 65372 02264 85447 01585 88016
600  20758 47492 26572 26002 08558 44665 21458 39889 39443 70926
650  59180 03113 88246 46815 70826 30100 59485 87040 03186 48034
700  21948 97278 29064 10450 72636 88131 37398 55256 11732 20402
750  45091 22770 02269 41127 57362 72804 95738 10896 75040 18369
800  86836 84507 25799 36472 90607 62996 94138 04756 54823 72899
850  71803 26802 47442 06292 69124 85905 21810 04459 84215 05911
900  20249 44134 17285 31478 10580 36033 71077 30918 28693 14710
950  17111 16839 16581 72688 94197 58716 58215 21282 29518 48847
Example (Positions)
var pb sqroot.PositionsBuilder
sqroot.Print(
	sqroot.Sqrt(2),
	pb.AddRange(110, 120).AddRange(200, 220).Build(),
	sqroot.DigitsPerRow(20),
)
fmt.Println()
Output:

100  ..... ..... 30912 29702
200  70109 55997 16059 70274

func Sprint

func Sprint(s Sequence, p Positions, options ...Option) string

Sprint works like Fprint and prints digits of s to a string.

func Swrite added in v3.3.0

func Swrite(s FiniteSequence, options ...Option) string

Swrite works like Fwrite and writes all the digits of s to returned string.

func Write added in v3.3.0

func Write(s FiniteSequence, options ...Option) (
	written int, err error)

Write works like Fwrite and writes all the digits of s to stdout.

Example
n := sqroot.Sqrt(2)
sqroot.Write(n.WithEnd(1000))
Output:

  0  14142 13562 37309 50488 01688 72420 96980 78569 67187 53769
 50  48073 17667 97379 90732 47846 21070 38850 38753 43276 41572
100  73501 38462 30912 29702 49248 36055 85073 72126 44121 49709
150  99358 31413 22266 59275 05592 75579 99505 01152 78206 05714
200  70109 55997 16059 70274 53459 68620 14728 51741 86408 89198
250  60955 23292 30484 30871 43214 50839 76260 36279 95251 40798
300  96872 53396 54633 18088 29640 62061 52583 52395 05474 57502
350  87759 96172 98355 75220 33753 18570 11354 37460 34084 98847
400  16038 68999 70699 00481 50305 44027 79031 64542 47823 06849
450  29369 18621 58057 84631 11596 66871 30130 15618 56898 72372
500  35288 50926 48612 49497 71542 18334 20428 56860 60146 82472
550  07714 35854 87415 56570 69677 65372 02264 85447 01585 88016
600  20758 47492 26572 26002 08558 44665 21458 39889 39443 70926
650  59180 03113 88246 46815 70826 30100 59485 87040 03186 48034
700  21948 97278 29064 10450 72636 88131 37398 55256 11732 20402
750  45091 22770 02269 41127 57362 72804 95738 10896 75040 18369
800  86836 84507 25799 36472 90607 62996 94138 04756 54823 72899
850  71803 26802 47442 06292 69124 85905 21810 04459 84215 05911
900  20249 44134 17285 31478 10580 36033 71077 30918 28693 14710
950  17111 16839 16581 72688 94197 58716 58215 21282 29518 48847

Types

type Digit deprecated

type Digit struct {

	// The 0 based position of the digit.
	Position int

	// The value of the digit. Always between 0 and 9.
	Value int
}

Digit represents a digit and its zero based position in a mantissa.

Deprecated: Not needed because of golang iterators.

type FiniteNumber

type FiniteNumber struct {
	// contains filtered or unexported fields
}

FiniteNumber is a Number with a finite number of digits. FiniteNumber implements both Number and FiniteSequence. The zero value for FiniteNumber is 0.

Pass FiniteNumber instances by reference not by value. Copying a FiniteNumber instance is not supported and may cause errors.

func NewFiniteNumber added in v3.6.0

func NewFiniteNumber(fixed []int, exponent int) (*FiniteNumber, error)

NewFiniteNumber works like NewNumberForTesting except that it returns a *FiniteNumber instead of a Number. Note that there is no repeating parameter because FiniteNumbers have a finite number of digits.

Example
// n = 563.5
n, _ := sqroot.NewFiniteNumber([]int{5, 6, 3, 5}, 3)

fmt.Println(n.Exact())
Output:

563.5

func (*FiniteNumber) All added in v3.5.0

func (n *FiniteNumber) All() iter.Seq2[int, int]

All comes from the Sequence interface.

Example
// sqrt(7) = 0.26457513110... * 10^1
n := sqroot.Sqrt(7)

for index, value := range n.All() {
	fmt.Println(index, value)
	if index == 5 {
		break
	}
}
Output:

0 2
1 6
2 4
3 5
4 7
5 5

func (*FiniteNumber) At

func (n *FiniteNumber) At(posit int) int

At comes from the Number interface.

Example
// sqrt(7) = 0.264575131106459...*10^1
n := sqroot.Sqrt(7)

fmt.Println(n.At(0))
fmt.Println(n.At(1))
fmt.Println(n.At(2))
Output:

2
6
4

func (*FiniteNumber) Backward added in v3.5.0

func (n *FiniteNumber) Backward() iter.Seq2[int, int]

Backward comes from the FiniteSequence interface.

Example
// sqrt(7) = 0.26457513110... * 10^1
n := sqroot.Sqrt(7).WithSignificant(6)

for index, value := range n.Backward() {
	fmt.Println(index, value)
}
Output:

5 5
4 7
3 5
2 4
1 6
0 2

func (*FiniteNumber) Exact added in v3.4.0

func (n *FiniteNumber) Exact() string

Exact works like String, but uses enough significant digits to return the exact representation of n.

Example
n := sqroot.Sqrt(2).WithSignificant(60)
fmt.Println(n.Exact())
Output:

1.41421356237309504880168872420969807856967187537694807317667

func (*FiniteNumber) Exponent

func (n *FiniteNumber) Exponent() int

Exponent comes from the Number interface.

Example
// sqrt(50176) = 0.224 * 10^3
n := sqroot.Sqrt(50176)

fmt.Println(n.Exponent())
Output:

3

func (*FiniteNumber) FiniteWithStart

func (n *FiniteNumber) FiniteWithStart(start int) FiniteSequence

FiniteWithStart comes from the FiniteSequence interface.

func (*FiniteNumber) Format

func (n *FiniteNumber) Format(state fmt.State, verb rune)

Format comes from the Number interface.

func (*FiniteNumber) IsZero

func (n *FiniteNumber) IsZero() bool

IsZero comes from the Number interface.

func (*FiniteNumber) Iterator deprecated

func (n *FiniteNumber) Iterator() func() (Digit, bool)

Iterator comes from the Sequence interface.

Deprecated: Use All instead.

func (*FiniteNumber) Reverse deprecated

func (n *FiniteNumber) Reverse() func() (Digit, bool)

Reverse comes from the FiniteSequence interface.

Deprecated: Use Backward instead.

func (*FiniteNumber) String

func (n *FiniteNumber) String() string

String comes from the Number interface.

func (*FiniteNumber) Values added in v3.7.0

func (n *FiniteNumber) Values() iter.Seq[int]

Values comes from the Sequence interface.

Example
// sqrt(7) = 0.26457513110... * 10^1
n := sqroot.Sqrt(7)

for value := range n.WithEnd(6).Values() {
	fmt.Println(value)
}
Output:

2
6
4
5
7
5

func (*FiniteNumber) WithEnd

func (n *FiniteNumber) WithEnd(end int) FiniteSequence

WithEnd comes from the Sequence interface.

func (*FiniteNumber) WithSignificant

func (n *FiniteNumber) WithSignificant(limit int) *FiniteNumber

WithSignificant comes from the Number interface.

Example
// n is 1.42857142857... but truncated to 10000 significant digits
n := sqroot.SqrtRat(100, 49).WithSignificant(10000)

// Instead of running forever, FindFirst returns -1 because n is truncated.
fmt.Println(sqroot.FindFirst(n, []int{1, 1, 2}))
Output:

-1

func (*FiniteNumber) WithStart

func (n *FiniteNumber) WithStart(start int) Sequence

WithStart comes from the Sequence interface.

Example
// sqrt(29) = 5.3851648...
n := sqroot.Sqrt(29)

// Find all occurrences of '85' in the first 1000 digits of sqrt(29)
matches := sqroot.Matches(n.WithEnd(1000), []int{8, 5})
fmt.Println(slices.Collect(matches))

// Find all occurrences of '85' in the first 1000 digits of sqrt(29)
// on or after position 800
matches = sqroot.Matches(n.WithStart(800).WithEnd(1000), []int{8, 5})
fmt.Println(slices.Collect(matches))
Output:

[2 167 444 507 511 767 853 917 935 958]
[853 917 935 958]

type FiniteSequence

type FiniteSequence interface {
	Sequence

	// Backward returns the 0 based position and value of each digit in this
	// FiniteSequence from end to beginning.
	Backward() iter.Seq2[int, int]

	// Reverse returns a function that generates the digits in this
	// FiniteSequence along with their zero based positions from end to
	// beginning. When there are no more digits, returned function
	// returns false.
	//
	// Deprecated: Use Backward instead.
	Reverse() func() (Digit, bool)

	// FiniteWithStart works like WithStart except that it returns a
	// FiniteSequence.
	FiniteWithStart(start int) FiniteSequence
}

FiniteSequence represents a Sequence of finite length.

type Generator added in v3.1.0

type Generator interface {

	// Generate returns the digits of the mantissa and the exponent for a
	// Number. Numbers are of the form mantissa*10^exp where mantissa is
	// is between 0.1 inclusive and 1.0 exclusive. Calling digits() returns
	// each digit of the mantissa in turn. The first call to digits() cannot
	// return 0 because of the range of values a mantissa can have. If a
	// call to digits() returns -1, this means that there are no more digits
	// in the mantissa. If the first call to digits() return -1, that means
	// that the resulting Number is zero. Once a call to digits() returns -1,
	// all successive calls to digits() must also return -1. digits() must
	// return values between 0 and 9 or -1.
	Generate() (digits func() int, exp int)
}

Interface Generator lazily generates the digits of a Number.

type Number

type Number interface {
	Sequence

	// At returns the significant digit of this Number at the given 0 based
	// position. If this Number has posit or fewer significant digits, At
	// returns -1. If posit is negative, At returns -1.
	At(posit int) int

	// WithSignificant returns a view of this Number that has no more than
	// limit significant digits. WithSignificant rounds the returned value
	// down toward zero. WithSignificant panics if limit is negative.
	WithSignificant(limit int) *FiniteNumber

	// Exponent returns the exponent of this Number.
	Exponent() int

	// Format prints this Number with the f, F, g, G, e, E verbs. The
	// verbs work in the usual way except that they always round down.
	// Because Number can have an infinite number of digits, g with no
	// precision shows a max of 16 significant digits. Format supports
	// width, precision, and the '-' flag for left justification. The v
	// verb is an alias for g.
	Format(state fmt.State, verb rune)

	// String returns the decimal representation of this Number using %g.
	String() string

	// IsZero returns true if this Number is zero.
	IsZero() bool
	// contains filtered or unexported methods
}

Number is a reference to a non-negative real number. A non-zero Number is of the form mantissa * 10^exponent where mantissa is between 0.1 inclusive and 1.0 exclusive. A Number can represent either a finite or infinite number of digits. A Number computes the digits of its mantissa lazily on an as needed basis. To compute a given digit, a Number must compute all digits that come before that digit. A Number stores its computed digits so that they only have to be computed once. Number instances are safe to use with multiple goroutines.

The Number factory functions such as the Sqrt and CubeRoot functions return new Number instances that contain no computed digits initially.

Because Number instances store their computed digits, it is best to reuse a Number instance when possible. For example the code:

n := sqroot.Sqrt(6)
fmt.Println(sqroot.FindFirst(n, []int{0, 0, 0, 0, 0}))
fmt.Println(sqroot.FindFirst(n, []int{2, 2, 2, 2, 2}))

runs faster than the code:

fmt.Println(sqroot.FindFirst(sqroot.Sqrt(6), []int{0, 0, 0, 0, 0}))
fmt.Println(sqroot.FindFirst(sqroot.Sqrt(6), []int{2, 2, 2, 2, 2}))

In the first code block, the second line reuses digits computed in the first line, but in the second code block, no reuse is possible since sqroot.Sqrt(6) always returns a Number with no precomputed digits.

A Number can be 0, in which case IsZero() returns true. Zero Numbers have an exponent of 0 and no digits in their mantissa. This means that calling At() on a zero Number always returns -1. Likewise calling All() or Values() on a zero Number returns an empty iterator. However, calling String() on a zero Number returns "0" and printing a zero Number prints 0 according to the format specification used.

func CubeRoot

func CubeRoot(radican int64) Number

CubeRoot returns the cube root of radican. CubeRoot panics if radican is negative as Number can only hold positive results.

Example
// Print the cube root of 3 with 100 significant digits.
fmt.Printf("%.100g\n", sqroot.CubeRoot(3))
Output:

1.442249570307408382321638310780109588391869253499350577546416194541687596829997339854755479705645256

func CubeRootBigInt

func CubeRootBigInt(radican *big.Int) Number

CubeRootBigInt returns the cube root of radican. CubeRootBigInt panics if radican is negative as Number can only hold positive results.

func CubeRootBigRat

func CubeRootBigRat(radican *big.Rat) Number

CubeRootBigRat returns the cube root of radican. Because Number can only hold positive results, the denominator of radican must be positive, and the numerator must be non-negative or else CubeRootBigRat panics.

func CubeRootRat

func CubeRootRat(num, denom int64) Number

CubeRootRat returns the cube root of num / denom. Because Number can only hold positive results, denom must be positive, and num must be non-negative or else CubeRootRat panics.

func NewNumber added in v3.1.0

func NewNumber(g Generator) Number

NewNumber returns a new Number based on g. Although g is expected to follow the contract of Generator, if g yields mantissa digits outside the range of 0 and 9, NewNumber regards that as a signal that there are no more mantissa digits. Also if g happens to yield 0 as the first digit of the mantissa, NewNumber will return zero.

func NewNumberForTesting added in v3.1.0

func NewNumberForTesting(fixed, repeating []int, exp int) (Number, error)

NewNumberForTesting creates an arbitrary Number for testing. fixed are digits between 0 and 9 representing the non repeating digits that come immediately after the decimal place of the mantissa. repeating are digits between 0 and 9 representing the repeating digits that follow the non repeating digits of the mantissa. exp is the exponent part of the returned Number. NewNumberForTesting returns an error if fixed or repeating contain values not between 0 and 9, or if the first digit of the mantissa would be zero since mantissas must be between 0.1 inclusive and 1.0 exclusive.

Example
// n = 10.2003400340034...
n, _ := sqroot.NewNumberForTesting([]int{1, 0, 2}, []int{0, 0, 3, 4}, 2)

fmt.Println(n)
Output:

10.20034003400340

func NewNumberFromBigRat deprecated

func NewNumberFromBigRat(value *big.Rat) Number

NewNumberFromBigRat returns value as a Number. Because Number can only hold positive results, the denominator of value must be positive, and the numerator must be non-negative or else NewNumberFromBigRat panics. NewNumberFromBigRat can be used to create arbitrary Number instances for testing.

Deprecated: Use NewNumberForTesting instead.

func Sqrt

func Sqrt(radican int64) Number

Sqrt returns the square root of radican. Sqrt panics if radican is negative.

Example
// Print the square root of 13 with 100 significant digits.
fmt.Printf("%.100g\n", sqroot.Sqrt(13))
Output:

3.605551275463989293119221267470495946251296573845246212710453056227166948293010445204619082018490717

func SqrtBigInt

func SqrtBigInt(radican *big.Int) Number

SqrtBigInt returns the square root of radican. SqrtBigInt panics if radican is negative.

func SqrtBigRat

func SqrtBigRat(radican *big.Rat) Number

SqrtBigRat returns the square root of radican. The denominator of radican must be positive, and the numerator must be non-negative or else SqrtBigRat panics.

func SqrtRat

func SqrtRat(num, denom int64) Number

SqrtRat returns the square root of num / denom. denom must be positive, and num must be non-negative or else SqrtRat panics.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option represents an option for the Print, Fprint, and Sprint methods

func DigitsPerColumn

func DigitsPerColumn(count int) Option

DigitsPerColumn sets the number of digits per column. Zero or negative means no separate columns.

func DigitsPerRow

func DigitsPerRow(count int) Option

DigitsPerRow sets the number of digits per row. Zero or negative means no separate rows.

func LeadingDecimal added in v3.3.0

func LeadingDecimal(on bool) Option

LeadingDecimal prints "0." before the first digit if on is true.

func MissingDigit

func MissingDigit(missingDigit rune) Option

MissingDigit sets the character to represent a missing digit.

func ShowCount

func ShowCount(on bool) Option

ShowCount shows the digit count in the left margin if on is true.

func TrailingLF added in v3.3.0

func TrailingLF(on bool) Option

TrailingLF adds a trailing line feed to what is printed if on is true.

type PositionRange

type PositionRange struct {

	// The zero based starting position inclusive.
	Start int

	// The zero based ending position exclusive.
	End int
}

PositionRange is a single range of positions within a Positions instance.

type Positions

type Positions struct {
	// contains filtered or unexported fields
}

Positions represents a set of zero based positions for which to fetch digits. The zero value contains no positions.

Example
var builder sqroot.PositionsBuilder
builder.AddRange(0, 7).AddRange(40, 50)
positions := builder.AddRange(5, 10).Build()
fmt.Printf("End: %d\n", positions.End())
for pr := range positions.All() {
	fmt.Printf("%+v\n", pr)
}
Output:

End: 50
{Start:0 End:10}
{Start:40 End:50}

func Between

func Between(start, end int) Positions

Between returns the positions from start up to but not including end.

func UpTo

func UpTo(end int) Positions

UpTo returns the positions from 0 up to but not including end.

func (Positions) All added in v3.5.0

func (p Positions) All() iter.Seq[PositionRange]

All returns all the non overlapping ranges of positions in p.

func (Positions) End

func (p Positions) End() int

End returns the last zero based position in p plus 1. If p is the zero value, End returns 0.

func (Positions) Ranges deprecated

func (p Positions) Ranges() func() (pr PositionRange, ok bool)

Ranges returns a function that generates all the non overlapping ranges of positions in p. The returned function generates all the ranges in increasing order and returns false when there are no more.

Deprecated: Use All() instead.

type PositionsBuilder

type PositionsBuilder struct {
	// contains filtered or unexported fields
}

PositionsBuilder builds Positions objects. The zero value has no positions in it and is ready to use. Do not copy a PositionsBuilder instance.

func (*PositionsBuilder) Add

func (p *PositionsBuilder) Add(posit int) *PositionsBuilder

Add adds posit to this instance and returns this instance for chaining. If posit is negative, Add is a no-op.

func (*PositionsBuilder) AddRange

func (p *PositionsBuilder) AddRange(start, end int) *PositionsBuilder

AddRange adds a range of positions to this instance and returns this instance for chaining. The range is between start inclusive and end exclusive. AddRange ignores any negative positions within the specified range. If end <= start, AddRange is a no-op.

func (*PositionsBuilder) Build

func (p *PositionsBuilder) Build() Positions

Build builds a Positions instance from this builder and resets this builder so that it has no positions in it.

type Sequence

type Sequence interface {

	// All returns the 0 based position and value of each digit in this
	// Sequence from beginning to end.
	All() iter.Seq2[int, int]

	// Values returns the value of each digit in this Sequence from
	// beginning to end.
	Values() iter.Seq[int]

	// Iterator returns a function that generates the digits in this
	// Sequence along with their zero based positions from beginning to end.
	// If there are no more digits, returned function returns false.
	//
	// Deprecated: Use All instead.
	Iterator() func() (Digit, bool)

	// WithStart returns a view of this Sequence that only has digits with
	// zero based positions greater than or equal to start.
	WithStart(start int) Sequence

	// WithEnd returns a view of this Sequence that only has digits with
	// zero based positions less than end.
	WithEnd(end int) FiniteSequence
	// contains filtered or unexported methods
}

Sequence represents a sequence of digits of either finite or infinite length within the mantissa of a real number. Although they can start and optionally end anywhere within a mantissa, Sequences must be contiguous. That is they can have no gaps in the middle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL