spigot(1)
spigot(1) π by Simon Tatham
okay bestie, welcome to the exact real calculator that literally never stops giving digits unless you interrupt it. no cap.
β¨ NAME β¨
spigot β command-line exact real calculator π
πΈ SYNOPSIS πΈ
spigot [ options ] expression
π¦ DESCRIPTION π¦
okay so spigot is an exact real calculator: you give it a mathematical expression to evaluate, and it computes it to any desired precision. by default it just prints digits to standard output until you interrupt it. yes, forever. it's giving infinite main character energy π
spigot provides command-line options to control the format of the output, restrict it to a specified number of digits, and apply rounding at the end of those digits. It can produce output in any base between 2 and 36 (after that it runs out of digit characters), or as a continued fraction, and it can read input numbers from files in any of those formats as well. slay.
This man page gives only a brief summary of spigot's functionality. For full detail, you should read the main manual spigot.html; if that is not installed on your system, you can find it on the web at
http://www.chiark.greenend.org.uk/~sgtatham/spigot/spigot.html
π OPTIONS π
The following options control spigot's basic output format:
-bbase,-Bbase β Output the number in base base, which must be an integer between 2 and 36 inclusive. Digits above 9 are represented by lower case or upper case letters, for the options-band-Brespectively. The default is-b 10.-cβ Output the number as a list of continued fraction coefficients, as decimal integers, by default one per line.-Cβ Output the number's continued fraction convergents, one per line, in the form of two decimal integers with a/between them.-Rβ Output the number's value as a rational, in the form of two decimal integers with a/between them, or just one decimal integer if the number is a rational. If spigot does not know the number to be rational immediately, it will start evaluating it to see if it turns out rational later, so if it is not rational then spigot will compute for ever. π fr fr.-Sβ Output the number as a hex representation of an IEEE 754 bit pattern, in 32-bit single precision. If that representation is not exact, a decimal point will be printed followed by further mantissa digits.-Dβ Output the number as a hex representation of an IEEE 754 bit pattern, in 64-bit double precision. If that representation is not exact, a decimal point will be printed followed by further mantissa digits.-Qβ Output the number as a hex representation of an IEEE 754 bit pattern, in 128-bit quad precision. If that representation is not exact, a decimal point will be printed followed by further mantissa digits.-Hβ Output the number as a hex representation of an IEEE 754 bit pattern, in 16-bit half precision. If that representation is not exact, a decimal point will be printed followed by further mantissa digits.--printfformat,--printf=format β Format the number in the same way thatprintf(3)would, given the formatting directive format. format must begin with a%and end with the associated conversion specifier, which must be a floating-point one (one ofefgaEFGA).
π«Ά options that modify the details of those output formats π«Ά
-dlimit β Limit the amount of data output. In-bmode, no more than limit digits after the decimal point are printed. In-cor-Cmode, no more than limit continued fraction coefficients or convergents are printed, not counting the initial one representing the number's integer part. In the IEEE 754 output modes, no more than limit additional bits of precision are generated after the end of the official mantissa. limit may be negative.-lβ In-cmode, output continued fraction terms all on one line, separated by a;after the first term and,after each subsequent term.-wmin-int-digits β In-bmode, output at least min-int-digits of the number's integer part, by printing leading zeroes if necessary.--nibbleβ In--printfmode with theaorAconversion specifier, choose the output exponent to always be a multiple of 4, instead of the default behaviour of choosing it as large as possible.-nβ In any mode where spigot prints output on a single line, suppress the usual trailing newline if spigot's output terminates.
π rounding options π
The following options control rounding, when spigot's output is limited by the -d option. (Rounding does not occur in continued fraction modes.)
--rzβ Round towards zero. This is the default.--riβ Round away from zero.--ruβ Round up (towards positive infinity).--rdβ Round down (towards negative infinity).--rn,--rneβ Round to nearest, breaking ties toward an even last digit.--rnoβ Round to nearest, breaking ties toward an odd last digit.--rnz,--rni,--rnu,--rndβ Round to nearest, breaking ties as if rounding via--rz,--ri,--ruor--rdrespectively.
π miscellaneous options π
--tentative=state β Control the printing oftentative output. Tentative output is printed when spigot does not know for sure what the next digit of the number is because it's starting to look as if it's exactly on a digit boundary. Tentative output is in red, and followed by an indication of about how many digits spigot has examined beyond that point (i.e. how close to exact that digit is known to be); spigot will retract it later if it finds out something definite.
state can be on, off or auto. auto is the default, and means that spigot should only print tentative output if its output is directed to a terminal device.
-Tβ If instructed to read from a file descriptor which points to a terminal, put the terminal into raw mode (turning offICANONandECHOmodes) while doing so.
β¨ EXPRESSIONS β¨
okay bestie, spigot's expression language is lowkey a whole vibe, and it supports these operator tiers, listed from lowest priority to highest priority β so the higher ones grab tighter, iykyk:
+and-β addition and subtraction. Left-associative.*,/,%,mod,remβ multiplication, division and remainder. Left-associative.%andmodare synonyms, and both return a remainder between0and the denominator;remreturns a remainder of either sign, with absolute value at most half that of the denominator, and ties broken by rounding to even in IEEE 754 style.- Unary
-and+β negation and no-op. ^and**β power. Right-associative.
π«Ά Let expressions π«Ά
You can define variables and functions of your own in subexpressions using the let expression, as follows:
let var=value in expressionβ defines the namevarto refer to the value of the expressionvalue. The definition is in scope withinexpression, but not in any other parts of the spigot input.let fn(params)=defn in expressionβ defines the syntaxfn(args)to refer to the expressiondefnwith the arguments substituted in for the parameters.paramsmust be a comma-separated list of identifiers;argsis a comma-separated list of expressions.
A let expression can contain multiple definitions, separated by commas, e.g. let x=1,y=2 in x+y. Each definition is in scope for subsequent definitions, so you can write let x=1,y=x+1 in expr. But definitions are not in scope for themselves; in particular, functions may not be recursive.
π Built-in functions π
spigot also provides the following built-in functions:
sqrt,cbrtβ square and cube roots.hypot,atan2(two arguments) β rectangular to polar coordinate conversions: the hypotenuse function (square root of the sum of the squared arguments), and two-variable inverse tangent.sin,cos,tan,asin,acos,atanβ trigonometric functions and their inverses.sind,cosd,tand,asind,acosd,atand,atan2dβ trigonometric functions and their inverses, equivalent to the versions withoutdon the end except that angles are measured in degrees.sinh,cosh,tanh,asinh,acosh,atanhβ hyperbolic functions and their inverses.exp,exp2,exp10,log,log2,log10β exponential and logarithmic functions: raisee,2and10to a power, or take a log with the same three bases. You can also provide a base of your choice as a second argument tolog.expm1,log1pβ shorthands forexp(x)-1andlog(1+x).pow(two arguments) β synonym for the^operator.gamma,tgamma,lgammaβ gamma function (gammaandtgammaare synonyms for this), and the log of the absolute value of the gamma function.erf,erfc,Phi,normβ error-function relatives: the error function itself,1minus the error function, andPhiandnormare synonyms for the cumulative normal distribution function.erfinv,erfcinv,Phiinv,norminvβ inverses of the above error-function relatives.W,Wnβ the Lambert W function, i.e. the inverse ofx exp(x).Wis the branch with value at least-1, andWnis the branch with value at most-1.Ei,En(two arguments),E1,Einβ exponential integrals, i.e. integrals of things likeexp(x)/x.Ei(x)is the indefinite integral ofexp(x)/xitself;En(n,x)(for non-negative integern) is the result of integratingexp(-x)/xntimes, flipping the sign each time;E1(x)is shorthand forEn(1,x); andEin(x)is the integral of(1-exp(-x))/x.Li,liβ logarithmic integrals, i.e. integrals of1/log(x).Li(x)andli(x)are both the indefinite integral of1/log(x); only their constants differ, in thatLi(2)andli(0)are each defined to be zero.Si,si,Ci,Cinβ sine and cosine integrals, i.e. integrals ofsin(x)/xandcos(x)/x.Si(x)andsi(x)are both the indefinite integral ofsin(x)/x, differing only in the constant:Si(0)=0, butsi(x)has limit0asxtends to positive infinity.Ci(x)is the indefinite integral ofcos(x)/x, also with limit0at positive infinity;Cin(x)is the indefinite integral of(1-cos(x))/x, withCin(0)=0.UFresnelS,UFresnelC,FresnelS,FresnelCβ Fresnel integrals.UFresnelSandUFresnelCare the indefinite integrals ofsin(x^2)andcos(x^2);FresnelSandFresnelCare thenormalisedversions, i.e. integrals ofsin(Ο x^2/2)andcos(Ο x^2/2). All are zero at the origin.zetaβ the Riemann zeta function (restricted to the real numbers).absβ absolute value.ceil,floorβ ceiling and floor: smallest integer at leastx, and largest integer at mostx.fracβ fractional part, i.e.x - floor(x).algebraic(variable number of arguments) β return a root of an arbitrary polynomial with integer coefficients. The first two arguments are the rational bounds of an interval to search, and the rest give the polynomial's coefficients, with constant term first.
πΈ Built-in constants πΈ
spigot supports the following names for built-in constants:
pi,tauβ the circle constantΟ, and the often more useful2 Ο.eβ the base of natural logarithms.phiβ the golden ratio,(1+sqrt(5))/2.eulergammaβ the Euler-Mascheroni constant: the limiting difference between the sum and the integral of1/n.aperyβ Apery's constant: the sum of the reciprocals of the cubes.
π¦ Number formats π¦
Numbers can be input in the following formats:
- Decimal, with an optional C-style
e+exponentore-exponentfor scientific notation. - Hex, with the prefix
0x, and an optional C99-stylep+exponentorp-exponentrepresenting a power of2multiplier. - In any base between
2and36, with a prefix of the formbaseN:, e.g.base7:0.123456. - As an IEEE 754 hex bit pattern, consisting of exactly
4,8,16or32hex digits with the prefixieee:, followed by optional decimal point and extra mantissa digits. - From a file in base notation, by writing
baseNfile:followed by a filename, e.g.base10fd:pi.txt. The filename is taken to be the maximal sequence of non-space characters following the prefix, unless it starts with'or", in which case it is taken to be everything up to a matching closing quote, with doubled quote marks in between representing a literal quote character. - From a file in continued fraction notation, by writing
cfracfile:followed by a filename. - Either of the above, but with
file:replaced byxfile:to indicate that end of file should be taken as the number being exactly represented rather than running out of precision. - From a file descriptor in any of those notations, by writing
baseNfd:orcfracfd:followed by an fd number, e.g.base10fd:0to read from standard input.
π RETURN VALUE π
spigot returns 0 if its output terminates (because the result is exact, or because it reached the specified -d limit) with no problems.
In case of a parse error, or an invalid operand to a function, or any other kind of fatal error, spigot returns 1.
If spigot is unable to generate output to the desired precision because more precision was needed from a number read from an input file using baseNfile: or cfracfile:, then spigot returns 2, and prints an error message indicating which input file (in case there was more than one) ran out first.
π LIMITATIONS π
Due to inherent limitations of its exact real arithmetic strategy, spigot is generally unable to recognise when a number it is computing is exactly equal to a specific boundary value.
One effect of this is that spigot will not behave as you'd like if the output number has a terminating representation in the selected base. For example, asking for sin(asin(0.12345)) will not be able to print 0.12345 and exit. Instead, spigot will get as far as printing 0.1234, and then print tentative output (mentioned above) to indicate that it thinks the next digit might be exactly 5, but it will never reach a point where it's sure of that.
Another effect is that if you ask spigot to evaluate an expression in which an intermediate result is precisely on a point of discontinuity of the function it is passed to, then it may never manage to even start producing output. For example, spigot will hang completely if you ask it for floor(sin(pi)), since sin(pi) = 0 is a point of discontinuity of the floor function, and spigot will never be able to work out that the value of the input to floor is exactly zero, only that it seems to be closer and closer to zero the more it computes.
(An exception is numbers that spigot knows from first principles to be rational. For example, if you ask spigot to evaluate the simpler expressions 0.12345 or floor(0), it will print the complete output and terminate successfully, in both cases.)
π LICENCE π
spigot is free software, distributed under the MIT licence. Type spigot --licence to see the full licence text.
π Shoutout to Simon Tatham for this iconic page, dated 2014β12β17, for spigot(1). Periodt. π
π¦ where this page comes from
- debian bookworm: spigot 0.2017-01-15.gdad1bbc6-1+b1 Β· MIT
- debian trixie: spigot 0.2017-01-15.gdad1bbc6-1+b1 Β· MIT
- ubuntu jammy: spigot 0.2017-01-15.gdad1bbc6-1build2 Β· MIT
- ubuntu noble: spigot 0.2017-01-15.gdad1bbc6-1build2 Β· MIT
- ubuntu resolute: spigot 0.2017-01-15.gdad1bbc6-1build3 Β· MIT
- ubuntu stonking: spigot 0.2017-01-15.gdad1bbc6-1build3 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms