engine

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Example (BondPrice)
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	c := engine.New()

	c.FinN = 5
	c.Y = 7
	c.X = 6
	c.BondPrice()

	fmt.Printf("Bond price: $%.2f\n", c.X)
}
Output:
Bond price: $97.74
Example (DateAdd)
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	c := engine.New()

	c.Flags.Dmy = false
	c.Y = 3.012025
	c.X = 90
	c.DateAdd()

	fmt.Printf("Date after 90 days: %.6f\n", c.X)
}
Output:
Date after 90 days: 5.302025
Example (DateDays)
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	c := engine.New()

	c.Flags.Dmy = false
	c.Y = 1.012025
	c.X = 6.152025
	c.DaysBetween()

	fmt.Printf("Days between: %.0f\n", c.X)
}
Output:
Days between: 165
Example (DepreciationSL)
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	c := engine.New()

	c.FinN = 10
	c.FinPV = 50000
	c.FinFV = 5000
	c.X = 1
	c.DepreciationSL()

	fmt.Printf("Annual depreciation: $%.2f\n", c.X)
}
Output:
Annual depreciation: $4500.00
Example (FutureValue)
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	c := engine.New()

	c.FinN = 20
	c.FinI = 8
	c.FinPV = -10000
	c.FinPMT = 0
	c.SolveFV()

	fmt.Printf("Future value: $%.2f\n", c.X)
}
Output:
Future value: $46609.57
Example (Irr)
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	c := engine.New()

	c.FinCF0 = -100000
	c.FinCFj[0] = 40000
	c.FinCFj[1] = 50000
	c.FinCFj[2] = 30000
	c.FinCfCnt = 3
	c.Flags.StackLift = true
	c.ComputeIRR()

	fmt.Printf("IRR: %.2f%%\n", c.X)
}
Output:
IRR: 10.13%
Example (Mortgage)
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	c := engine.New()

	c.FinN = 30 * 12
	c.FinI = 6.0 / 12.0
	c.FinPV = 300000
	c.FinFV = 0
	c.SolvePMT()

	fmt.Printf("Monthly payment: $%.2f\n", -c.X)
}
Output:
Monthly payment: $1798.65
Example (Npv)
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	c := engine.New()

	c.FinCF0 = -100000
	c.FinCFj[0] = 30000
	c.FinCFj[1] = 40000
	c.FinCFj[2] = 50000
	c.FinCFj[3] = 60000
	c.FinCfCnt = 4
	c.X = 10
	c.Flags.StackLift = true
	c.ComputeNPV()

	fmt.Printf("NPV: $%.2f\n", c.X)
}
Output:
NPV: $38877.13
Example (Statistics)
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	c := engine.New()

	c.X, c.Y = 10, 1
	c.StatAdd()
	c.X, c.Y = 15, 2
	c.StatAdd()
	c.X, c.Y = 22, 3
	c.StatAdd()
	c.X, c.Y = 18, 4
	c.StatAdd()
	c.X, c.Y = 25, 5
	c.StatAdd()

	c.MeanX()
	meanX := c.X

	c.SDev()
	sdev := c.X

	fmt.Printf("Mean: %.1f, StdDev: %.1f\n", meanX, sdev)
}
Output:
Mean: 3.0, StdDev: 1.6

Index

Examples

Constants

View Source
const MaxDigits = 10

Variables

View Source
var (
	// ErrNoConvergence is returned by Rate/IRR when the iterative solver fails
	// to converge (e.g. a cash-flow stream with no valid internal rate).
	ErrNoConvergence = errors.New("engine: solution did not converge")
	// ErrNoSolution is returned when the inputs admit no finite answer.
	ErrNoSolution = errors.New("engine: no solution for the given inputs")
)

Functions

func DepDB

func DepDB(cost, salvage, life, period, factorPct float64) (dep, remaining float64)

DepDB returns declining-balance depreciation for the given period and the remaining book value (above salvage) after that period. factorPct is the declining-balance factor as a percent (200 == double-declining balance); zero defaults to 200%.

func DepSL

func DepSL(cost, salvage, life, period float64) (dep, remaining float64)

DepSL returns straight-line depreciation for the given period and the remaining depreciable value after that period.

Example
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	// $50,000 asset, $5,000 salvage, 10-year life, first year.
	dep, remaining := engine.DepSL(50000, 5000, 10, 1)
	fmt.Printf("Year 1 depreciation: $%.2f, remaining: $%.2f\n", dep, remaining)
}
Output:
Year 1 depreciation: $4500.00, remaining: $40500.00

func DepSOYD

func DepSOYD(cost, salvage, life, period float64) (dep, remaining float64)

DepSOYD returns sum-of-the-years'-digits depreciation for the given period and the remaining depreciable value after that period.

func FV

func FV(rate, n, pv, pmt float64, when Timing) float64

FV returns the future value of a series of cash flows.

Example
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	// $10,000 invested for 20 years at 8% (outflow now, so PV is negative).
	fv := engine.FV(0.08, 20, -10000, 0, engine.End)
	fmt.Printf("Future value: $%.2f\n", fv)
}
Output:
Future value: $46609.57

func IRR

func IRR(cashflows ...float64) (float64, error)

IRR returns the internal rate of return (as a fraction) of a cash-flow stream, i.e. the rate at which NPV is zero. cashflows[0] is the period-0 flow. It returns ErrNoConvergence if the solver does not converge.

Example
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	irr, _ := engine.IRR(-100000, 40000, 50000, 30000)
	fmt.Printf("IRR: %.2f%%\n", irr*100)
}
Output:
IRR: 10.13%

func NPV

func NPV(rate float64, cashflows ...float64) float64

NPV returns the net present value of a cash-flow stream discounted at rate (a fraction). cashflows[0] is the period-0 flow (typically the initial outlay); cashflows[k] occurs at the end of period k.

Example
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	npv := engine.NPV(0.10, -100000, 30000, 40000, 50000, 60000)
	fmt.Printf("NPV: $%.2f\n", npv)
}
Output:
NPV: $38877.13

func NPer

func NPer(rate, pmt, pv, fv float64, when Timing) (float64, error)

NPer solves for the number of periods. It returns ErrNoSolution when the cash flows never reach the target future value.

func NaNf64 added in v0.3.0

func NaNf64() float64

func PMT

func PMT(rate, n, pv, fv float64, when Timing) float64

PMT returns the periodic payment for a loan or annuity.

Example
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	// A $300,000 mortgage at 6% APR over 30 years, paid monthly.
	pmt := engine.PMT(0.06/12, 360, 300000, 0, engine.End)
	fmt.Printf("Monthly payment: $%.2f\n", -pmt)
}
Output:
Monthly payment: $1798.65

func PV

func PV(rate, n, pmt, fv float64, when Timing) float64

PV returns the present value of a series of cash flows.

func Rate

func Rate(n, pmt, pv, fv float64, when Timing) (float64, error)

Rate solves for the periodic interest rate (as a fraction) using Newton-Raphson iteration. It returns ErrNoConvergence if no rate is found.

Example
package main

import (
	"fmt"

	"github.com/shanehull/dozen/engine"
)

func main() {
	// What rate turns $1,000 today into $2,000 after 12 periods?
	r, _ := engine.Rate(12, 0, -1000, 2000, engine.End)
	fmt.Printf("Rate: %.3f%%\n", r*100)
}
Output:
Rate: 5.946%

func SolveTVM added in v0.3.0

func SolveTVM(n, i, pv, pmt, fv float64, when Timing) (float64, error)

SolveTVM solves for the missing time-value-of-money variable. Pass math.NaN() for exactly one of n, i, pv, pmt, or fv. i is the periodic rate as a fraction (0.05 = 5%). The result is the solved value or an error if the solver fails to converge.

Types

type AngleMode

type AngleMode int
const (
	Deg AngleMode = iota
	Rad
	Grad
)

type Engine

type Engine struct {
	LastX float64
	Mem   [20]float64

	StatsN, StatsSx, StatsSy, StatsSxx, StatsSyy, StatsSxy float64
	StatsLx, StatsLy, StatsWx, StatsWxx                    float64

	PgmLen  int
	PgmPC   int
	Program [200]Instruction

	Flags Flags

	Stack
	Registers
}

func New

func New() *Engine

func (*Engine) Abs

func (e *Engine) Abs()

func (*Engine) Acos

func (e *Engine) Acos()

func (*Engine) Add

func (e *Engine) Add()

func (*Engine) Amortize

func (e *Engine) Amortize()

func (*Engine) Asin

func (e *Engine) Asin()

func (*Engine) Atan

func (e *Engine) Atan()

func (*Engine) BST

func (e *Engine) BST()

func (*Engine) BondPrice

func (e *Engine) BondPrice()

func (*Engine) BondYield

func (e *Engine) BondYield()

func (*Engine) Chs

func (e *Engine) Chs()

func (*Engine) ClearFin

func (e *Engine) ClearFin()

func (*Engine) ClearPgm

func (e *Engine) ClearPgm()

func (*Engine) ClearReg

func (e *Engine) ClearReg()

func (*Engine) ClearStats

func (e *Engine) ClearStats()

func (*Engine) Clx

func (e *Engine) Clx()

func (*Engine) ComputeIRR

func (e *Engine) ComputeIRR()

func (*Engine) ComputeNPV

func (e *Engine) ComputeNPV()

func (*Engine) Cos

func (e *Engine) Cos()

func (*Engine) DateAdd

func (e *Engine) DateAdd()

func (*Engine) DaysBetween

func (e *Engine) DaysBetween()

func (*Engine) DepreciationDB

func (e *Engine) DepreciationDB()

func (*Engine) DepreciationSL

func (e *Engine) DepreciationSL()

func (*Engine) DepreciationSOYD

func (e *Engine) DepreciationSOYD()

func (*Engine) Div

func (e *Engine) Div()

func (*Engine) Enter

func (e *Engine) Enter()

func (*Engine) Exp

func (e *Engine) Exp()

func (*Engine) Exp10

func (e *Engine) Exp10()

func (*Engine) Fact

func (e *Engine) Fact()

func (*Engine) Frac

func (e *Engine) Frac()

func (*Engine) Goto

func (e *Engine) Goto(line int)

func (*Engine) Intg

func (e *Engine) Intg()

func (*Engine) LastXRecall

func (e *Engine) LastXRecall()

func (*Engine) LinEst

func (e *Engine) LinEst()

func (*Engine) Ln

func (e *Engine) Ln()

func (*Engine) Log

func (e *Engine) Log()

func (*Engine) MeanX

func (e *Engine) MeanX()

func (*Engine) MeanY

func (e *Engine) MeanY()

func (*Engine) Mul

func (e *Engine) Mul()

func (*Engine) Pct

func (e *Engine) Pct()

func (*Engine) PctChg

func (e *Engine) PctChg()

func (*Engine) PctTotal

func (e *Engine) PctTotal()

func (*Engine) Pi

func (e *Engine) Pi()

func (*Engine) Push

func (e *Engine) Push()

func (*Engine) Recall

func (e *Engine) Recall(idx int)

func (*Engine) Recip

func (e *Engine) Recip()

func (*Engine) Restore

func (e *Engine) Restore(s EngineState)

func (*Engine) RollDown

func (e *Engine) RollDown()

func (*Engine) RollUp

func (e *Engine) RollUp()

func (*Engine) SDev

func (e *Engine) SDev()

func (*Engine) SST

func (e *Engine) SST()

func (*Engine) SetFV

func (e *Engine) SetFV()

func (*Engine) SetI

func (e *Engine) SetI()

func (*Engine) SetN

func (e *Engine) SetN()

func (*Engine) SetPMT

func (e *Engine) SetPMT()

func (*Engine) SetPV

func (e *Engine) SetPV()

func (*Engine) Sin

func (e *Engine) Sin()

func (*Engine) Snapshot

func (e *Engine) Snapshot() EngineState

func (*Engine) SolveFV

func (e *Engine) SolveFV()

func (*Engine) SolveI

func (e *Engine) SolveI()

func (*Engine) SolveN

func (e *Engine) SolveN()

func (*Engine) SolvePMT

func (e *Engine) SolvePMT()

func (*Engine) SolvePV

func (e *Engine) SolvePV()

func (*Engine) Sqr

func (e *Engine) Sqr()

func (*Engine) Sqrt

func (e *Engine) Sqrt()

func (*Engine) StatAdd

func (e *Engine) StatAdd()

func (*Engine) Store

func (e *Engine) Store(idx int)

func (*Engine) Sub

func (e *Engine) Sub()

func (*Engine) Tan

func (e *Engine) Tan()

func (*Engine) ToDeg

func (e *Engine) ToDeg()

func (*Engine) ToH

func (e *Engine) ToH()

func (*Engine) ToHMS

func (e *Engine) ToHMS()

func (*Engine) ToPolar

func (e *Engine) ToPolar()

func (*Engine) ToRad

func (e *Engine) ToRad()

func (*Engine) ToRect

func (e *Engine) ToRect()

func (*Engine) Tuck

func (e *Engine) Tuck()

func (*Engine) WeightedMean

func (e *Engine) WeightedMean()

func (*Engine) XY

func (e *Engine) XY()

func (*Engine) YPowX

func (e *Engine) YPowX()

type EngineState

type EngineState struct {
	X    float64     `json:"x"`
	Y    float64     `json:"y"`
	Z    float64     `json:"z"`
	T    float64     `json:"t"`
	Last float64     `json:"last"`
	Mem  [20]float64 `json:"mem"`

	FinN      float64     `json:"finN"`
	FinI      float64     `json:"finI"`
	FinPV     float64     `json:"finPV"`
	FinPMT    float64     `json:"finPMT"`
	FinFV     float64     `json:"finFV"`
	FinCF0    float64     `json:"finCF0"`
	FinCFj    [10]float64 `json:"finCFj"`
	FinNj     [10]int     `json:"finNj"`
	FinCfCnt  int         `json:"finCfCnt"`
	AmortN    float64     `json:"amortN"`
	AmortInt  float64     `json:"amortInt"`
	AmortPrin float64     `json:"amortPrin"`

	Begin bool      `json:"begin"`
	Dmy   bool      `json:"dmy"`
	Angle AngleMode `json:"angle"`
}

func UnmarshalState

func UnmarshalState(data []byte) (EngineState, error)

func (EngineState) Marshal

func (s EngineState) Marshal() ([]byte, error)

type Flags

type Flags struct {
	StackLift bool
	Begin     bool
	Dmy       bool
	Angle     AngleMode
}

type Instruction

type Instruction struct {
	Op    string
	Arg   float64
	ArgS  string
	IsPfx bool
}

type Registers

type Registers struct {
	FinN      float64
	FinI      float64
	FinPV     float64
	FinPMT    float64
	FinFV     float64
	FinCF0    float64
	FinCFj    [10]float64
	FinNj     [10]int
	FinCfCnt  int
	AmortN    float64
	AmortInt  float64
	AmortPrin float64
}

type Stack

type Stack struct {
	X, Y, Z, T float64
}

type Timing

type Timing bool

Timing selects whether payments occur at the end of each period (an ordinary annuity) or the beginning (an annuity due, "BEGIN" mode on the HP-12C).

const (
	End   Timing = false
	Begin Timing = true
)

Jump to

Keyboard shortcuts

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