Coder Social home page Coder Social logo

shellmath's People

Contributors

clarity20 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

shellmath's Issues

Multiple errors while attempting to utilize shellmath.sh

Sorry I cannot be more descriptive with the exact issue. I am short on time and just wanted to quickly try the script.

I cloned the repo and subsequently wrote the following script (the test script is located at a different path: /home/toazd/github/personal/scripts/bash/misc/sandbox):

#!/usr/bin/env bash

source /home/toazd/github/external/shellmath/shellmath.sh

_shellmath_add 1.2 2.4340 3.1234 4.234314 5.324234
_shellmath_subtract 2.4340 3.1234
_shellmath_multiply 2.4340 3.1234 4.234314 5.324234
_shellmath_divide 2.4340 3.1234

And the output after running the previous script:

$ ./shellmath1.sh
0.6894
/home/toazd/github/external/shellmath/shellmath.sh: line 484: ((: 063173158332: value too great for base (error token is "063173158332")
/home/toazd/github/external/shellmath/shellmath.sh: line 494: ((: 063173158332: value too great for base (error token is "063173158332")
/home/toazd/github/external/shellmath/shellmath.sh: line 498: ((: 063173158332: value too great for base (error token is "063173158332")
Invalid argument; decimal number required: '154.-4096516847975576672'/home/toazd/github/external/shellmath/shellmath.sh: line 742: ((: 3.1234: syntax error: invalid arithmetic operator (error token is ".1234")
0.7792789908433

For reference the answers should be (unless the order is swapped for subtract and divide - I could not find that detail in the readme):
16.315948
-0.6894 (or 0.6894)
171.391142871326
0.779278990843312 (or 1.28323746918652)

Subtraction...

... should be short and easy because it's addition by another name.

10# can't accept negative fractionalSum

Hello,
in the comments, you point out "(the "10#" operator) cannot work directly with negative numbers".

Yet $fractionalSum can become negative (line 511) and is later used (line 518 and others) with the 10# operator:

shellmath/shellmath.sh

Lines 506 to 554 in 1734b02

if ((unsignedFracSumLength < unsignedFracLength)); then
local lengthDiff=$((unsignedFracLength - unsignedFracSumLength))
local zeroPrefix
printf -v zeroPrefix "%0*d" "$lengthDiff" 0
if ((fractionalSum < 0)); then
fractionalSum="-"${zeroPrefix}${fractionalSum:1}
else
fractionalSum=${zeroPrefix}${fractionalSum}
fi
fi
# Carry a digit from fraction to integer if required
if ((10#$fractionalSum!=0 && unsignedFracSumLength > unsignedFracLength)); then
local carryAmount
((carryAmount = isNegative1?-1:1))
((integerSum += carryAmount))
# Remove the leading 1-digit whether the fraction is + or -
fractionalSum=${fractionalSum/1/}
fi
# Transform the partial sums from additive to concatenative. Example: the
# pair (-2,3) is not -2.3 but rather (-2)+(0.3), i.e. -1.7 so we want to
# transform (-2,3) to (-1,7). This transformation is meaningful when
# the two parts have opposite signs, so that's what we look for.
if ((integerSum < 0 && 10#$fractionalSum > 0)); then
((integerSum += 1))
((fractionalSum = 10#$fractionalSum - 10**unsignedFracSumLength))
elif ((integerSum > 0 && 10#$fractionalSum < 0)); then
((integerSum -= 1))
((fractionalSum = 10**unsignedFracSumLength + 10#$fractionalSum))
fi
# This last case needs to function either as an "else" for the above,
# or as a coda to the "if" clause when integerSum is -1 initially.
if ((integerSum == 0 && 10#$fractionalSum < 0)); then
integerSum="-"$integerSum
((fractionalSum *= -1))
fi
# Touch up the numbers for display
local sum
((10#$fractionalSum < 0)) && fractionalSum=${fractionalSum:1}
if (( (!isSubcall) && (isScientific1 || isScientific2) )); then
_shellmath_numToScientific "$integerSum" "$fractionalSum"
_shellmath_getReturnValue sum
elif ((10#$fractionalSum)); then
printf -v sum "%s.%s" "$integerSum" "$fractionalSum"
else
sum=$integerSum
fi

Multiplication...

... is easier now that addition is implemented. Pretty much boils down to applying the distributive law.

Bash 5.1.0 - Subtracting error: invalid integer constant

Hello, running a simple example I get the following error:

$ cat foo.sh
#!/bin/bash
source shellmath.sh
__shellmath_isOptimized=${__shellmath_true}
_shellmath_subtract 3.0 1.6
_shellmath_getReturnValue tmp
echo $tmp

$ ./foo.sh
shellmath.sh: line 518: ((: 10#: invalid integer constant (error token is "10#")
shellmath.sh: line 530: ((: integerSum < 0 && 10#: invalid integer constant (error token is "10#")
shellmath.sh: line 533: ((: integerSum > 0 && 10#: invalid integer constant (error token is "10#")
shellmath.sh: line 539: ((: integerSum == 0 && 10#: invalid integer constant (error token is "10#")
shellmath.sh: line 546: ((: 10#: invalid integer constant (error token is "10#")
shellmath.sh: line 550: ((: 10#: invalid integer constant (error token is "10#")
2

$ bash --version
GNU bash, version 5.1.0(1)-release (x86_64-pc-linux-gnu)

Use shallower recursion in many-argument addition and multiplication

Both + and * are implemented "head-recursively," i.e. add 1 2 3 4 expands to add 1 (add 2 (add 3 4))

This design was set up in the exigency of the moment. Its recursion depth is linear in the number of arguments.

We can improve upon this with a binary recursion: add 1 2 3 4 becomes add (add 1 2) (add 3 4)

This creates a recursion whose depth is logarithmic in the number of arguments.

Make creating and running test cases easier

Introduce a small test engine runTestCases.sh that will read testCases.sh and run the tests inside it. Then edit the tests to fit the simplified scheme and rename the file to testCases.in.

Features of the new testing scheme:

  • Relieve the burden of typing out the full function names for each test by inserting _shellfloat_assert or _shellfloat where appropriate.
  • Make output more informative than printing only "ok" or "FAIL":
    • Print line numbers, function names, arguments
  • Introduce verbosity options to regulate this output
  • Introduce option to abort at first failure

Division...

... can be tricky but I have handwritten notes and fasder's "reciprocation" to draw from.

Speed up multiplication

Like the other main APIs, multiplication calls validateAndParse() twice explicitly.
It later calls addition twice. That's 4 extra calls to validateAndParse().

Tests have shown that validateAndParse() is expensive compared to the rest of the logic.

One workaround would be to have a "secret" mechanism through which multiplication can call addition without wasting clock cycles doing stuff it's already done.

Let's introduce a new calling syntax:

    _shellfloat_add  --  integerPart1  fractionalPart1  isNegative1  type1  integerPart2  fractionalPart2  isNegative2  type2

If _shellfloat_add sees the "--" argument, it will skip the calls to validateAndParse and pencil in the trailing arguments for the values that validateAndParse would have generated.

We are omitting isScientific{1,2} because they will always be FALSE in this situation.

We will have to increase the value of __shellfloat_storageSpace to accommodate the long argument list.

Add descriptive function headers

All the usuals:

  • Function name
  • Usage / invocation syntax
  • Description of arguments
  • Return code and/or text generated
  • Verbal statement of purpose

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.