mirror of
https://codeberg.org/scip/rpnc.git
synced 2025-12-16 20:11:02 +01:00
fix printing of fractionals (not scientific anymore), added -p flag
This commit is contained in:
19
calc.go
19
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
|
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
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -20,6 +20,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -35,6 +36,8 @@ type Calc struct {
|
|||||||
showstack bool
|
showstack bool
|
||||||
intermediate bool
|
intermediate bool
|
||||||
notdone bool // set to true as long as there are items left in the eval loop
|
notdone bool // set to true as long as there are items left in the eval loop
|
||||||
|
precision int
|
||||||
|
|
||||||
stack *Stack
|
stack *Stack
|
||||||
history []string
|
history []string
|
||||||
completer readline.AutoCompleter
|
completer readline.AutoCompleter
|
||||||
@@ -91,6 +94,7 @@ Register variables:
|
|||||||
const (
|
const (
|
||||||
//Commands string = `dump reverse clear shift undo help history manual exit quit swap debug undebug nodebug batch nobatch showstack noshowstack vars`
|
//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`
|
Constants string = `Pi Phi Sqrt2 SqrtE SqrtPi SqrtPhi Ln2 Log2E Ln10 Log10E`
|
||||||
|
Precision int = 2
|
||||||
)
|
)
|
||||||
|
|
||||||
// That way we can add custom functions to completion
|
// That way we can add custom functions to completion
|
||||||
@@ -150,7 +154,7 @@ func (c *Calc) GetCompleteCustomFuncalls() func(string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewCalc() *Calc {
|
func NewCalc() *Calc {
|
||||||
c := Calc{stack: NewStack(), debug: false}
|
c := Calc{stack: NewStack(), debug: false, precision: Precision}
|
||||||
|
|
||||||
c.Funcalls = DefineFunctions()
|
c.Funcalls = DefineFunctions()
|
||||||
c.BatchFuncalls = DefineBatchFunctions()
|
c.BatchFuncalls = DefineBatchFunctions()
|
||||||
@@ -438,7 +442,16 @@ func (c *Calc) Result() float64 {
|
|||||||
fmt.Print("= ")
|
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]
|
return c.stack.Last()[0]
|
||||||
|
|||||||
24
main.go
24
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
|
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
|
it under the terms of the GNU General Public License as published by
|
||||||
@@ -30,26 +30,27 @@ import (
|
|||||||
lua "github.com/yuin/gopher-lua"
|
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.
|
const Usage string = `This is rpn, a reverse polish notation calculator cli.
|
||||||
|
|
||||||
Usage: rpn [-bdvh] [<operator>]
|
Usage: rpn [-bdvh] [<operator>]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-b, --batchmode enable batch mode
|
-b, --batchmode enable batch mode
|
||||||
-d, --debug enable debug mode
|
-d, --debug enable debug mode
|
||||||
-s, --stack show last 5 items of the stack (off by default)
|
-s, --stack show last 5 items of the stack (off by default)
|
||||||
-i --intermediate print intermediate results
|
-i --intermediate print intermediate results
|
||||||
-m, --manual show manual
|
-m, --manual show manual
|
||||||
-c, --config <file> load <file> containing LUA code
|
-c, --config <file> load <file> containing LUA code
|
||||||
-v, --version show version
|
-p, --precision <int> floating point number precision (default 2)
|
||||||
-h, --help show help
|
-v, --version show version
|
||||||
|
-h, --help show help
|
||||||
|
|
||||||
When <operator> is given, batch mode ist automatically enabled. Use
|
When <operator> is given, batch mode ist automatically enabled. Use
|
||||||
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
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() {
|
func main() {
|
||||||
os.Exit(Main())
|
os.Exit(Main())
|
||||||
@@ -74,6 +75,7 @@ func Main() int {
|
|||||||
flag.BoolVarP(&showmanual, "manual", "m", false, "show manual")
|
flag.BoolVarP(&showmanual, "manual", "m", false, "show manual")
|
||||||
flag.StringVarP(&configfile, "config", "c",
|
flag.StringVarP(&configfile, "config", "c",
|
||||||
os.Getenv("HOME")+"/.rpn.lua", "config file (lua format)")
|
os.Getenv("HOME")+"/.rpn.lua", "config file (lua format)")
|
||||||
|
flag.IntVarP(&calc.precision, "precision", "p", Precision, "floating point precision")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
|||||||
27
rpn.go
27
rpn.go
@@ -8,13 +8,15 @@ SYNOPSIS
|
|||||||
Usage: rpn [-bdvh] [<operator>]
|
Usage: rpn [-bdvh] [<operator>]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-b, --batchmode enable batch mode
|
-b, --batchmode enable batch mode
|
||||||
-d, --debug enable debug mode
|
-d, --debug enable debug mode
|
||||||
-s, --stack show last 5 items of the stack (off by default)
|
-s, --stack show last 5 items of the stack (off by default)
|
||||||
-i --intermediate print intermediate results
|
-i --intermediate print intermediate results
|
||||||
-m, --manual show manual
|
-m, --manual show manual
|
||||||
-v, --version show version
|
-c, --config <file> load <file> containing LUA code
|
||||||
-h, --help show help
|
-p, --precision <int> floating point number precision (default 2)
|
||||||
|
-v, --version show version
|
||||||
|
-h, --help show help
|
||||||
|
|
||||||
When <operator> is given, batch mode ist automatically enabled. Use
|
When <operator> is given, batch mode ist automatically enabled. Use
|
||||||
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
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
|
So you can't open files, execute other programs or open a connection to
|
||||||
the outside!
|
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
|
GETTING HELP
|
||||||
In interactive mode you can enter the help command (or ?) to get a short
|
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.
|
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
|
This software is licensed under the GNU GENERAL PUBLIC LICENSE version
|
||||||
3.
|
3.
|
||||||
|
|
||||||
Copyright (c) 2023 by Thomas von Dein
|
Copyright (c) 2023-2024 by Thomas von Dein
|
||||||
|
|
||||||
This software uses the following GO modules:
|
This software uses the following GO modules:
|
||||||
|
|
||||||
|
|||||||
28
rpn.pod
28
rpn.pod
@@ -7,13 +7,15 @@ rpn - Programmable command-line calculator using reverse polish notation
|
|||||||
Usage: rpn [-bdvh] [<operator>]
|
Usage: rpn [-bdvh] [<operator>]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
-b, --batchmode enable batch mode
|
-b, --batchmode enable batch mode
|
||||||
-d, --debug enable debug mode
|
-d, --debug enable debug mode
|
||||||
-s, --stack show last 5 items of the stack (off by default)
|
-s, --stack show last 5 items of the stack (off by default)
|
||||||
-i --intermediate print intermediate results
|
-i --intermediate print intermediate results
|
||||||
-m, --manual show manual
|
-m, --manual show manual
|
||||||
-v, --version show version
|
-c, --config <file> load <file> containing LUA code
|
||||||
-h, --help show help
|
-p, --precision <int> floating point number precision (default 2)
|
||||||
|
-v, --version show version
|
||||||
|
-h, --help show help
|
||||||
|
|
||||||
When <operator> is given, batch mode ist automatically enabled. Use
|
When <operator> is given, batch mode ist automatically enabled. Use
|
||||||
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
|
||||||
@@ -342,6 +344,16 @@ B<Please note, that io, networking and system stuff is not allowed
|
|||||||
though. So you can't open files, execute other programs or open a
|
though. So you can't open files, execute other programs or open a
|
||||||
connection to the outside!>
|
connection to the outside!>
|
||||||
|
|
||||||
|
=head1 CONFIGURATION
|
||||||
|
|
||||||
|
B<rpn> 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<batch> 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
|
=head1 GETTING HELP
|
||||||
|
|
||||||
In interactive mode you can enter the B<help> command (or B<?>) to get
|
In interactive mode you can enter the B<help> command (or B<?>) to get
|
||||||
@@ -364,7 +376,7 @@ L<https://github.com/TLINDEN/rpnc/issues>.
|
|||||||
|
|
||||||
This software is licensed under the GNU GENERAL PUBLIC LICENSE version 3.
|
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:
|
This software uses the following GO modules:
|
||||||
|
|
||||||
|
|||||||
2
t/cmdline-precision.txtar
Normal file
2
t/cmdline-precision.txtar
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
exec testrpn -p 4 2 3 /
|
||||||
|
stdout '0.6667\n'
|
||||||
Reference in New Issue
Block a user