| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415 |
- // Copyright 2016 - 2020 The excelize Authors. All rights reserved. Use of
- // this source code is governed by a BSD-style license that can be found in
- // the LICENSE file.
- //
- // Package excelize providing a set of functions that allow you to write to
- // and read from XLSX / XLSM / XLTM files. Supports reading and writing
- // spreadsheet documents generated by Microsoft Exce™ 2007 and later. Supports
- // complex components by high compatibility, and provided streaming API for
- // generating or reading data from a worksheet with huge amounts of data. This
- // library needs Go version 1.10 or later.
- package excelize
- import (
- "bytes"
- "container/list"
- "errors"
- "fmt"
- "math"
- "math/rand"
- "reflect"
- "strconv"
- "strings"
- "time"
- "github.com/xuri/efp"
- )
- // Excel formula errors
- const (
- formulaErrorDIV = "#DIV/0!"
- formulaErrorNAME = "#NAME?"
- formulaErrorNA = "#N/A"
- formulaErrorNUM = "#NUM!"
- formulaErrorVALUE = "#VALUE!"
- formulaErrorREF = "#REF!"
- formulaErrorNULL = "#NULL"
- formulaErrorSPILL = "#SPILL!"
- formulaErrorCALC = "#CALC!"
- formulaErrorGETTINGDATA = "#GETTING_DATA"
- )
- // cellRef defines the structure of a cell reference.
- type cellRef struct {
- Col int
- Row int
- Sheet string
- }
- // cellRef defines the structure of a cell range.
- type cellRange struct {
- From cellRef
- To cellRef
- }
- // formulaArg is the argument of a formula or function.
- type formulaArg struct {
- Value string
- Matrix []string
- }
- // formulaFuncs is the type of the formula functions.
- type formulaFuncs struct{}
- // CalcCellValue provides a function to get calculated cell value. This
- // feature is currently in beta. Array formula, table formula and some other
- // formulas are not supported currently.
- func (f *File) CalcCellValue(sheet, cell string) (result string, err error) {
- var (
- formula string
- token efp.Token
- )
- if formula, err = f.GetCellFormula(sheet, cell); err != nil {
- return
- }
- ps := efp.ExcelParser()
- tokens := ps.Parse(formula)
- if tokens == nil {
- return
- }
- if token, err = f.evalInfixExp(sheet, tokens); err != nil {
- return
- }
- result = token.TValue
- return
- }
- // getPriority calculate arithmetic operator priority.
- func getPriority(token efp.Token) (pri int) {
- var priority = map[string]int{
- "*": 2,
- "/": 2,
- "+": 1,
- "-": 1,
- }
- pri, _ = priority[token.TValue]
- if token.TValue == "-" && token.TType == efp.TokenTypeOperatorPrefix {
- pri = 3
- }
- if token.TSubType == efp.TokenSubTypeStart && token.TType == efp.TokenTypeSubexpression { // (
- pri = 0
- }
- return
- }
- // evalInfixExp evaluate syntax analysis by given infix expression after
- // lexical analysis. Evaluate an infix expression containing formulas by
- // stacks:
- //
- // opd - Operand
- // opt - Operator
- // opf - Operation formula
- // opfd - Operand of the operation formula
- // opft - Operator of the operation formula
- //
- // Evaluate arguments of the operation formula by list:
- //
- // args - Arguments of the operation formula
- //
- // TODO: handle subtypes: Nothing, Text, Logical, Error, Concatenation, Intersection, Union
- //
- func (f *File) evalInfixExp(sheet string, tokens []efp.Token) (efp.Token, error) {
- var err error
- opdStack, optStack, opfStack, opfdStack, opftStack := NewStack(), NewStack(), NewStack(), NewStack(), NewStack()
- argsList := list.New()
- for i := 0; i < len(tokens); i++ {
- token := tokens[i]
- // out of function stack
- if opfStack.Len() == 0 {
- if err = f.parseToken(sheet, token, opdStack, optStack); err != nil {
- return efp.Token{}, err
- }
- }
- // function start
- if token.TType == efp.TokenTypeFunction && token.TSubType == efp.TokenSubTypeStart {
- opfStack.Push(token)
- continue
- }
- // in function stack, walk 2 token at once
- if opfStack.Len() > 0 {
- var nextToken efp.Token
- if i+1 < len(tokens) {
- nextToken = tokens[i+1]
- }
- // current token is args or range, skip next token, order required: parse reference first
- if token.TSubType == efp.TokenSubTypeRange {
- if !opftStack.Empty() {
- // parse reference: must reference at here
- result, _, err := f.parseReference(sheet, token.TValue)
- if err != nil {
- return efp.Token{TValue: formulaErrorNAME}, err
- }
- if len(result) != 1 {
- return efp.Token{}, errors.New(formulaErrorVALUE)
- }
- opfdStack.Push(efp.Token{
- TType: efp.TokenTypeOperand,
- TSubType: efp.TokenSubTypeNumber,
- TValue: result[0],
- })
- continue
- }
- if nextToken.TType == efp.TokenTypeArgument || nextToken.TType == efp.TokenTypeFunction {
- // parse reference: reference or range at here
- result, matrix, err := f.parseReference(sheet, token.TValue)
- if err != nil {
- return efp.Token{TValue: formulaErrorNAME}, err
- }
- for idx, val := range result {
- arg := formulaArg{Value: val}
- if idx < len(matrix) {
- arg.Matrix = matrix[idx]
- }
- argsList.PushBack(arg)
- }
- if len(result) == 0 {
- return efp.Token{}, errors.New(formulaErrorVALUE)
- }
- continue
- }
- }
- // check current token is opft
- if err = f.parseToken(sheet, token, opfdStack, opftStack); err != nil {
- return efp.Token{}, err
- }
- // current token is arg
- if token.TType == efp.TokenTypeArgument {
- for !opftStack.Empty() {
- // calculate trigger
- topOpt := opftStack.Peek().(efp.Token)
- if err := calculate(opfdStack, topOpt); err != nil {
- return efp.Token{}, err
- }
- opftStack.Pop()
- }
- if !opfdStack.Empty() {
- argsList.PushBack(formulaArg{
- Value: opfdStack.Pop().(efp.Token).TValue,
- })
- }
- continue
- }
- // current token is logical
- if token.TType == efp.OperatorsInfix && token.TSubType == efp.TokenSubTypeLogical {
- }
- // current token is text
- if token.TType == efp.TokenTypeOperand && token.TSubType == efp.TokenSubTypeText {
- argsList.PushBack(formulaArg{
- Value: token.TValue,
- })
- }
- // current token is function stop
- if token.TType == efp.TokenTypeFunction && token.TSubType == efp.TokenSubTypeStop {
- for !opftStack.Empty() {
- // calculate trigger
- topOpt := opftStack.Peek().(efp.Token)
- if err := calculate(opfdStack, topOpt); err != nil {
- return efp.Token{}, err
- }
- opftStack.Pop()
- }
- // push opfd to args
- if opfdStack.Len() > 0 {
- argsList.PushBack(formulaArg{
- Value: opfdStack.Pop().(efp.Token).TValue,
- })
- }
- // call formula function to evaluate
- result, err := callFuncByName(&formulaFuncs{}, strings.NewReplacer(
- "_xlfn", "", ".", "").Replace(opfStack.Peek().(efp.Token).TValue),
- []reflect.Value{reflect.ValueOf(argsList)})
- if err != nil {
- return efp.Token{}, err
- }
- argsList.Init()
- opfStack.Pop()
- if opfStack.Len() > 0 { // still in function stack
- opfdStack.Push(efp.Token{TValue: result, TType: efp.TokenTypeOperand, TSubType: efp.TokenSubTypeNumber})
- } else {
- opdStack.Push(efp.Token{TValue: result, TType: efp.TokenTypeOperand, TSubType: efp.TokenSubTypeNumber})
- }
- }
- }
- }
- for optStack.Len() != 0 {
- topOpt := optStack.Peek().(efp.Token)
- if err = calculate(opdStack, topOpt); err != nil {
- return efp.Token{}, err
- }
- optStack.Pop()
- }
- return opdStack.Peek().(efp.Token), err
- }
- // calculate evaluate basic arithmetic operations.
- func calculate(opdStack *Stack, opt efp.Token) error {
- if opt.TValue == "-" && opt.TType == efp.TokenTypeOperatorPrefix {
- opd := opdStack.Pop().(efp.Token)
- opdVal, err := strconv.ParseFloat(opd.TValue, 64)
- if err != nil {
- return err
- }
- result := 0 - opdVal
- opdStack.Push(efp.Token{TValue: fmt.Sprintf("%g", result), TType: efp.TokenTypeOperand, TSubType: efp.TokenSubTypeNumber})
- }
- if opt.TValue == "+" {
- rOpd := opdStack.Pop().(efp.Token)
- lOpd := opdStack.Pop().(efp.Token)
- lOpdVal, err := strconv.ParseFloat(lOpd.TValue, 64)
- if err != nil {
- return err
- }
- rOpdVal, err := strconv.ParseFloat(rOpd.TValue, 64)
- if err != nil {
- return err
- }
- result := lOpdVal + rOpdVal
- opdStack.Push(efp.Token{TValue: fmt.Sprintf("%g", result), TType: efp.TokenTypeOperand, TSubType: efp.TokenSubTypeNumber})
- }
- if opt.TValue == "-" && opt.TType == efp.TokenTypeOperatorInfix {
- rOpd := opdStack.Pop().(efp.Token)
- lOpd := opdStack.Pop().(efp.Token)
- lOpdVal, err := strconv.ParseFloat(lOpd.TValue, 64)
- if err != nil {
- return err
- }
- rOpdVal, err := strconv.ParseFloat(rOpd.TValue, 64)
- if err != nil {
- return err
- }
- result := lOpdVal - rOpdVal
- opdStack.Push(efp.Token{TValue: fmt.Sprintf("%g", result), TType: efp.TokenTypeOperand, TSubType: efp.TokenSubTypeNumber})
- }
- if opt.TValue == "*" {
- rOpd := opdStack.Pop().(efp.Token)
- lOpd := opdStack.Pop().(efp.Token)
- lOpdVal, err := strconv.ParseFloat(lOpd.TValue, 64)
- if err != nil {
- return err
- }
- rOpdVal, err := strconv.ParseFloat(rOpd.TValue, 64)
- if err != nil {
- return err
- }
- result := lOpdVal * rOpdVal
- opdStack.Push(efp.Token{TValue: fmt.Sprintf("%g", result), TType: efp.TokenTypeOperand, TSubType: efp.TokenSubTypeNumber})
- }
- if opt.TValue == "/" {
- rOpd := opdStack.Pop().(efp.Token)
- lOpd := opdStack.Pop().(efp.Token)
- lOpdVal, err := strconv.ParseFloat(lOpd.TValue, 64)
- if err != nil {
- return err
- }
- rOpdVal, err := strconv.ParseFloat(rOpd.TValue, 64)
- if err != nil {
- return err
- }
- result := lOpdVal / rOpdVal
- if rOpdVal == 0 {
- return errors.New(formulaErrorDIV)
- }
- opdStack.Push(efp.Token{TValue: fmt.Sprintf("%g", result), TType: efp.TokenTypeOperand, TSubType: efp.TokenSubTypeNumber})
- }
- return nil
- }
- // parseToken parse basic arithmetic operator priority and evaluate based on
- // operators and operands.
- func (f *File) parseToken(sheet string, token efp.Token, opdStack, optStack *Stack) error {
- // parse reference: must reference at here
- if token.TSubType == efp.TokenSubTypeRange {
- result, _, err := f.parseReference(sheet, token.TValue)
- if err != nil {
- return errors.New(formulaErrorNAME)
- }
- if len(result) != 1 {
- return errors.New(formulaErrorVALUE)
- }
- token.TValue = result[0]
- token.TType = efp.TokenTypeOperand
- token.TSubType = efp.TokenSubTypeNumber
- }
- if (token.TValue == "-" && token.TType == efp.TokenTypeOperatorPrefix) || token.TValue == "+" || token.TValue == "-" || token.TValue == "*" || token.TValue == "/" {
- if optStack.Len() == 0 {
- optStack.Push(token)
- } else {
- tokenPriority := getPriority(token)
- topOpt := optStack.Peek().(efp.Token)
- topOptPriority := getPriority(topOpt)
- if tokenPriority > topOptPriority {
- optStack.Push(token)
- } else {
- for tokenPriority <= topOptPriority {
- optStack.Pop()
- if err := calculate(opdStack, topOpt); err != nil {
- return err
- }
- if optStack.Len() > 0 {
- topOpt = optStack.Peek().(efp.Token)
- topOptPriority = getPriority(topOpt)
- continue
- }
- break
- }
- optStack.Push(token)
- }
- }
- }
- if token.TType == efp.TokenTypeSubexpression && token.TSubType == efp.TokenSubTypeStart { // (
- optStack.Push(token)
- }
- if token.TType == efp.TokenTypeSubexpression && token.TSubType == efp.TokenSubTypeStop { // )
- for optStack.Peek().(efp.Token).TSubType != efp.TokenSubTypeStart && optStack.Peek().(efp.Token).TType != efp.TokenTypeSubexpression { // != (
- topOpt := optStack.Peek().(efp.Token)
- if err := calculate(opdStack, topOpt); err != nil {
- return err
- }
- optStack.Pop()
- }
- optStack.Pop()
- }
- // opd
- if token.TType == efp.TokenTypeOperand && token.TSubType == efp.TokenSubTypeNumber {
- opdStack.Push(token)
- }
- return nil
- }
- // parseReference parse reference and extract values by given reference
- // characters and default sheet name.
- func (f *File) parseReference(sheet, reference string) (result []string, matrix [][]string, err error) {
- reference = strings.Replace(reference, "$", "", -1)
- refs, cellRanges, cellRefs := list.New(), list.New(), list.New()
- for _, ref := range strings.Split(reference, ":") {
- tokens := strings.Split(ref, "!")
- cr := cellRef{}
- if len(tokens) == 2 { // have a worksheet name
- cr.Sheet = tokens[0]
- if cr.Col, cr.Row, err = CellNameToCoordinates(tokens[1]); err != nil {
- return
- }
- if refs.Len() > 0 {
- e := refs.Back()
- cellRefs.PushBack(e.Value.(cellRef))
- refs.Remove(e)
- }
- refs.PushBack(cr)
- continue
- }
- if cr.Col, cr.Row, err = CellNameToCoordinates(tokens[0]); err != nil {
- return
- }
- e := refs.Back()
- if e == nil {
- cr.Sheet = sheet
- refs.PushBack(cr)
- continue
- }
- cellRanges.PushBack(cellRange{
- From: e.Value.(cellRef),
- To: cr,
- })
- refs.Remove(e)
- }
- if refs.Len() > 0 {
- e := refs.Back()
- cellRefs.PushBack(e.Value.(cellRef))
- refs.Remove(e)
- }
- result, matrix, err = f.rangeResolver(cellRefs, cellRanges)
- return
- }
- // rangeResolver extract value as string from given reference and range list.
- // This function will not ignore the empty cell. Note that the result of 3D
- // range references may be different from Excel in some cases, for example,
- // A1:A2:A2:B3 in Excel will include B1, but we wont.
- func (f *File) rangeResolver(cellRefs, cellRanges *list.List) (result []string, matrix [][]string, err error) {
- filter := map[string]string{}
- // extract value from ranges
- for temp := cellRanges.Front(); temp != nil; temp = temp.Next() {
- cr := temp.Value.(cellRange)
- if cr.From.Sheet != cr.To.Sheet {
- err = errors.New(formulaErrorVALUE)
- }
- rng := []int{cr.From.Col, cr.From.Row, cr.To.Col, cr.To.Row}
- sortCoordinates(rng)
- matrix = [][]string{}
- for row := rng[1]; row <= rng[3]; row++ {
- var matrixRow = []string{}
- for col := rng[0]; col <= rng[2]; col++ {
- var cell, value string
- if cell, err = CoordinatesToCellName(col, row); err != nil {
- return
- }
- if value, err = f.GetCellValue(cr.From.Sheet, cell); err != nil {
- return
- }
- filter[cell] = value
- matrixRow = append(matrixRow, value)
- }
- matrix = append(matrix, matrixRow)
- }
- }
- // extract value from references
- for temp := cellRefs.Front(); temp != nil; temp = temp.Next() {
- cr := temp.Value.(cellRef)
- var cell string
- if cell, err = CoordinatesToCellName(cr.Col, cr.Row); err != nil {
- return
- }
- if filter[cell], err = f.GetCellValue(cr.Sheet, cell); err != nil {
- return
- }
- }
- for _, val := range filter {
- result = append(result, val)
- }
- return
- }
- // callFuncByName calls the no error or only error return function with
- // reflect by given receiver, name and parameters.
- func callFuncByName(receiver interface{}, name string, params []reflect.Value) (result string, err error) {
- function := reflect.ValueOf(receiver).MethodByName(name)
- if function.IsValid() {
- rt := function.Call(params)
- if len(rt) == 0 {
- return
- }
- if !rt[1].IsNil() {
- err = rt[1].Interface().(error)
- return
- }
- result = rt[0].Interface().(string)
- return
- }
- err = fmt.Errorf("not support %s function", name)
- return
- }
- // Math and Trigonometric functions
- // ABS function returns the absolute value of any supplied number. The syntax
- // of the function is:
- //
- // ABS(number)
- //
- func (fn *formulaFuncs) ABS(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ABS requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Abs(val))
- return
- }
- // ACOS function calculates the arccosine (i.e. the inverse cosine) of a given
- // number, and returns an angle, in radians, between 0 and π. The syntax of
- // the function is:
- //
- // ACOS(number)
- //
- func (fn *formulaFuncs) ACOS(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ACOS requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Acos(val))
- return
- }
- // ACOSH function calculates the inverse hyperbolic cosine of a supplied number.
- // of the function is:
- //
- // ACOSH(number)
- //
- func (fn *formulaFuncs) ACOSH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ACOSH requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Acosh(val))
- return
- }
- // ACOT function calculates the arccotangent (i.e. the inverse cotangent) of a
- // given number, and returns an angle, in radians, between 0 and π. The syntax
- // of the function is:
- //
- // ACOT(number)
- //
- func (fn *formulaFuncs) ACOT(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ACOT requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Pi/2-math.Atan(val))
- return
- }
- // ACOTH function calculates the hyperbolic arccotangent (coth) of a supplied
- // value. The syntax of the function is:
- //
- // ACOTH(number)
- //
- func (fn *formulaFuncs) ACOTH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ACOTH requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Atanh(1/val))
- return
- }
- // ARABIC function converts a Roman numeral into an Arabic numeral. The syntax
- // of the function is:
- //
- // ARABIC(text)
- //
- func (fn *formulaFuncs) ARABIC(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ARABIC requires 1 numeric argument")
- return
- }
- val, last, prefix := 0.0, 0.0, 1.0
- for _, char := range argsList.Front().Value.(formulaArg).Value {
- digit := 0.0
- switch char {
- case '-':
- prefix = -1
- continue
- case 'I':
- digit = 1
- case 'V':
- digit = 5
- case 'X':
- digit = 10
- case 'L':
- digit = 50
- case 'C':
- digit = 100
- case 'D':
- digit = 500
- case 'M':
- digit = 1000
- }
- val += digit
- switch {
- case last == digit && (last == 5 || last == 50 || last == 500):
- result = formulaErrorVALUE
- return
- case 2*last == digit:
- result = formulaErrorVALUE
- return
- }
- if last < digit {
- val -= 2 * last
- }
- last = digit
- }
- result = fmt.Sprintf("%g", prefix*val)
- return
- }
- // ASIN function calculates the arcsine (i.e. the inverse sine) of a given
- // number, and returns an angle, in radians, between -π/2 and π/2. The syntax
- // of the function is:
- //
- // ASIN(number)
- //
- func (fn *formulaFuncs) ASIN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ASIN requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Asin(val))
- return
- }
- // ASINH function calculates the inverse hyperbolic sine of a supplied number.
- // The syntax of the function is:
- //
- // ASINH(number)
- //
- func (fn *formulaFuncs) ASINH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ASINH requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Asinh(val))
- return
- }
- // ATAN function calculates the arctangent (i.e. the inverse tangent) of a
- // given number, and returns an angle, in radians, between -π/2 and +π/2. The
- // syntax of the function is:
- //
- // ATAN(number)
- //
- func (fn *formulaFuncs) ATAN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ATAN requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Atan(val))
- return
- }
- // ATANH function calculates the inverse hyperbolic tangent of a supplied
- // number. The syntax of the function is:
- //
- // ATANH(number)
- //
- func (fn *formulaFuncs) ATANH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ATANH requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Atanh(val))
- return
- }
- // ATAN2 function calculates the arctangent (i.e. the inverse tangent) of a
- // given set of x and y coordinates, and returns an angle, in radians, between
- // -π/2 and +π/2. The syntax of the function is:
- //
- // ATAN2(x_num,y_num)
- //
- func (fn *formulaFuncs) ATAN2(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("ATAN2 requires 2 numeric arguments")
- return
- }
- var x, y float64
- if x, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if y, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Atan2(x, y))
- return
- }
- // gcd returns the greatest common divisor of two supplied integers.
- func gcd(x, y float64) float64 {
- x, y = math.Trunc(x), math.Trunc(y)
- if x == 0 {
- return y
- }
- if y == 0 {
- return x
- }
- for x != y {
- if x > y {
- x = x - y
- } else {
- y = y - x
- }
- }
- return x
- }
- // BASE function converts a number into a supplied base (radix), and returns a
- // text representation of the calculated value. The syntax of the function is:
- //
- // BASE(number,radix,[min_length])
- //
- func (fn *formulaFuncs) BASE(argsList *list.List) (result string, err error) {
- if argsList.Len() < 2 {
- err = errors.New("BASE requires at least 2 arguments")
- return
- }
- if argsList.Len() > 3 {
- err = errors.New("BASE allows at most 3 arguments")
- return
- }
- var number float64
- var radix, minLength int
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if radix, err = strconv.Atoi(argsList.Front().Next().Value.(formulaArg).Value); err != nil {
- return
- }
- if radix < 2 || radix > 36 {
- err = errors.New("radix must be an integer ≥ 2 and ≤ 36")
- return
- }
- if argsList.Len() > 2 {
- if minLength, err = strconv.Atoi(argsList.Back().Value.(formulaArg).Value); err != nil {
- return
- }
- }
- result = strconv.FormatInt(int64(number), radix)
- if len(result) < minLength {
- result = strings.Repeat("0", minLength-len(result)) + result
- }
- result = strings.ToUpper(result)
- return
- }
- // CEILING function rounds a supplied number away from zero, to the nearest
- // multiple of a given number. The syntax of the function is:
- //
- // CEILING(number,significance)
- //
- func (fn *formulaFuncs) CEILING(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("CEILING requires at least 1 argument")
- return
- }
- if argsList.Len() > 2 {
- err = errors.New("CEILING allows at most 2 arguments")
- return
- }
- var number, significance float64 = 0, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if number < 0 {
- significance = -1
- }
- if argsList.Len() > 1 {
- if significance, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- }
- if significance < 0 && number > 0 {
- err = errors.New("negative sig to CEILING invalid")
- return
- }
- if argsList.Len() == 1 {
- result = fmt.Sprintf("%g", math.Ceil(number))
- return
- }
- number, res := math.Modf(number / significance)
- if res > 0 {
- number++
- }
- result = fmt.Sprintf("%g", number*significance)
- return
- }
- // CEILINGMATH function rounds a supplied number up to a supplied multiple of
- // significance. The syntax of the function is:
- //
- // CEILING.MATH(number,[significance],[mode])
- //
- func (fn *formulaFuncs) CEILINGMATH(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("CEILING.MATH requires at least 1 argument")
- return
- }
- if argsList.Len() > 3 {
- err = errors.New("CEILING.MATH allows at most 3 arguments")
- return
- }
- var number, significance, mode float64 = 0, 1, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if number < 0 {
- significance = -1
- }
- if argsList.Len() > 1 {
- if significance, err = strconv.ParseFloat(argsList.Front().Next().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- }
- if argsList.Len() == 1 {
- result = fmt.Sprintf("%g", math.Ceil(number))
- return
- }
- if argsList.Len() > 2 {
- if mode, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- }
- val, res := math.Modf(number / significance)
- if res != 0 {
- if number > 0 {
- val++
- } else if mode < 0 {
- val--
- }
- }
- result = fmt.Sprintf("%g", val*significance)
- return
- }
- // CEILINGPRECISE function rounds a supplied number up (regardless of the
- // number's sign), to the nearest multiple of a given number. The syntax of
- // the function is:
- //
- // CEILING.PRECISE(number,[significance])
- //
- func (fn *formulaFuncs) CEILINGPRECISE(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("CEILING.PRECISE requires at least 1 argument")
- return
- }
- if argsList.Len() > 2 {
- err = errors.New("CEILING.PRECISE allows at most 2 arguments")
- return
- }
- var number, significance float64 = 0, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if number < 0 {
- significance = -1
- }
- if argsList.Len() == 1 {
- result = fmt.Sprintf("%g", math.Ceil(number))
- return
- }
- if argsList.Len() > 1 {
- if significance, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- significance = math.Abs(significance)
- if significance == 0 {
- result = "0"
- return
- }
- }
- val, res := math.Modf(number / significance)
- if res != 0 {
- if number > 0 {
- val++
- }
- }
- result = fmt.Sprintf("%g", val*significance)
- return
- }
- // COMBIN function calculates the number of combinations (in any order) of a
- // given number objects from a set. The syntax of the function is:
- //
- // COMBIN(number,number_chosen)
- //
- func (fn *formulaFuncs) COMBIN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("COMBIN requires 2 argument")
- return
- }
- var number, chosen, val float64 = 0, 0, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if chosen, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- number, chosen = math.Trunc(number), math.Trunc(chosen)
- if chosen > number {
- err = errors.New("COMBIN requires number >= number_chosen")
- return
- }
- if chosen == number || chosen == 0 {
- result = "1"
- return
- }
- for c := float64(1); c <= chosen; c++ {
- val *= (number + 1 - c) / c
- }
- result = fmt.Sprintf("%g", math.Ceil(val))
- return
- }
- // COMBINA function calculates the number of combinations, with repetitions,
- // of a given number objects from a set. The syntax of the function is:
- //
- // COMBINA(number,number_chosen)
- //
- func (fn *formulaFuncs) COMBINA(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("COMBINA requires 2 argument")
- return
- }
- var number, chosen float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if chosen, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- number, chosen = math.Trunc(number), math.Trunc(chosen)
- if number < chosen {
- err = errors.New("COMBINA requires number > number_chosen")
- return
- }
- if number == 0 {
- result = "0"
- return
- }
- args := list.New()
- args.PushBack(formulaArg{
- Value: fmt.Sprintf("%g", number+chosen-1),
- })
- args.PushBack(formulaArg{
- Value: fmt.Sprintf("%g", number-1),
- })
- return fn.COMBIN(args)
- }
- // COS function calculates the cosine of a given angle. The syntax of the
- // function is:
- //
- // COS(number)
- //
- func (fn *formulaFuncs) COS(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("COS requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Cos(val))
- return
- }
- // COSH function calculates the hyperbolic cosine (cosh) of a supplied number.
- // The syntax of the function is:
- //
- // COSH(number)
- //
- func (fn *formulaFuncs) COSH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("COSH requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Cosh(val))
- return
- }
- // COT function calculates the cotangent of a given angle. The syntax of the
- // function is:
- //
- // COT(number)
- //
- func (fn *formulaFuncs) COT(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("COT requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if val == 0 {
- err = errors.New(formulaErrorNAME)
- return
- }
- result = fmt.Sprintf("%g", math.Tan(val))
- return
- }
- // COTH function calculates the hyperbolic cotangent (coth) of a supplied
- // angle. The syntax of the function is:
- //
- // COTH(number)
- //
- func (fn *formulaFuncs) COTH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("COTH requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if val == 0 {
- err = errors.New(formulaErrorNAME)
- return
- }
- result = fmt.Sprintf("%g", math.Tanh(val))
- return
- }
- // CSC function calculates the cosecant of a given angle. The syntax of the
- // function is:
- //
- // CSC(number)
- //
- func (fn *formulaFuncs) CSC(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("CSC requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if val == 0 {
- err = errors.New(formulaErrorNAME)
- return
- }
- result = fmt.Sprintf("%g", 1/math.Sin(val))
- return
- }
- // CSCH function calculates the hyperbolic cosecant (csch) of a supplied
- // angle. The syntax of the function is:
- //
- // CSCH(number)
- //
- func (fn *formulaFuncs) CSCH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("CSCH requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if val == 0 {
- err = errors.New(formulaErrorNAME)
- return
- }
- result = fmt.Sprintf("%g", 1/math.Sinh(val))
- return
- }
- // DECIMAL function converts a text representation of a number in a specified
- // base, into a decimal value. The syntax of the function is:
- //
- // DECIMAL(text,radix)
- //
- func (fn *formulaFuncs) DECIMAL(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("DECIMAL requires 2 numeric arguments")
- return
- }
- var text = argsList.Front().Value.(formulaArg).Value
- var radix int
- if radix, err = strconv.Atoi(argsList.Back().Value.(formulaArg).Value); err != nil {
- return
- }
- if len(text) > 2 && (strings.HasPrefix(text, "0x") || strings.HasPrefix(text, "0X")) {
- text = text[2:]
- }
- val, err := strconv.ParseInt(text, radix, 64)
- if err != nil {
- err = errors.New(formulaErrorNUM)
- return
- }
- result = fmt.Sprintf("%g", float64(val))
- return
- }
- // DEGREES function converts radians into degrees. The syntax of the function
- // is:
- //
- // DEGREES(angle)
- //
- func (fn *formulaFuncs) DEGREES(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("DEGREES requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if val == 0 {
- err = errors.New(formulaErrorNAME)
- return
- }
- result = fmt.Sprintf("%g", 180.0/math.Pi*val)
- return
- }
- // EVEN function rounds a supplied number away from zero (i.e. rounds a
- // positive number up and a negative number down), to the next even number.
- // The syntax of the function is:
- //
- // EVEN(number)
- //
- func (fn *formulaFuncs) EVEN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("EVEN requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- sign := math.Signbit(number)
- m, frac := math.Modf(number / 2)
- val := m * 2
- if frac != 0 {
- if !sign {
- val += 2
- } else {
- val -= 2
- }
- }
- result = fmt.Sprintf("%g", val)
- return
- }
- // EXP function calculates the value of the mathematical constant e, raised to
- // the power of a given number. The syntax of the function is:
- //
- // EXP(number)
- //
- func (fn *formulaFuncs) EXP(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("EXP requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = strings.ToUpper(fmt.Sprintf("%g", math.Exp(number)))
- return
- }
- // fact returns the factorial of a supplied number.
- func fact(number float64) float64 {
- val := float64(1)
- for i := float64(2); i <= number; i++ {
- val *= i
- }
- return val
- }
- // FACT function returns the factorial of a supplied number. The syntax of the
- // function is:
- //
- // FACT(number)
- //
- func (fn *formulaFuncs) FACT(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("FACT requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if number < 0 {
- err = errors.New(formulaErrorNUM)
- }
- result = strings.ToUpper(fmt.Sprintf("%g", fact(number)))
- return
- }
- // FACTDOUBLE function returns the double factorial of a supplied number. The
- // syntax of the function is:
- //
- // FACTDOUBLE(number)
- //
- func (fn *formulaFuncs) FACTDOUBLE(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("FACTDOUBLE requires 1 numeric argument")
- return
- }
- var number, val float64 = 0, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if number < 0 {
- err = errors.New(formulaErrorNUM)
- }
- for i := math.Trunc(number); i > 1; i -= 2 {
- val *= i
- }
- result = strings.ToUpper(fmt.Sprintf("%g", val))
- return
- }
- // FLOOR function rounds a supplied number towards zero to the nearest
- // multiple of a specified significance. The syntax of the function is:
- //
- // FLOOR(number,significance)
- //
- func (fn *formulaFuncs) FLOOR(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("FLOOR requires 2 numeric arguments")
- return
- }
- var number, significance float64 = 0, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if significance, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if significance < 0 && number >= 0 {
- err = errors.New(formulaErrorNUM)
- }
- val := number
- val, res := math.Modf(val / significance)
- if res != 0 {
- if number < 0 && res < 0 {
- val--
- }
- }
- result = strings.ToUpper(fmt.Sprintf("%g", val*significance))
- return
- }
- // FLOORMATH function rounds a supplied number down to a supplied multiple of
- // significance. The syntax of the function is:
- //
- // FLOOR.MATH(number,[significance],[mode])
- //
- func (fn *formulaFuncs) FLOORMATH(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("FLOOR.MATH requires at least 1 argument")
- return
- }
- if argsList.Len() > 3 {
- err = errors.New("FLOOR.MATH allows at most 3 arguments")
- return
- }
- var number, significance, mode float64 = 0, 1, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if number < 0 {
- significance = -1
- }
- if argsList.Len() > 1 {
- if significance, err = strconv.ParseFloat(argsList.Front().Next().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- }
- if argsList.Len() == 1 {
- result = fmt.Sprintf("%g", math.Floor(number))
- return
- }
- if argsList.Len() > 2 {
- if mode, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- }
- val, res := math.Modf(number / significance)
- if res != 0 && number < 0 && mode > 0 {
- val--
- }
- result = fmt.Sprintf("%g", val*significance)
- return
- }
- // FLOORPRECISE function rounds a supplied number down to a supplied multiple
- // of significance. The syntax of the function is:
- //
- // FLOOR.PRECISE(number,[significance])
- //
- func (fn *formulaFuncs) FLOORPRECISE(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("FLOOR.PRECISE requires at least 1 argument")
- return
- }
- if argsList.Len() > 2 {
- err = errors.New("FLOOR.PRECISE allows at most 2 arguments")
- return
- }
- var number, significance float64 = 0, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if number < 0 {
- significance = -1
- }
- if argsList.Len() == 1 {
- result = fmt.Sprintf("%g", math.Floor(number))
- return
- }
- if argsList.Len() > 1 {
- if significance, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- significance = math.Abs(significance)
- if significance == 0 {
- result = "0"
- return
- }
- }
- val, res := math.Modf(number / significance)
- if res != 0 {
- if number < 0 {
- val--
- }
- }
- result = fmt.Sprintf("%g", val*significance)
- return
- }
- // GCD function returns the greatest common divisor of two or more supplied
- // integers. The syntax of the function is:
- //
- // GCD(number1,[number2],...)
- //
- func (fn *formulaFuncs) GCD(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("GCD requires at least 1 argument")
- return
- }
- var (
- val float64
- nums = []float64{}
- )
- for arg := argsList.Front(); arg != nil; arg = arg.Next() {
- token := arg.Value.(formulaArg).Value
- if token == "" {
- continue
- }
- if val, err = strconv.ParseFloat(token, 64); err != nil {
- return
- }
- nums = append(nums, val)
- }
- if nums[0] < 0 {
- err = errors.New("GCD only accepts positive arguments")
- return
- }
- if len(nums) == 1 {
- result = fmt.Sprintf("%g", nums[0])
- return
- }
- cd := nums[0]
- for i := 1; i < len(nums); i++ {
- if nums[i] < 0 {
- err = errors.New("GCD only accepts positive arguments")
- return
- }
- cd = gcd(cd, nums[i])
- }
- result = fmt.Sprintf("%g", cd)
- return
- }
- // INT function truncates a supplied number down to the closest integer. The
- // syntax of the function is:
- //
- // INT(number)
- //
- func (fn *formulaFuncs) INT(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("INT requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- val, frac := math.Modf(number)
- if frac < 0 {
- val--
- }
- result = fmt.Sprintf("%g", val)
- return
- }
- // ISOCEILING function rounds a supplied number up (regardless of the number's
- // sign), to the nearest multiple of a supplied significance. The syntax of
- // the function is:
- //
- // ISO.CEILING(number,[significance])
- //
- func (fn *formulaFuncs) ISOCEILING(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("ISO.CEILING requires at least 1 argument")
- return
- }
- if argsList.Len() > 2 {
- err = errors.New("ISO.CEILING allows at most 2 arguments")
- return
- }
- var number, significance float64 = 0, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if number < 0 {
- significance = -1
- }
- if argsList.Len() == 1 {
- result = fmt.Sprintf("%g", math.Ceil(number))
- return
- }
- if argsList.Len() > 1 {
- if significance, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- significance = math.Abs(significance)
- if significance == 0 {
- result = "0"
- return
- }
- }
- val, res := math.Modf(number / significance)
- if res != 0 {
- if number > 0 {
- val++
- }
- }
- result = fmt.Sprintf("%g", val*significance)
- return
- }
- // lcm returns the least common multiple of two supplied integers.
- func lcm(a, b float64) float64 {
- a = math.Trunc(a)
- b = math.Trunc(b)
- if a == 0 && b == 0 {
- return 0
- }
- return a * b / gcd(a, b)
- }
- // LCM function returns the least common multiple of two or more supplied
- // integers. The syntax of the function is:
- //
- // LCM(number1,[number2],...)
- //
- func (fn *formulaFuncs) LCM(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("LCM requires at least 1 argument")
- return
- }
- var (
- val float64
- nums = []float64{}
- )
- for arg := argsList.Front(); arg != nil; arg = arg.Next() {
- token := arg.Value.(formulaArg).Value
- if token == "" {
- continue
- }
- if val, err = strconv.ParseFloat(token, 64); err != nil {
- return
- }
- nums = append(nums, val)
- }
- if nums[0] < 0 {
- err = errors.New("LCM only accepts positive arguments")
- return
- }
- if len(nums) == 1 {
- result = fmt.Sprintf("%g", nums[0])
- return
- }
- cm := nums[0]
- for i := 1; i < len(nums); i++ {
- if nums[i] < 0 {
- err = errors.New("LCM only accepts positive arguments")
- return
- }
- cm = lcm(cm, nums[i])
- }
- result = fmt.Sprintf("%g", cm)
- return
- }
- // LN function calculates the natural logarithm of a given number. The syntax
- // of the function is:
- //
- // LN(number)
- //
- func (fn *formulaFuncs) LN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("LN requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Log(number))
- return
- }
- // LOG function calculates the logarithm of a given number, to a supplied
- // base. The syntax of the function is:
- //
- // LOG(number,[base])
- //
- func (fn *formulaFuncs) LOG(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("LOG requires at least 1 argument")
- return
- }
- if argsList.Len() > 2 {
- err = errors.New("LOG allows at most 2 arguments")
- return
- }
- var number, base float64 = 0, 10
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if argsList.Len() > 1 {
- if base, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- }
- if number == 0 {
- err = errors.New(formulaErrorNUM)
- return
- }
- if base == 0 {
- err = errors.New(formulaErrorNUM)
- return
- }
- if base == 1 {
- err = errors.New(formulaErrorDIV)
- return
- }
- result = fmt.Sprintf("%g", math.Log(number)/math.Log(base))
- return
- }
- // LOG10 function calculates the base 10 logarithm of a given number. The
- // syntax of the function is:
- //
- // LOG10(number)
- //
- func (fn *formulaFuncs) LOG10(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("LOG10 requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Log10(number))
- return
- }
- func minor(sqMtx [][]float64, idx int) [][]float64 {
- ret := [][]float64{}
- for i := range sqMtx {
- if i == 0 {
- continue
- }
- row := []float64{}
- for j := range sqMtx {
- if j == idx {
- continue
- }
- row = append(row, sqMtx[i][j])
- }
- ret = append(ret, row)
- }
- return ret
- }
- // det determinant of the 2x2 matrix.
- func det(sqMtx [][]float64) float64 {
- if len(sqMtx) == 2 {
- m00 := sqMtx[0][0]
- m01 := sqMtx[0][1]
- m10 := sqMtx[1][0]
- m11 := sqMtx[1][1]
- return m00*m11 - m10*m01
- }
- var res, sgn float64 = 0, 1
- for j := range sqMtx {
- res += sgn * sqMtx[0][j] * det(minor(sqMtx, j))
- sgn *= -1
- }
- return res
- }
- // MDETERM calculates the determinant of a square matrix. The
- // syntax of the function is:
- //
- // MDETERM(array)
- //
- func (fn *formulaFuncs) MDETERM(argsList *list.List) (result string, err error) {
- var num float64
- var rows int
- var numMtx = [][]float64{}
- var strMtx = [][]string{}
- for arg := argsList.Front(); arg != nil; arg = arg.Next() {
- if len(arg.Value.(formulaArg).Matrix) == 0 {
- break
- }
- strMtx = append(strMtx, arg.Value.(formulaArg).Matrix)
- rows++
- }
- for _, row := range strMtx {
- if len(row) != rows {
- err = errors.New(formulaErrorVALUE)
- return
- }
- numRow := []float64{}
- for _, ele := range row {
- if num, err = strconv.ParseFloat(ele, 64); err != nil {
- return
- }
- numRow = append(numRow, num)
- }
- numMtx = append(numMtx, numRow)
- }
- result = fmt.Sprintf("%g", det(numMtx))
- return
- }
- // MOD function returns the remainder of a division between two supplied
- // numbers. The syntax of the function is:
- //
- // MOD(number,divisor)
- //
- func (fn *formulaFuncs) MOD(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("MOD requires 2 numeric arguments")
- return
- }
- var number, divisor float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if divisor, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if divisor == 0 {
- err = errors.New(formulaErrorDIV)
- return
- }
- trunc, rem := math.Modf(number / divisor)
- if rem < 0 {
- trunc--
- }
- result = fmt.Sprintf("%g", number-divisor*trunc)
- return
- }
- // MROUND function rounds a supplied number up or down to the nearest multiple
- // of a given number. The syntax of the function is:
- //
- // MOD(number,multiple)
- //
- func (fn *formulaFuncs) MROUND(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("MROUND requires 2 numeric arguments")
- return
- }
- var number, multiple float64 = 0, 1
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if multiple, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if multiple == 0 {
- err = errors.New(formulaErrorNUM)
- return
- }
- if multiple < 0 && number > 0 ||
- multiple > 0 && number < 0 {
- err = errors.New(formulaErrorNUM)
- return
- }
- number, res := math.Modf(number / multiple)
- if math.Trunc(res+0.5) > 0 {
- number++
- }
- result = fmt.Sprintf("%g", number*multiple)
- return
- }
- // MULTINOMIAL function calculates the ratio of the factorial of a sum of
- // supplied values to the product of factorials of those values. The syntax of
- // the function is:
- //
- // MULTINOMIAL(number1,[number2],...)
- //
- func (fn *formulaFuncs) MULTINOMIAL(argsList *list.List) (result string, err error) {
- var val, num, denom float64 = 0, 0, 1
- for arg := argsList.Front(); arg != nil; arg = arg.Next() {
- token := arg.Value.(formulaArg)
- if token.Value == "" {
- continue
- }
- if val, err = strconv.ParseFloat(token.Value, 64); err != nil {
- return
- }
- num += val
- denom *= fact(val)
- }
- result = fmt.Sprintf("%g", fact(num)/denom)
- return
- }
- // MUNIT function returns the unit matrix for a specified dimension. The
- // syntax of the function is:
- //
- // MUNIT(dimension)
- //
- func (fn *formulaFuncs) MUNIT(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("MUNIT requires 1 numeric argument")
- return
- }
- var dimension int
- if dimension, err = strconv.Atoi(argsList.Front().Value.(formulaArg).Value); err != nil {
- return
- }
- matrix := make([][]float64, 0, dimension)
- for i := 0; i < dimension; i++ {
- row := make([]float64, dimension)
- for j := 0; j < dimension; j++ {
- if i == j {
- row[j] = float64(1.0)
- } else {
- row[j] = float64(0.0)
- }
- }
- matrix = append(matrix, row)
- }
- return
- }
- // ODD function ounds a supplied number away from zero (i.e. rounds a positive
- // number up and a negative number down), to the next odd number. The syntax
- // of the function is:
- //
- // ODD(number)
- //
- func (fn *formulaFuncs) ODD(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("ODD requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if number == 0 {
- result = "1"
- return
- }
- sign := math.Signbit(number)
- m, frac := math.Modf((number - 1) / 2)
- val := m*2 + 1
- if frac != 0 {
- if !sign {
- val += 2
- } else {
- val -= 2
- }
- }
- result = fmt.Sprintf("%g", val)
- return
- }
- // PI function returns the value of the mathematical constant π (pi), accurate
- // to 15 digits (14 decimal places). The syntax of the function is:
- //
- // PI()
- //
- func (fn *formulaFuncs) PI(argsList *list.List) (result string, err error) {
- if argsList.Len() != 0 {
- err = errors.New("PI accepts no arguments")
- return
- }
- result = fmt.Sprintf("%g", math.Pi)
- return
- }
- // POWER function calculates a given number, raised to a supplied power.
- // The syntax of the function is:
- //
- // POWER(number,power)
- //
- func (fn *formulaFuncs) POWER(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("POWER requires 2 numeric arguments")
- return
- }
- var x, y float64
- if x, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if y, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if x == 0 && y == 0 {
- err = errors.New(formulaErrorNUM)
- return
- }
- if x == 0 && y < 0 {
- err = errors.New(formulaErrorDIV)
- return
- }
- result = fmt.Sprintf("%g", math.Pow(x, y))
- return
- }
- // PRODUCT function returns the product (multiplication) of a supplied set of
- // numerical values. The syntax of the function is:
- //
- // PRODUCT(number1,[number2],...)
- //
- func (fn *formulaFuncs) PRODUCT(argsList *list.List) (result string, err error) {
- var val, product float64 = 0, 1
- for arg := argsList.Front(); arg != nil; arg = arg.Next() {
- token := arg.Value.(formulaArg)
- if token.Value == "" {
- continue
- }
- if val, err = strconv.ParseFloat(token.Value, 64); err != nil {
- return
- }
- product = product * val
- }
- result = fmt.Sprintf("%g", product)
- return
- }
- // QUOTIENT function returns the integer portion of a division between two
- // supplied numbers. The syntax of the function is:
- //
- // QUOTIENT(numerator,denominator)
- //
- func (fn *formulaFuncs) QUOTIENT(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("QUOTIENT requires 2 numeric arguments")
- return
- }
- var x, y float64
- if x, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if y, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if y == 0 {
- err = errors.New(formulaErrorDIV)
- return
- }
- result = fmt.Sprintf("%g", math.Trunc(x/y))
- return
- }
- // RADIANS function converts radians into degrees. The syntax of the function is:
- //
- // RADIANS(angle)
- //
- func (fn *formulaFuncs) RADIANS(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("RADIANS requires 1 numeric argument")
- return
- }
- var angle float64
- if angle, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Pi/180.0*angle)
- return
- }
- // RAND function generates a random real number between 0 and 1. The syntax of
- // the function is:
- //
- // RAND()
- //
- func (fn *formulaFuncs) RAND(argsList *list.List) (result string, err error) {
- if argsList.Len() != 0 {
- err = errors.New("RAND accepts no arguments")
- return
- }
- result = fmt.Sprintf("%g", rand.New(rand.NewSource(time.Now().UnixNano())).Float64())
- return
- }
- // RANDBETWEEN function generates a random integer between two supplied
- // integers. The syntax of the function is:
- //
- // RANDBETWEEN(bottom,top)
- //
- func (fn *formulaFuncs) RANDBETWEEN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("RANDBETWEEN requires 2 numeric arguments")
- return
- }
- var bottom, top int64
- if bottom, err = strconv.ParseInt(argsList.Front().Value.(formulaArg).Value, 10, 64); err != nil {
- return
- }
- if top, err = strconv.ParseInt(argsList.Back().Value.(formulaArg).Value, 10, 64); err != nil {
- return
- }
- if top < bottom {
- err = errors.New(formulaErrorNUM)
- return
- }
- result = fmt.Sprintf("%g", float64(rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(top-bottom+1)+bottom))
- return
- }
- // romanNumerals defined a numeral system that originated in ancient Rome and
- // remained the usual way of writing numbers throughout Europe well into the
- // Late Middle Ages.
- type romanNumerals struct {
- n float64
- s string
- }
- var romanTable = [][]romanNumerals{{{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}},
- {{1000, "M"}, {950, "LM"}, {900, "CM"}, {500, "D"}, {450, "LD"}, {400, "CD"}, {100, "C"}, {95, "VC"}, {90, "XC"}, {50, "L"}, {45, "VL"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}},
- {{1000, "M"}, {990, "XM"}, {950, "LM"}, {900, "CM"}, {500, "D"}, {490, "XD"}, {450, "LD"}, {400, "CD"}, {100, "C"}, {99, "IC"}, {90, "XC"}, {50, "L"}, {45, "VL"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}},
- {{1000, "M"}, {995, "VM"}, {990, "XM"}, {950, "LM"}, {900, "CM"}, {500, "D"}, {495, "VD"}, {490, "XD"}, {450, "LD"}, {400, "CD"}, {100, "C"}, {99, "IC"}, {90, "XC"}, {50, "L"}, {45, "VL"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}},
- {{1000, "M"}, {999, "IM"}, {995, "VM"}, {990, "XM"}, {950, "LM"}, {900, "CM"}, {500, "D"}, {499, "ID"}, {495, "VD"}, {490, "XD"}, {450, "LD"}, {400, "CD"}, {100, "C"}, {99, "IC"}, {90, "XC"}, {50, "L"}, {45, "VL"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}}}
- // ROMAN function converts an arabic number to Roman. I.e. for a supplied
- // integer, the function returns a text string depicting the roman numeral
- // form of the number. The syntax of the function is:
- //
- // ROMAN(number,[form])
- //
- func (fn *formulaFuncs) ROMAN(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("ROMAN requires at least 1 argument")
- return
- }
- if argsList.Len() > 2 {
- err = errors.New("ROMAN allows at most 2 arguments")
- return
- }
- var number float64
- var form int
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if argsList.Len() > 1 {
- if form, err = strconv.Atoi(argsList.Back().Value.(formulaArg).Value); err != nil {
- return
- }
- if form < 0 {
- form = 0
- } else if form > 4 {
- form = 4
- }
- }
- decimalTable := romanTable[0]
- switch form {
- case 1:
- decimalTable = romanTable[1]
- case 2:
- decimalTable = romanTable[2]
- case 3:
- decimalTable = romanTable[3]
- case 4:
- decimalTable = romanTable[4]
- }
- val := math.Trunc(number)
- buf := bytes.Buffer{}
- for _, r := range decimalTable {
- for val >= r.n {
- buf.WriteString(r.s)
- val -= r.n
- }
- }
- result = buf.String()
- return
- }
- type roundMode byte
- const (
- closest roundMode = iota
- down
- up
- )
- // round rounds a supplied number up or down.
- func (fn *formulaFuncs) round(number, digits float64, mode roundMode) float64 {
- significance := 1.0
- if digits > 0 {
- significance = math.Pow(1/10.0, digits)
- } else {
- significance = math.Pow(10.0, -digits)
- }
- val, res := math.Modf(number / significance)
- switch mode {
- case closest:
- const eps = 0.499999999
- if res >= eps {
- val++
- } else if res <= -eps {
- val--
- }
- case down:
- case up:
- if res > 0 {
- val++
- } else if res < 0 {
- val--
- }
- }
- return val * significance
- }
- // ROUND function rounds a supplied number up or down, to a specified number
- // of decimal places. The syntax of the function is:
- //
- // ROUND(number,num_digits)
- //
- func (fn *formulaFuncs) ROUND(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("ROUND requires 2 numeric arguments")
- return
- }
- var number, digits float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if digits, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", fn.round(number, digits, closest))
- return
- }
- // ROUNDDOWN function rounds a supplied number down towards zero, to a
- // specified number of decimal places. The syntax of the function is:
- //
- // ROUNDDOWN(number,num_digits)
- //
- func (fn *formulaFuncs) ROUNDDOWN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("ROUNDDOWN requires 2 numeric arguments")
- return
- }
- var number, digits float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if digits, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", fn.round(number, digits, down))
- return
- }
- // ROUNDUP function rounds a supplied number up, away from zero, to a
- // specified number of decimal places. The syntax of the function is:
- //
- // ROUNDUP(number,num_digits)
- //
- func (fn *formulaFuncs) ROUNDUP(argsList *list.List) (result string, err error) {
- if argsList.Len() != 2 {
- err = errors.New("ROUNDUP requires 2 numeric arguments")
- return
- }
- var number, digits float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if digits, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", fn.round(number, digits, up))
- return
- }
- // SEC function calculates the secant of a given angle. The syntax of the
- // function is:
- //
- // SEC(number)
- //
- func (fn *formulaFuncs) SEC(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("SEC requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Cos(number))
- return
- }
- // SECH function calculates the hyperbolic secant (sech) of a supplied angle.
- // The syntax of the function is:
- //
- // SECH(number)
- //
- func (fn *formulaFuncs) SECH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("SECH requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", 1/math.Cosh(number))
- return
- }
- // SIGN function returns the arithmetic sign (+1, -1 or 0) of a supplied
- // number. I.e. if the number is positive, the Sign function returns +1, if
- // the number is negative, the function returns -1 and if the number is 0
- // (zero), the function returns 0. The syntax of the function is:
- //
- // SIGN(number)
- //
- func (fn *formulaFuncs) SIGN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("SIGN requires 1 numeric argument")
- return
- }
- var val float64
- if val, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if val < 0 {
- result = "-1"
- return
- }
- if val > 0 {
- result = "1"
- return
- }
- result = "0"
- return
- }
- // SIN function calculates the sine of a given angle. The syntax of the
- // function is:
- //
- // SIN(number)
- //
- func (fn *formulaFuncs) SIN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("SIN requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Sin(number))
- return
- }
- // SINH function calculates the hyperbolic sine (sinh) of a supplied number.
- // The syntax of the function is:
- //
- // SINH(number)
- //
- func (fn *formulaFuncs) SINH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("SINH requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Sinh(number))
- return
- }
- // SQRT function calculates the positive square root of a supplied number. The
- // syntax of the function is:
- //
- // SQRT(number)
- //
- func (fn *formulaFuncs) SQRT(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("SQRT requires 1 numeric argument")
- return
- }
- var res float64
- var value = argsList.Front().Value.(formulaArg).Value
- if value == "" {
- result = "0"
- return
- }
- if res, err = strconv.ParseFloat(value, 64); err != nil {
- return
- }
- if res < 0 {
- err = errors.New(formulaErrorNUM)
- return
- }
- result = fmt.Sprintf("%g", math.Sqrt(res))
- return
- }
- // SQRTPI function returns the square root of a supplied number multiplied by
- // the mathematical constant, π. The syntax of the function is:
- //
- // SQRTPI(number)
- //
- func (fn *formulaFuncs) SQRTPI(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("SQRTPI requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Sqrt(number*math.Pi))
- return
- }
- // SUM function adds together a supplied set of numbers and returns the sum of
- // these values. The syntax of the function is:
- //
- // SUM(number1,[number2],...)
- //
- func (fn *formulaFuncs) SUM(argsList *list.List) (result string, err error) {
- var val, sum float64
- for arg := argsList.Front(); arg != nil; arg = arg.Next() {
- token := arg.Value.(formulaArg)
- if token.Value == "" {
- continue
- }
- if val, err = strconv.ParseFloat(token.Value, 64); err != nil {
- return
- }
- sum += val
- }
- result = fmt.Sprintf("%g", sum)
- return
- }
- // TAN function calculates the tangent of a given angle. The syntax of the
- // function is:
- //
- // TAN(number)
- //
- func (fn *formulaFuncs) TAN(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("TAN requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Tan(number))
- return
- }
- // TANH function calculates the hyperbolic tangent (tanh) of a supplied
- // number. The syntax of the function is:
- //
- // TANH(number)
- //
- func (fn *formulaFuncs) TANH(argsList *list.List) (result string, err error) {
- if argsList.Len() != 1 {
- err = errors.New("TANH requires 1 numeric argument")
- return
- }
- var number float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- result = fmt.Sprintf("%g", math.Tanh(number))
- return
- }
- // TRUNC function truncates a supplied number to a specified number of decimal
- // places. The syntax of the function is:
- //
- // TRUNC(number,[number_digits])
- //
- func (fn *formulaFuncs) TRUNC(argsList *list.List) (result string, err error) {
- if argsList.Len() == 0 {
- err = errors.New("TRUNC requires at least 1 argument")
- return
- }
- var number, digits, adjust, rtrim float64
- if number, err = strconv.ParseFloat(argsList.Front().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- if argsList.Len() > 1 {
- if digits, err = strconv.ParseFloat(argsList.Back().Value.(formulaArg).Value, 64); err != nil {
- return
- }
- digits = math.Floor(digits)
- }
- adjust = math.Pow(10, digits)
- x := int((math.Abs(number) - math.Abs(float64(int(number)))) * adjust)
- if x != 0 {
- if rtrim, err = strconv.ParseFloat(strings.TrimRight(strconv.Itoa(x), "0"), 64); err != nil {
- return
- }
- }
- if (digits > 0) && (rtrim < adjust/10) {
- result = fmt.Sprintf("%g", number)
- return
- }
- result = fmt.Sprintf("%g", float64(int(number*adjust))/adjust)
- return
- }
|