Skip to content

Commit

Permalink
fix printing of fractionals (not scientific anymore), added -p flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas von Dein authored and TLINDEN committed Jan 26, 2024
1 parent e81be12 commit 6a2a501
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 30 deletions.
19 changes: 16 additions & 3 deletions calc.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +20,7 @@ package main
import (
"errors"
"fmt"
"math"
"regexp"
"sort"
"strconv"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]
Expand Down
24 changes: 13 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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] [<operator>]
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 <file> load <file> 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 <file> load <file> containing LUA code
-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
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())
Expand All @@ -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()

Expand Down
27 changes: 19 additions & 8 deletions rpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ SYNOPSIS
Usage: rpn [-bdvh] [<operator>]
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 <file> load <file> containing LUA code
-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
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down
28 changes: 20 additions & 8 deletions rpn.pod
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ rpn - Programmable command-line calculator using reverse polish notation
Usage: rpn [-bdvh] [<operator>]

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 <file> load <file> containing LUA code
-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
this only when working with stdin. E.g.: echo "2 3 4 5" | rpn +
Expand Down Expand Up @@ -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
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

In interactive mode you can enter the B<help> command (or B<?>) to get
Expand All @@ -364,7 +376,7 @@ L<https://github.com/TLINDEN/rpnc/issues>.

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:

Expand Down
2 changes: 2 additions & 0 deletions t/cmdline-precision.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exec testrpn -p 4 2 3 /
stdout '0.6667\n'

0 comments on commit 6a2a501

Please sign in to comment.