3 ms·
> Now, suppose I want to write a POSIX shell script that does trigonometric calculations. Should I actually use `bc`? Yes, you should; `bc` includes trigonomet
by ghoward 5y ago
> Now, suppose I want to write a POSIX shell script that does trigonometric calculations. Should I actually use `bc`?
Yes, you should; `bc` includes trigonometric functions even in POSIX accessible by the `-l` flag.
However, I'm not sure why you would; if you know you have a `bc` implementation other than a strict POSIX `bc`, I would just use that, even in a POSIX shell script.
POSIX is extremely limited; it does not even have `else` for `if` statements! It also does not allow you to use comparison operators outside an `if` statement or loop header. It does not have `continue`.
On the other hand, my `bc` is known to work in just about every POSIX system, and there are ports for it in OpenBSD and NetBSD. It is also the system `bc` in FreeBSD for FreeBSD 13. It also builds on Linux for either glibc or musl, and there are packages for it in Arch AUR, Gentoo, and other smaller distros. And it's easy to build from source with zero dependencies.
In short, if you're going to write a shell script to do math, I'd personally find it more appealing to actually check for and install my `bc` than to use strict POSIX.
> Is there a "translator" that reads `GNU bc` or your `bc` for example, and converts it into POSIX-compliant bc? (meaning converting the symbols to one letter in a way that does not produce conflict, declaring all used variables in a function in the first line (auto), etc)
For the record, my `bc` and GNU `bc` still require the auto list. That's part of the standard, so we cannot really get away from it.
But as for the existence of a translator, no, there is not a translator. I could probably write one, and putting it in my `bc` would be easiest since my `bc` already has a parser.
However, I do not think that would be a good use of time; trying to translate some features, especially `continue` would be tough. `continue` would be tough because I would have to translate the loop into two loops and when exiting the loop, differentiate the case between exiting for a `continue` and an actual exit, and if on `continue` let the outer loop loop.
Also, some features just could not translate, I'm afraid. My `bc` has a full PRNG, which can generate numbers of arbitrary size and precision. There just would not be a way of translating that feature.
And putting the translator in my `bc` would sort of be useless; if you installed it for the translator, why not just use it?
If this message sounds annoyed, it's because I'm annoyed at POSIX, not you. The fact that they have not even standardized `else` really gets my goat; all implementations, besides the Plan 9 one, have `else`, even one that translates for `dc`. There's no excuse for not having it. And they also seem to limit it on purpose, with not allowing comparison operators outside of `if` and loops. (Oh, and you can only use ONE operator per `if` and loop!) Grr...
tl;dr: Yes, use `bc` for trigonometry in shell scripts, but use a full-featured `bc` if at all possible.
- ducktective 5y agoThanks again :) I've bookmarked your gitea bc repo, not just for bc, but as an example of a neatly managed FOSS project, like providing performance comparison, fuzzing, project structure, the fact of using self-hosted gitea itself, etc Skimming through [1], I see neither of Winget [2] nor Chocolatey [3] have any `bc` implementations as of yet. Maybe you can send them a PR? It may help adoption in other distros in the future. [1] https://repology.org/project/bc/versions https://repology.org/project/bc/versions [2] https://github.com/microsoft/winget-pkgs https://github.com/microsoft/winget-pkgs [3] https://community.chocolatey.org/packages?q=%22bc%22 https://community.chocolatey.org/packages?q=%22bc%22 Hope your implementation wins ;)
- ghoward 5y agoYou're welcome. :) > I've bookmarked your gitea bc repo, not just for bc, but as an example of a neatly managed FOSS project, like providing performance comparison, fuzzing, project structure, the fact of using self-hosted gitea itself, etc Thank you! It's good to know that my project is actually good; sometimes I wonder if I am just full of myself. XD > Skimming through [1], I see neither of Winget [2] nor Chocolatey [3] have any `bc` implementations as of yet. Maybe you can send them a PR? It may help adoption in other distros in the future. I will do that. However, I might wait until I accept a PR that was just made [1] that will fix issues on Windows. I am not good at Windows, so there are problems. > Hope your implementation wins ;) Thank you. :) [1]: https://github.com/gavinhoward/bc/pull/38 https://github.com/gavinhoward/bc/pull/38