LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2019, 12:51 PM   #1
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 2,241

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
The power of bash: Fun with math


How about some fun with math using bash. No awk, bc or perl here, that's cheating. Plus they're not needed.

Some examples of how capable bash is.

((100 / $x)) out to 4 decimal places.
Code:
#! /usr/bin/env bash

div() {
    local d=${3:-4}
    local n=0000000000
    n=${n:0:$d}
    local r=$(($1$n/$2))
    r=${r:0:-$d}.${r: -$d}
    echo "$r"
}

a=100
b=1

while :; do
    echo ""$a" / "$b"" = $(div "$a" "$b")
    b=$(($b + 1))
    sleep .5
done
Fractions to decimal, out to 8 decimal places.
Code:
#! /usr/bin/env bash

a=1
b=1

for i in {1..10}; do
    for i in {1..10}; do
        ans=$(("$a"00000000/"$b"))
        echo -e ""$a"/"$b" to decimal is ${ans:0:-8}.${ans: -8}"
        b=$(($b + 1))
        sleep .5
    done
    echo ""
    a=$((a + 1))
    b=1
done
Another fraction to decimal converter
Code:
#! /usr/bin/env bash

a=1
b=64

until [ $b -eq 1 ]; do
    while [ $a -lt $b -o $a -eq $b ]; do
        printf "%s : %.8f\\n" "$a/$b is" "$((10000000 * $a/$b))e-7"
        a=$(($a + 1))
        sleep .5
    done
    b=$(($b / 2))
    a=1
    echo ""
done

Exponent calculator
Code:
#! /usr/bin/env bash

a=2
b=1

while :; do
    for i in {1..10}; do
        echo ""$a"^"$b" = $(($a ** $b))"
        sleep .5
        b=$(($b + 1))
    done
    a=$(($a + 1))
    b=1
    echo ""
done
Bash with awk example.
Finding the square, square root, sin, cosine, of a number.
Code:
#! /usr/bin/env bash

a=1
while :; do
    echo "The square of "$a" is $(($a * $a))"
    echo "The sqrt of "$a" is $(awk 'BEGIN{print sqrt('$a')}')"
    echo "The sine of "$a" is $(awk 'BEGIN{print sin('$a')}')"
    echo "The cosine of "$a" is $(awk 'BEGIN{print cos('$a')}')"
    echo ""
    sleep 3
    a=$(($a + 1))
done
Anyone who wants to participate, use the above info to make:

1. A decimal to fraction converter in bash (0.01562500 = 1/64)

2. A square root calculator in bash (35 = 5.9160798^2)

3. A cube root calculator in bash (10 = 2.154435^3)

4. A decimal number calculator in bash (.234 + .362 = 0.596)
 
Old 08-04-2019, 06:38 PM   #2
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (Chicago area)
Distribution: CentOS, MacOS, [Open]SuSE, Raspian, Red Hat, Slackware, Solaris, Tru64
Posts: 1,424

Rep: Reputation: 120Reputation: 120
Quote:
Originally Posted by teckk View Post
How about some fun with math using bash. No awk, bc or perl here, that's cheating. Plus they're not needed.

Some examples of how capable bash is.

<snip>
Back in the '80s, one could buy a full business accounting package written entirely using Bourne shell. (None of that fancypants Korn and Bash scripting.) It was only US$79.95 (in 1992). Tell JDEdwards, Oracle, and Ross to all take a flying leap. :^D
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I have heard of "new math" is this "Ubuntu math"? taylorkh Ubuntu 2 01-30-2014 05:19 PM
Perl Math::Bezier returns Math::Bizarre output captainentropy Programming 3 10-09-2013 09:00 PM
C (math.h)not doing right math? exp() issue. knockout_artist Programming 7 11-25-2011 02:13 PM
Running a 2.6.* kernel with math emulation ( Does the math emulation work ?) dar_beh_dar Linux - Kernel 3 05-20-2009 11:43 PM
math program that I can enter math functions ... Four General 5 04-19-2006 08:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration