numprint

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

README

numprint

A package to pretty print sequences of digits.

While this package is meant to work with the data structures github.com/keep94/sqrt, it will work with anything that implements the required interfaces.

Examples

package main

import (
    "github.com/keep94/numprint"
    "github.com/keep94/sqrt"
)

func main() {

    // Pretty print the first 1000 digits of the square root of 2.
    numprint.Write(sqrt.Sqrt(2).WithEnd(1000))
}

Which outputs:

  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

More documentation and examples can be found here.

Documentation

Overview

Package numprint pretty prints sequences of digits.

For the github.com/keep94/sqrt package, sqrt.Sequence implements Printable and sqrt.FiniteSequence implements both Writable and Printable.

While this package is meant to be used with the data structures in the github.com/keep94/sqrt package, it will work with anything that implements the Printable or Writable interface.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprint

func Fprint(w io.Writer, s Printable, 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

func Fwrite(w io.Writer, s Writable, 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 Print

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

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

Example
package main

import (
	"fmt"

	"github.com/keep94/numprint"
	"github.com/keep94/sqrt"
)

func main() {

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

	fmt.Printf("10^%d *\n", n.Exponent())
	numprint.Print(n, numprint.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)
package main

import (
	"fmt"

	"github.com/keep94/numprint"
	"github.com/keep94/sqrt"
)

func main() {
	var pb numprint.PositionsBuilder
	numprint.Print(
		sqrt.Sqrt(2),
		pb.AddRange(110, 120).AddRange(1000, 1020).Build(),
		numprint.DigitsPerRow(20),
	)
	fmt.Println()
}
Output:

 100  ..... ..... 30912 29702
1000  20896 94633 86289 15628

func Sprint

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

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

func Swrite

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

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

func Write

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

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

Example
package main

import (
	"github.com/keep94/numprint"
	"github.com/keep94/sqrt"
)

func main() {
	n := sqrt.Sqrt(2)
	numprint.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 Option

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

Option represents an option for printing.

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

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

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
package main

import (
	"fmt"

	"github.com/keep94/numprint"
)

func main() {
	var builder numprint.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

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.

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 Printable

type Printable interface {

	// AllInRange returns the 0 based position and value of each digit in
	// this Printable from position start up to but not including position
	// end.
	AllInRange(start, end int) iter.Seq2[int, int]
}

Printable represents a sequence of digits between 0-9 with contiguous positions that can be printed with Print(), Fprint(), or Sprint().

type Writable

type Writable interface {

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

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

Writable represents a sequence of digits between 0-9 with contiguous positions that can be printed with Write(), Fwrite(), or Swrite().

Jump to

Keyboard shortcuts

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