h3rald / min

A small but practical concatenative programming language and shell
https://min-lang.org
MIT License
311 stars 23 forks source link

Added simple trigonometry functions to stdlib #5

Closed PMunch closed 7 years ago

PMunch commented 7 years ago

Added some simple trigonometry functions to the stdlib of min.

h3rald commented 7 years ago

Nice!

I’ll merge it but... would you mind adding some minimal tests (written in min) and also maybe some docs for the site and guide as well ;)

I’ll have to plan a new release soon at this rate... thanks a lot!

PMunch commented 7 years ago

Hmm, working on some tests now by copying tests from the num_test.min file. How have you handled floating point precision so far? Some of the functions returns the "wrong" number because of floating point errors, and therefore fail a simple == with the correct answer. Should I just use the expected value instead, even if it's "wrong"?

EDIT: Even that doesn't work well, for example:

[/tmp/min]$ 0.5 tanh
{1} -> 0.4621171572600097
[/tmp/min]$ 0.5 tanh 0.4621171572600097 ==
{2} -> false

EDIT 2: Another issue, asin is only valid for [-1, 1]. It returns nan when you try to run it with other values, but how can I test this? 2 asin nan == complains that nan is not defined and 2 asin number? returns true..

h3rald commented 7 years ago

Sigh... I didn’t, I guess :(

Perhaps it would be necessary to:

PMunch commented 7 years ago

Tried to search around for something like this implemented in a Nim library. Unfortunately (and a bit surprising) I couldn't find anything.

Something else to consider is if you want to have something like Clojures ratios in Min. This way 1 10 / would yield a ratio, which after being added to 1 10 / nine times would give 1 as expected and not 0.99999999999... This would of course mean even more work, but it might be worth it, depending on what you foresee as the use case for min.

h3rald commented 7 years ago

Proper rational numbers would be nice, but that would definitely mean more work, especially considering that I was originally debating on whether to include support for floats at all!

For now, the main use cases I foresee for min are related to shell/file processing, glue code and so on. So far I am using min within HastySite, a static site generator I used for min-lang.org and h3rald.com.

Out of curiosity, what are you planning to use min for?

PMunch commented 7 years ago

Haha, I see. I was looking into this a bit more yesterday and for now I think we can get away with just adding nan, inf, and an equals operator. Both nan and inf are specially defined numbers in the IEEE standard for floating point numbers so Nim should be able to deal with those as you would expect. Edge cases could of course be improved upon but I think most people are used to floating point arithmetic errors so it shouldn't be too surprising to anyone if you had rounding errors.

To be honest I just discovered it through Nim. I've used RPN calculators my entire life so I found the concept very interesting. Currently I'm just using it as a way to play around with a concatenative language, but I might end up using it for something else in the future.

h3rald commented 7 years ago

Hi again,

Merged! I actually renamed your trig module to math and added a few more symbols to calculate logarithms, square roots, and also pi, e, tau. Thanks to the new floating point support you implemented everything works as expected :)

There are still some symbols missing compared to the nim math module, but I decided to stop for now (also because they also need documenting!) -- if you need anything more feel free to add them.

PMunch commented 7 years ago

Great! I was thinking about adding those as well, but I had kinda dug myself into a hole by calling it trig. Glad you changed it :)