*To*: debian-user@lists.debian.org*Subject*: Re: shell wrappers for trig and other mathematical functions*From*: <tomas@tuxteam.de>*Date*: Wed, 2 Oct 2019 09:11:57 +0200*Message-id*: <[🔎] 20191002071157.GC24943@tuxteam.de>*In-reply-to*: <[🔎] CAOphizJnMAXDnLRaMNRUGpC2A7W6S03nJGXb2Pgk53wF71WRzw@mail.gmail.com>*References*: <[🔎] CAOphizJnMAXDnLRaMNRUGpC2A7W6S03nJGXb2Pgk53wF71WRzw@mail.gmail.com>

On Tue, Oct 01, 2019 at 08:32:01PM -0700, Dan Hitt wrote: > I'm half-way looking for some shell wrappers for common trig functions like > sin, cos, exp, log, and others. > > I'm aware of bc, but it seems cumbersome. The shell way would be to do it with bc (or dc, if you want it cryptic). Note that the shell itself doesn't have a notion of floating point numbers: you're passing them around as strings, so you'd want to "spend as much time as possible" at the one or the other "side", and "cross the border" at rather few and defined times. Here's how I display my battery status as a shell script using bc: BAT='/sys/class/power_supply/BAT0' AC='/sys/class/power_supply/AC' FULL=$(cat $BAT/energy_full) NOW=$(cat $BAT/energy_now) case $(cat $AC/online) in 0) online="BAT" ;; 1) online="AC " ;; *) online="???" ;; esac echo -ne "$online " ; dc -e "5k $NOW $FULL / p" ; date +"%F%a%n %T" Now there are many holes in that: this assumes, f.ex. that /sys/class/... all produce sensible results (no spaces in them). But that's how I let bc cope with the "floating point" part -- the shell does the plumbing the way it likes to plumb: variable substitution, etc. I think this is the way shell is designed. You're trying to bend it into a different shape, which I'm sure is possible -- but I doubt it'll make you happy (it's on you to try, though, and don't let others discourage you!). Think a moment about "sin(1) + cos(1)" (and that the shell's "+" is going to bark at this [1]) to consider that you're looking into implementing a whole arithmetic interpreter... and then you're already at dc, take or give. Cheers [1] try "echo $((0.6 + 1.3))" in your bash to see what I mean :) -- t

**Attachment:
signature.asc**

**Follow-Ups**:**Re: shell wrappers for trig and other mathematical functions***From:*Nicolas George <george@nsup.org>

**References**:**shell wrappers for trig and other mathematical functions***From:*Dan Hitt <dan.hitt@gmail.com>

- Prev by Date:
**Re: is there who's who for this list?** - Next by Date:
**Re: shell wrappers for trig and other mathematical functions** - Previous by thread:
**Re: shell wrappers for trig and other mathematical functions** - Next by thread:
**Re: shell wrappers for trig and other mathematical functions** - Index(es):