diff --git a/calc.go b/calc.go index 567ba04..6c8a198 100644 --- a/calc.go +++ b/calc.go @@ -1,5 +1,5 @@ /* -Copyright © 2023 Thomas von Dein +Copyright © 2023-2024 Thomas von Dein This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ package main import ( "errors" "fmt" + "math" "regexp" "sort" "strconv" @@ -35,6 +36,8 @@ type Calc struct { showstack bool intermediate bool notdone bool // set to true as long as there are items left in the eval loop + precision int + stack *Stack history []string completer readline.AutoCompleter @@ -91,6 +94,7 @@ Register variables: const ( //Commands string = `dump reverse clear shift undo help history manual exit quit swap debug undebug nodebug batch nobatch showstack noshowstack vars` Constants string = `Pi Phi Sqrt2 SqrtE SqrtPi SqrtPhi Ln2 Log2E Ln10 Log10E` + Precision int = 2 ) // That way we can add custom functions to completion @@ -150,7 +154,7 @@ func (c *Calc) GetCompleteCustomFuncalls() func(string) []string { } func NewCalc() *Calc { - c := Calc{stack: NewStack(), debug: false} + c := Calc{stack: NewStack(), debug: false, precision: Precision} c.Funcalls = DefineFunctions() c.BatchFuncalls = DefineBatchFunctions() @@ -438,7 +442,16 @@ func (c *Calc) Result() float64 { fmt.Print("= ") } - fmt.Println(c.stack.Last()[0]) + result := c.stack.Last()[0] + truncated := math.Trunc(result) + precision := c.precision + + if result == truncated { + precision = 0 + } + + format := fmt.Sprintf("%%.%df\n", precision) + fmt.Printf(format, result) } return c.stack.Last()[0] diff --git a/main.go b/main.go index 825d280..28cf9e9 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,5 @@ /* -Copyright © 2023 Thomas von Dein +Copyright © 2023-2024 Thomas von Dein This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -30,26 +30,27 @@ import ( lua "github.com/yuin/gopher-lua" ) -const VERSION string = "2.0.13" +const VERSION string = "2.1.0" const Usage string = `This is rpn, a reverse polish notation calculator cli. Usage: rpn [-bdvh] [] Options: - -b, --batchmode enable batch mode - -d, --debug enable debug mode - -s, --stack show last 5 items of the stack (off by default) - -i --intermediate print intermediate results - -m, --manual show manual - -c, --config load containing LUA code - -v, --version show version - -h, --help show help + -b, --batchmode enable batch mode + -d, --debug enable debug mode + -s, --stack show last 5 items of the stack (off by default) + -i --intermediate print intermediate results + -m, --manual show manual + -c, --config load containing LUA code + -p, --precision floating point number precision (default 2) + -v, --version show version + -h, --help show help When is given, batch mode ist automatically enabled. Use this only when working with stdin. E.g.: echo "2 3 4 5" | rpn + -Copyright (c) 2023 T.v.Dein` +Copyright (c) 2023-2024 T.v.Dein` func main() { os.Exit(Main()) @@ -74,6 +75,7 @@ func Main() int { flag.BoolVarP(&showmanual, "manual", "m", false, "show manual") flag.StringVarP(&configfile, "config", "c", os.Getenv("HOME")+"/.rpn.lua", "config file (lua format)") + flag.IntVarP(&calc.precision, "precision", "p", Precision, "floating point precision") flag.Parse() diff --git a/rpn.go b/rpn.go index b02a9dd..e9ccd6b 100644 --- a/rpn.go +++ b/rpn.go @@ -8,13 +8,15 @@ SYNOPSIS Usage: rpn [-bdvh] [] Options: - -b, --batchmode enable batch mode - -d, --debug enable debug mode - -s, --stack show last 5 items of the stack (off by default) - -i --intermediate print intermediate results - -m, --manual show manual - -v, --version show version - -h, --help show help + -b, --batchmode enable batch mode + -d, --debug enable debug mode + -s, --stack show last 5 items of the stack (off by default) + -i --intermediate print intermediate results + -m, --manual show manual + -c, --config load containing LUA code + -p, --precision floating point number precision (default 2) + -v, --version show version + -h, --help show help When is given, batch mode ist automatically enabled. Use this only when working with stdin. E.g.: echo "2 3 4 5" | rpn + @@ -309,6 +311,15 @@ EXTENDING RPN USING LUA So you can't open files, execute other programs or open a connection to the outside! +CONFIGURATION + rpn can be configured via command line flags (see usage above). Most of + the flags are also available as interactive commands, such as "--batch" + has the same effect as the batch command. + + The floating point number precision option "-p, --precision" however is + not available as interactive command, it MUST be configured on the + command line, if needed. The default precision is 2. + GETTING HELP In interactive mode you can enter the help command (or ?) to get a short help along with a list of all supported operators and functions. @@ -328,7 +339,7 @@ LICENSE This software is licensed under the GNU GENERAL PUBLIC LICENSE version 3. - Copyright (c) 2023 by Thomas von Dein + Copyright (c) 2023-2024 by Thomas von Dein This software uses the following GO modules: diff --git a/rpn.pod b/rpn.pod index 71a3591..f03c0cc 100644 --- a/rpn.pod +++ b/rpn.pod @@ -7,13 +7,15 @@ rpn - Programmable command-line calculator using reverse polish notation Usage: rpn [-bdvh] [] Options: - -b, --batchmode enable batch mode - -d, --debug enable debug mode - -s, --stack show last 5 items of the stack (off by default) - -i --intermediate print intermediate results - -m, --manual show manual - -v, --version show version - -h, --help show help + -b, --batchmode enable batch mode + -d, --debug enable debug mode + -s, --stack show last 5 items of the stack (off by default) + -i --intermediate print intermediate results + -m, --manual show manual + -c, --config load containing LUA code + -p, --precision floating point number precision (default 2) + -v, --version show version + -h, --help show help When is given, batch mode ist automatically enabled. Use this only when working with stdin. E.g.: echo "2 3 4 5" | rpn + @@ -342,6 +344,16 @@ B +=head1 CONFIGURATION + +B can be configured via command line flags (see usage +above). Most of the flags are also available as interactive commands, +such as C<--batch> has the same effect as the B command. + +The floating point number precision option C<-p, --precision> however +is not available as interactive command, it MUST be configured on the +command line, if needed. The default precision is 2. + =head1 GETTING HELP In interactive mode you can enter the B command (or B) to get @@ -364,7 +376,7 @@ L. This software is licensed under the GNU GENERAL PUBLIC LICENSE version 3. -Copyright (c) 2023 by Thomas von Dein +Copyright (c) 2023-2024 by Thomas von Dein This software uses the following GO modules: diff --git a/t/cmdline-precision.txtar b/t/cmdline-precision.txtar new file mode 100644 index 0000000..4be06fe --- /dev/null +++ b/t/cmdline-precision.txtar @@ -0,0 +1,2 @@ +exec testrpn -p 4 2 3 / +stdout '0.6667\n'