soveran / clac Goto Github PK
View Code? Open in Web Editor NEWCommand-line, stack-based calculator with postfix notation
License: BSD 2-Clause "Simplified" License
Command-line, stack-based calculator with postfix notation
License: BSD 2-Clause "Simplified" License
The makefile as currently written does not work out of the box on Ubuntu 14.04. It gives an error when trying to link the float
, pow
, ceil
and round
functions. A little googling lead to http://stackoverflow.com/a/8064497/823592, which resolved the issue via moving the -lm flag after the source and object files.
I'd create a PR for this, but it seems like a fairly easy fix regardless.
is there a reserved word ? because I read the docs and found Nothing
Adding the basic functions for a scientific calculator would be great! Something along the lines of trig fn, logs. Anyways great setup for the calculator! As an engineer, these are functions I use on a regular basis.
Let's say I have two items on the stack
> 4 3
And I want to square the second number (3) and add that to the product of the two numbers (4,3). There's no obvious way to do this, you do something like this, and hit a wall (where the pipe indicates the division between input and the printed stack).
> 4 3 dup dup * | 4 3 9
There's no obvious way to solve this. If, however, we had a word (e.g. roll) that changes a b c
to c a b
, we could do this:
> 4 3 dup dup * | 4 3 9
> 4 3 dup dup * roll | 9 4 3
> 4 3 dup dup * roll * | 9 12
> 4 3 dup dup * roll * + | 21
Hello, I have been using this program for a bit and I have noticed that the words file doesn't allow for comments. I think have some comments would useful even for a small words file.
Here's my proposal:
clac
would just ignore the line if it starts with a comment character. Probably having inline comments wouldn't be that much harder, but given how simple this program is, I think it wouldn't be worth it.#
, as in it is various UNIX(-like) shells, python, and various configuration languages."#"
is already interpreted as just #
, so I thinking there would be no need for any escape sequences here, just an update to the man
page.I'm not exactly the world's greatest C programmer, but I would be willing to try doing it if you think it's a reasonable idea.
Hello again. While I was working on the stuff in #16 I noticed there is a point of confusion in the docs. Under the "commands" section it says "When a command requires an argument, it pops a value from the stack. If the stack is empty, a zero is provided instead."
This is not true for most of the commands; this is the internal behavior when pop
is called on an empty stack, but most commands don't trigger that since they are guarded by if (count(s0) > ..)
and the observed behavior is instead that the command becomes a noop. It is true for others, e.g. sum
. This makes things a bit confusing and could probably stand to be clarified (or made consistent in the code if you prefer).
Again, nice project. It's been fun poking around and I appreciate how clean the code is.
Based on https://github.com/soveran/clac/releases and https://github.com/soveran/clac/releases/latest I cannot tell if 0.3.0, 0.3.1, or something else besides either of them, is the current stable version.
Which version should be in Homebrew's stable spec? Thanks!
Could it be renamed to "clsc"? Due to:
3.5 8 *
= 28
3.5 8*
= 3.5
The latter format should work as well to
It would be nice to have a tag/release so we could package this nice calculator in distributions (working on it for Arch).
Thanks !
Hello hello! This is a stupendous tool that's being made here, I like it a lot!
I would like to know if a factorial function would be acceptable for this tool. For most functions, I suspect defining words would be sufficient, but in the case of factorials, there's a bit more than just a one-run set of operations. If factorial is defined, I suspect that most other probability problems I would use this tool with could use words that utilize that factorial function.
I imagine the use case being something like the following:
> 9 !
= 362880
or
> 9 fact
= 362880
Again, thank you for this wonderful tool!
I find it somewhat annoying that I have to write 3 4 *
instead of 3 4*
. I know that clac uses sdssplitargs()
, which needs whitespace between tokens, and that any enhancement needs to be unambiguous for both the code and the user. I propose the following solution:
Every token output by sdssplitargs()
is examined first in its entirety, as it is now, and then, if it's not recognized, it is split into a head and a tail in a loop, where the tail is 1, 2, 3... characters long, until the head is recognized (or until the head would be empty, in which case everything is discarded). As soon as the head is recognized, it is processed, and then the tail is handled in the same way (cyclically or recursively).
E.g., if the input is 2pi*
(supposing pi
is a word): it is not recognized, so 2pi
is tried (the tail being *
), then 2p
(with tail i*
) and then 2
, which is recognized and processed. Then the tail pi*
is not recognized, so that pi
is tried, which is recognized and processed, and finally the tail *
is recognized and processed.
In other words: tokens are parsed in a "greedy" way, with backtracking.
Hello Michel!
I've looked at this project and found it even more clever than your map
one.
Such a beautiful thing!
It's a pure UNIX philosophy I guess, so minimalistic and so functional programs.
Thanks for that!
I just checked out your code, pretty good. But "switch " statement would be my choice if I am to match single length string. I noticed that you didn't check for zero denomination before division.
Hi ,
I found that call stack overflow occurs when use a word defined circularly.
A word can define circularly like below.
a "a"
or It can be defined circularly with multiple words like below.
a "b"
b "c"
c "d"
d "a"
And program is terminated by segmentation fault when there is the word defined circularly in the expression.
$ clac "a"
Segmentation fault
So, wouldn't it make sense to simply implement all/most of the functions (and probably some of the constants) in math.h as primitives, since you're already using it? That should automatically give you most of the functions of a scientific calculator, plus the standard library implementations for those functions are very high quality. Having some of the standard unary operations like exp is also very convenient syntactically, because you can just do something like 5 3 exp *
instead of 5 e 3 ^ *
or 5 e 3 swap ^ *
(I usually think in right to left order). At the very least more transcendental functions like atan
and erf
since you can't easily implement those in terms of other functions (and even if you could it's not a good idea because of numerical problems).
As another point regarding adding constants like e, pi etc., I feel like it'd make sense to just have those pre-loaded. It's not like anybody wants to customize the value of pi. Trying to add every obscure constant in existence isn't feasible, but the ones in math.h are already there and would be nice simply for numerical correctness.
I don't mind doing the legwork to add them, it's pretty easy. I already did exp
, that was what motivated this. I'm interested in building a physical calculator using clac, hence the desire to make it a more complete scientific calculator.
This is relevant to issue #10.
==> Downloading https://github.com/soveran/clac/archive/0.3.2.tar.gz
Already downloaded: /Users/joe/Library/Caches/Homebrew/clac-0.3.2.tar.gz
==> Verifying clac-0.3.2.tar.gz checksum
tar xzf /Users/joe/Library/Caches/Homebrew/clac-0.3.2.tar.gz
==> make PREFIX=/usr/local/Cellar/clac/0.3.2 install
clang -Wall -Os -g -c linenoise.c
clang -o sds-test sds.c -Wall -std=c99 -pedantic -O2 -DSDS_TEST_MAIN
>>> Type ./sds-test to run the sds.c unit tests.
clang -I./deps/linenoise -I./deps/sds -Wall -Os -o clac clac.c -lm deps/linenoise/linenoise.o deps/sds/sds.o
clang: error: no such file or directory: 'deps/sds/sds.o'
make: *** [clac] Error 1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.