Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Numbers and arbitrary precison #70

Open
davekeogh opened this issue May 22, 2024 · 10 comments
Open

[Feature] Numbers and arbitrary precison #70

davekeogh opened this issue May 22, 2024 · 10 comments
Labels
enhancement New feature or request

Comments

@davekeogh
Copy link

https://github.com/Ph0enixKM/Amber/blob/eedd6812c06885b8d1658c6b19246238bff7e489/src/translate/compute.rs#L51

I understand that bash cannot handle arbitrary precision numbers, but I think many users may prefer a low precision mode that does not generate pipelines like this.

There may also be an issue of bc not being installed on all systems.

@orent
Copy link

orent commented May 22, 2024

While bc is available on regular desktop linux environments it is missing in many "lite" cloud environments. It is not acceptable to rely on it, especially without checking at the top of the generated script to fail fast with a suitable warning.

In general, anything that can be done with bash builtins without forking an external command should be done with builtins e.g. variable expansion substitutions instead of sed, case/esac globs, etc. Even if it is awkward, the performance difference is huge.

Also, check all generated code with ShellSheck.net.

@Ph0enixKM
Copy link
Member

Yeah, At first I wanted to make Amber feel more JavaScriptish so I decided to use the bc everywhere. However this creates a limitation and dependency on the bc command. The solution would be to introduce a new integer datatype Int that is a lightweight alternative.

@Ph0enixKM Ph0enixKM added the enhancement New feature or request label May 22, 2024
@garyrob
Copy link

garyrob commented May 22, 2024

I just want to chime in that I agree about having an Int datatype that doesn't require bc. I was looking for something that compiled to bash so that I could make some software I'm thinking about distributing as easy to install and run as possible. Requiring cloud linux users to install bc just to run my software defeats the purpose. And I won't have need of anything beyond what an Int datatype would offer. I expect that's true for many potential projects that could otherwise use Amber.

@eXamadeus
Copy link

eXamadeus commented May 22, 2024

@Ph0enixKM there is a conversation happening on your reddit post related to this link. I'd like to say that I'm a big fan of a new integer type, as it would resolve to much "cleaner" integer comparisons.

In regards to needing bc for lots of operations, I'm not sure you'll be able to easily get around the requirement for things like floats/etc. In fact, I'm fairly certain bash only natively supports integer comparisons. So people are generally used to needing to use bc for complex math calculations. Those that don't have bc available are usually not needing to perform such arithmetic (this is a big assumption on my part, mind you).


An interesting approach you could take, that would allow for the complete removal of bc, would be to allow "fixed precision arithmetic" that uses integer math to get decimal results. In this example (taken from StackOverflow) you can see how it's possible to use integers to do basic arithmetic for fixed-precision results (in this case 2 decimal positions):

VALUE=100
DIV=3
RESULT=$((${VALUE}00/$DIV))
echo "${RESULT:0:-2}.${RESULT: -2}"
33.33

This would obviously be a large and rather complex change. You could come up with a "scale" that is generally available for floating-point math. Something like 6 decimals would probably suffice, and any more precision would be up to the users to figure out (they can do the same integer math, if they so desire). Granted this is assuming a lot and definitely makes maintenance of the language much more challenging.

The easiest approach would be to simply say "we don't support floating point arithmetic" and use only bash, leaving it to the user to figure out their own floating point solutions. This is actually the most "stable" approach IMO and will leave any generated output as close to pure bash as possible. This may, however, deviate wildy from your original vision of "JavaScript-like".

@github-project-automation github-project-automation bot moved this to 📋 Todo in Amber Project May 25, 2024
@Ph0enixKM Ph0enixKM changed the title Numbers and arbitrary precison ✨ Numbers and arbitrary precison May 25, 2024
@amber-lang amber-lang deleted a comment May 25, 2024
@Ph0enixKM
Copy link
Member

@eXamadeus Thank you for your point of view. I also think that we need to pivot the vision a little and simply add the Integer type to Amber. The closer we can get to readable bash, the easier it will be to read and debug!

@nicktimko
Copy link

#95 mentions checking for sed, [[ and bc, but [[ IIRC is specifically a Bash-ism.
Looking at some Docker images like amazonlinux and ubuntu, they include sed but not bc.

The alpine image doesn't include Bash, but it does seem to have [[ in it's BusyBox executable...maybe for some limited cross-compatibility?

In general though, compiling to Bash is nice because it would provide some good bootstrapping capabilities, and might be all you need. My interpretation for Amber's niche in that using Bash-as-a-target, it can be used in very minimal environments, and ideally down to zero in some cases. For instance, to configure some environment you could load your compiled .ab and go. If there's a hard requirement on some deps, you'll need to load some .sh which might need to configure proxies and set up the environment before apt/yum/apk even works, so you're needing to bootstrap a bootstrap script, but then you could just bootstrap Python which can take you even further.

Crappy ASCII diagram

                 Environments
 Simpler                                Richer
<---------------------------------------------->
^ base alpine
|    ^ base ubuntu, amazonlinux, debian
|    |         ^ base desktop Linuxes
|    |         |
X========== shell scripts
     |  =======X======== single-file Python or similar
     |         |  ======================== Ansible/Salt/Puppet/etc.
   [-?-]=======X=== Amber?
<---------------------------------------------->
Simple Tasks                           Whatever
                 Ideal for...

@Ph0enixKM
Copy link
Member

Ph0enixKM commented Jun 13, 2024

#95 mentions checking for sed, [[ and bc, but [[ IIRC is specifically a Bash-ism. Looking at some Docker images like amazonlinux and ubuntu, they include sed but not bc.

The solution would be the following: If Num is never used anywhere, then the compiler will not include the bc dependency in the bootstrap. This would extend the scope of Amber for simpler environments.

@Mte90
Copy link
Member

Mte90 commented Jul 19, 2024

Closed as duplicates for #70

@Mte90 Mte90 closed this as completed Jul 19, 2024
@github-project-automation github-project-automation bot moved this from 📋 Todo to 🏁 Done in Amber Project Jul 19, 2024
@Ph0enixKM
Copy link
Member

@Mte90 this is #70

@Ph0enixKM Ph0enixKM reopened this Jul 20, 2024
@Mte90
Copy link
Member

Mte90 commented Jul 31, 2024

So #381 tries to remove sed to clean up numbers and use the scale variable for echo that defines the approximation and the behavior with results.

Maybe it is the first step...

@Ph0enixKM Ph0enixKM changed the title ✨ Numbers and arbitrary precison [Feature] Numbers and arbitrary precison Sep 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants