Skip to content

Commit

Permalink
Added '@' and '$' functions(RNG and INT function)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceticamarco committed Mar 14, 2024
1 parent 0e15856 commit 088fbbe
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 52 deletions.
119 changes: 119 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Some of the supported features are:
- Trigonometrical functions(`sin`, `cos`, `tan`, `asin`, `acos`, `atan`);
- Base conversion(binary: `pb`, octal: `po`, hexadecimal: `px`);
- Factorial and constants(`!`, `pi`, `e`);
- Random number generator(`@`);
- Integer conversion(`$`);
- Stack operations:
- Print top element(`p`, `P`);
- Clear the stack(`c`);
Expand Down Expand Up @@ -246,6 +248,17 @@ lB -1 * lD + lA # POSITIVE DELTA
[ X2: ] P R LS lS p
```

16. Generate $n$ (pseudo)random numbers from user-defined range:
```
5 k
[ lA lB @ p ] sR
[ Enter number of samples: ] P ? sN
[ Enter lower bound: ] P ? sA
[ Enter upper bound: ] P ? sB
[ lR x r 1 + d lN >=L ] sL
0 lL x
```

## License

[GPLv3](https://choosealicense.com/licenses/gpl-3.0/)
32 changes: 26 additions & 6 deletions man.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: dc
section: 1
header: General Commands Manual
footer: Marco Cetica
date: March 13, 2024
date: March 14, 2024
---


Expand Down Expand Up @@ -102,7 +102,7 @@ the map's `key` and the associated value is represented by the map's `value`.
By default each value of any kind of stack is represented by a string. Each operation is in charge to type convert the value before and after
their invocation. The user can store both numeric and alphanumeric values on the stack. The latter using the _macro_ syntax(see below).

Arrays are homogeneous, thus the only supported data type is the `string`(the internal string type and not the **dc** one).
Arrays are homogeneous data structures that implement the same data type of the stack, i.e. the string.

# COMMANDS
Below, there is a list of supported **dc** commands.
Expand All @@ -121,7 +121,7 @@ Pops off the value on top of the stack, without altering the stack.

Prints the entire contents of the stack without altering anything.

## Arithmetic
## Mathematics

**+**

Expand Down Expand Up @@ -167,11 +167,20 @@ Pops one value, computes its factorial, and pushes that.

**pi**

Pushes pi approximation
Pushes pi approximation.

**e**

Pushes e approximation
Pushes e approximation.

**@**

Pops two values from the stack and generate a random number, using the first value popped as the upper bound and the second popped as the lower bound.
The random value is generated using a 64-bit Mersenne Twister pseudorandom number generator and a real uniform distribution.

**$**

Pops one value from the stack and convert it to the nearest integer of lesser magnitute.

## Trigonometrical

Expand Down Expand Up @@ -474,7 +483,7 @@ lB -1 * lD + lA # POSITIVE DELTA
```

11. Load an external file:
```sh
```
$> echo "[ p 1 + d lN >=L ] sL" > loop.dc
$> cat prg.dc
[ loop.dc ] ' # Load loop macro
Expand All @@ -483,6 +492,17 @@ $> cat prg.dc
c 1 lL x # Clear the stack, add lower bound, load and execute macro
```

12. Generate *n* (pseudo)random numbers from user-defined range:
```
5 k
[ lA lB @ p ] sR
[ Enter number of samples: ] P ? sN
[ Enter lower bound: ] P ? sA
[ Enter upper bound: ] P ? sB
[ lR x r 1 + d lN >=L ] sL
0 lL x
```

# AUTHORS
The original version of the **dc** command was written by Robert Morris and Lorinda Cherry.
This version of **dc** is developed by Marco Cetica.
Expand Down
6 changes: 3 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(src)
set(HEADER_FILES
eval.h
macro.h
math.h
mathematics.h
operation.h
stack.h
adt.h
Expand All @@ -12,9 +12,9 @@ set(HEADER_FILES
set(SOURCE_FILES
eval.cpp
macro.cpp
math.cpp
mathematics.cpp
stack.cpp
adt.cpp
)

add_library(src STATIC ${SOURCE_FILES} ${HEADER_FILES})
add_library(src STATIC ${SOURCE_FILES} ${HEADER_FILES})
40 changes: 21 additions & 19 deletions src/eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "adt.cpp"
#include "eval.h"
#include "math.h"
#include "mathematics.h"
#include "stack.h"
#include "macro.h"
#include "is_num.h"
Expand All @@ -22,24 +22,26 @@

void Evaluate::init_environment() {
// Numerical operations
this->op_factory.emplace("+", MAKE_UNIQUE_PTR(Math, OPType::ADD));
this->op_factory.emplace("-", MAKE_UNIQUE_PTR(Math, OPType::SUB));
this->op_factory.emplace("*", MAKE_UNIQUE_PTR(Math, OPType::MUL));
this->op_factory.emplace("/", MAKE_UNIQUE_PTR(Math, OPType::DIV));
this->op_factory.emplace("%", MAKE_UNIQUE_PTR(Math, OPType::MOD));
this->op_factory.emplace("~", MAKE_UNIQUE_PTR(Math, OPType::DIV_MOD));
this->op_factory.emplace("|", MAKE_UNIQUE_PTR(Math, OPType::MOD_EXP));
this->op_factory.emplace("^", MAKE_UNIQUE_PTR(Math, OPType::EXP));
this->op_factory.emplace("v", MAKE_UNIQUE_PTR(Math, OPType::SQRT));
this->op_factory.emplace("sin", MAKE_UNIQUE_PTR(Math, OPType::SIN));
this->op_factory.emplace("cos", MAKE_UNIQUE_PTR(Math, OPType::COS));
this->op_factory.emplace("tan", MAKE_UNIQUE_PTR(Math, OPType::TAN));
this->op_factory.emplace("asin", MAKE_UNIQUE_PTR(Math, OPType::ASIN));
this->op_factory.emplace("acos", MAKE_UNIQUE_PTR(Math, OPType::ACOS));
this->op_factory.emplace("atan", MAKE_UNIQUE_PTR(Math, OPType::ATAN));
this->op_factory.emplace("!", MAKE_UNIQUE_PTR(Math, OPType::FACT));
this->op_factory.emplace("pi", MAKE_UNIQUE_PTR(Math, OPType::PI));
this->op_factory.emplace("e", MAKE_UNIQUE_PTR(Math, OPType::E));
this->op_factory.emplace("+", MAKE_UNIQUE_PTR(Mathematics, OPType::ADD));
this->op_factory.emplace("-", MAKE_UNIQUE_PTR(Mathematics, OPType::SUB));
this->op_factory.emplace("*", MAKE_UNIQUE_PTR(Mathematics, OPType::MUL));
this->op_factory.emplace("/", MAKE_UNIQUE_PTR(Mathematics, OPType::DIV));
this->op_factory.emplace("%", MAKE_UNIQUE_PTR(Mathematics, OPType::MOD));
this->op_factory.emplace("~", MAKE_UNIQUE_PTR(Mathematics, OPType::DIV_MOD));
this->op_factory.emplace("|", MAKE_UNIQUE_PTR(Mathematics, OPType::MOD_EXP));
this->op_factory.emplace("^", MAKE_UNIQUE_PTR(Mathematics, OPType::EXP));
this->op_factory.emplace("v", MAKE_UNIQUE_PTR(Mathematics, OPType::SQRT));
this->op_factory.emplace("sin", MAKE_UNIQUE_PTR(Mathematics, OPType::SIN));
this->op_factory.emplace("cos", MAKE_UNIQUE_PTR(Mathematics, OPType::COS));
this->op_factory.emplace("tan", MAKE_UNIQUE_PTR(Mathematics, OPType::TAN));
this->op_factory.emplace("asin", MAKE_UNIQUE_PTR(Mathematics, OPType::ASIN));
this->op_factory.emplace("acos", MAKE_UNIQUE_PTR(Mathematics, OPType::ACOS));
this->op_factory.emplace("atan", MAKE_UNIQUE_PTR(Mathematics, OPType::ATAN));
this->op_factory.emplace("!", MAKE_UNIQUE_PTR(Mathematics, OPType::FACT));
this->op_factory.emplace("pi", MAKE_UNIQUE_PTR(Mathematics, OPType::PI));
this->op_factory.emplace("e", MAKE_UNIQUE_PTR(Mathematics, OPType::E));
this->op_factory.emplace("@", MAKE_UNIQUE_PTR(Mathematics, OPType::RND));
this->op_factory.emplace("$", MAKE_UNIQUE_PTR(Mathematics, OPType::INT));
// Stack operations
this->op_factory.emplace("p", MAKE_UNIQUE_PTR(Stack, OPType::PCG));
this->op_factory.emplace("pb", MAKE_UNIQUE_PTR(Stack, OPType::PBB));
Expand Down
Loading

0 comments on commit 088fbbe

Please sign in to comment.