Open b1ek opened 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?
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
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.
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?
so checking the code it is there to remove trailing zero from the expression generated by bc
.
i think i could live with a few trailing zeros after the number
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.
right now there are some cases when it compiles to use
bc
andsed
when its not necessary at all. problem is thatbc
is not exactly a common command to have installed, andsed
's behaviour differs on unix and bsdlike the way how
1 + 1
resolves to$(echo 1 '+' 1 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')
, but should be(( 1 + 1 ))