amber-lang / amber

💎 Amber the programming language compiled to Bash
https://amber-lang.com
GNU General Public License v3.0
3.81k stars 81 forks source link

Remove bc and sed #366

Open b1ek opened 1 month ago

b1ek commented 1 month ago

right now there are some cases when it compiles to use bc and sed when its not necessary at all. problem is that bc is not exactly a common command to have installed, and sed's behaviour differs on unix and bsd

like the way how 1 + 1 resolves to $(echo 1 '+' 1 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//'), but should be (( 1 + 1 ))

Ph0enixKM commented 1 month ago

@b1ek I think that we will add the Int data type. Is there any way to avoid bc and still be able to have simple floating point arithmetic API? Perhaps some constant precision arithmetic or maybe there is some relatively small library that handles that.

Could you make a research on that?

b1ek commented 1 month ago

avoid bc and still be able to have simple floating point arithmetic API

i would be surprised if there is, unless you'd be comfortable with doing stuff like 1 / 2 -> 10 / 2 and then just strip off the last digit to a floating point (but i dont think that's maintainable)

tbh i'd rather use python for simple floating point math, or select a math runtime dynamically from whats available on the system

Mte90 commented 1 month ago

I think that it is better to use bc for calculation.

It is part of the gnu command set https://www.gnu.org/software/bc/manual/html_mono/bc.html and it is everywhere and we will have the bash dependency checker anyway.

Mte90 commented 1 month ago

That line also generate vvarious shellcheck issues:

if [ "$(echo "$(echo ""$__AF_date_compare3_v0__4_16"" '==' "$(echo "" '-' "1" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')" '&&' "$(echo ""$__AF_date_compare3_v0__4_60"" '==' "1" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')" != 0 ]; then
                       ^--------------------------^ SC2027 (warning): The surrounding quotes actually unquote this. Remove or escape them.
                       ^--------------------------^ SC2086 (info): Double quote to prevent globbing and word splitting.
                                                                                                                                                                                       ^--------------------------^ SC2027 (warning): The surrounding quotes actually unquote this. Remove or escape them.
                                                                                                                                                                                       ^--------------------------^ SC2086 (info): Double quote to prevent globbing and word splitting.

Did you mean: 
if [ "$(echo "$(echo """$__AF_date_compare3_v0__4_16""" '==' "$(echo "" '-' "1" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')" '&&' "$(echo """$__AF_date_compare3_v0__4_60""" '==' "1" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')" | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')" != 0 ]; then

What is the purpose of this concat? Get the value and convert it as intenger with checks for the comma?

Mte90 commented 1 month ago

so checking the code it is there to remove trailing zero from the expression generated by bc.

b1ek commented 1 month ago

i think i could live with a few trailing zeros after the number

Mte90 commented 1 month ago

I think https://askubuntu.com/questions/217570/bc-set-number-of-digits-after-decimal-point

We can use bc to generate without trailing zero using the scale variable and we can remove the sed command.

I think that the issue starts from https://stackoverflow.com/questions/30033784/bash-sed-trim-trailing-zeroes-off-a-floating-point-number that is the first result about the issue but not the best answer for the issue we have.