Sponsored Content
Top Forums Shell Programming and Scripting Simple Scientific calculator for ADE, the UNIX environment for the AMIGA A1200(HD). Post 303036855 by wisecracker on Sunday 14th of July 2019 02:34:36 PM
Old 07-14-2019
Simple Scientific calculator for ADE, the UNIX environment for the AMIGA A1200(HD).

ADE is a UNIX environment for the ancient AMIGA A1200. By default this does NOT have the 'bc' command line calculator.
Although I did a DEMO code to create a C source and compile it under python 1.4.0 and ADE using ksh88 and the gcc of the day, I decided to create this baby that requires no Python and compilation at all. 'bc' can be obtained from AMINET but is is so convoluted that I don't use it.

This incarnation uses 'awk' purely as a calculator for complex floating point, (basic scientific), calculations and is a shell script.
This was developed on this MBP OSX 10.14.3 and tested in the AMIGA environment using the above.
Code:
#!/bin/sh

# #!/usr/local/bin/dash
# scicalc

# USAGE 1: All methods: [/full/path/to/|./]scicalc ['|"]expression_inside_quotes["|']<CR>
# USAGE 2: In a script: VAR='expression'; ./scicalc "${VAR}"<CR>
# Which quotes depends on the situation, "${VAR}" as an expression variable is valid '${VAR}' is invalid.

# All angular expressions are in radians!
expression="$1"

# Don't allow an empty "$1".
if [ "${expression}" = "" ]
then
    echo "Basic USAGE: [./]scicalc [']"'["]''expression_inside_quotes''["]'"[']""<CR>"
    echo "!!!Read the code for more information!!!"
    exit 1
fi

# Minimum awk Version tested on:
# GNU Awk 3.0.3.
# For the AMIGA A1200(HD) OS3.0.x, running ADE, the UNIX environment using ksh88 as the shell.
#
# Developed on OSX 10.14.x, "awk version 20070501".
awk 'BEGIN{ pi=3.141592653589793; e=2.718281828459045; printf "%.15f", ('"${expression}"'); print ""; }'

# Example 1:
# ./scicalc '(sin(pi / 3.5) * sin(pi / 3.5) + cos(pi / 3.5) * cos(pi / 3.5)) / 3.0'
# Result:
# 0.333333333333333
# Google result:
# 0.33333333333
#
# Example 2:
# ./scicalc '(123.456**(1.2/3.4))'
# Result:
# 5.472437081627787
# Google result:
# 5.47243708163

Results on both this MBP and the AMIGA, not tested in Linux flavours:
Code:
Last login: Sun Jul 14 19:08:41 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./scicalc '(sin(pi / 3.5) * sin(pi / 3.5) + cos(pi / 3.5) * cos(pi / 3.5)) / 3.0'
0.333333333333333
AMIGA:amiga~/Desktop/Code/Shell> ./scicalc '123.456**(1.2/3.4)'
5.472437081627787
AMIGA:amiga~/Desktop/Code/Shell> NUM='123.456**(1.2/3.4)'
AMIGA:amiga~/Desktop/Code/Shell> ./scicalc "$NUM"
5.472437081627787
AMIGA:amiga~/Desktop/Code/Shell> ./scicalc "$NUM" > /tmp/text
AMIGA:amiga~/Desktop/Code/Shell> cat /tmp/text
5.472437081627787
AMIGA:amiga~/Desktop/Code/Shell> ./scicalc
Basic USAGE: [./]scicalc [']["]expression_inside_quotes["][']<CR>
!!!Read the code for more information!!!
AMIGA:amiga~/Desktop/Code/Shell> echo "$?"
1
AMIGA:amiga~/Desktop/Code/Shell> _

I think this is good enough for the average user...
These 2 Users Gave Thanks to wisecracker For This Post:
 

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple script to return environment variable

HI , In the below script I am trying to return the value of the environment variable TIBCO_HOME to the caller #! /usr/bin/csh set VAR_NAME=$1 echo VAR_NAME On the aix console.. set to setenv TIBCO_HOME /app/tibco When I execute the script... myscript.sh TIBCO_HOME, the script... (5 Replies)
Discussion started by: bce_groups
5 Replies

2. Shell Programming and Scripting

How to calculate unix epoch time with DC(Calculator)

Hi All, In our code we are using below code to calculate the UNIX Epoch Time from the time stamp present in the file. Can any one explain how the DC operates in below command and how we calculate the UNIX EPOCH time from this. Date incide the file is 20:26:51 Output we get is 1289943554... (3 Replies)
Discussion started by: mithunpatel
3 Replies

3. Shell Programming and Scripting

Simple maths calculator loop.

Hi, I am trying to make a maths calculator that: 1. Prompts the user for a number. 2. Prompts the user for an operation (add, subtract, divide or multiply) 3. Prompts the user for a number. 4. Prompts the user for another operation (same as above) OR the option to get the result for the... (4 Replies)
Discussion started by: johnthebaptist
4 Replies

4. Shell Programming and Scripting

Simple calculator with menu input - Need Help

I am trying to make a calculator. The user Enters number 1, chooses and operation, enters number 2, then chooses another operation or for the answer to be displayed. eg. 1 + 1 = or 1 + 1 + 2 + 1 = Both of these should be possible. #!/bin/bash read -p "what's the first number? " n1... (3 Replies)
Discussion started by: redshine6
3 Replies

5. Homework & Coursework Questions

Simple Calculator

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known/data: Script a simple calculator. In the command line enter the script file /home/etc/mycalc or /home/etc/mycalc 1 +... (6 Replies)
Discussion started by: herb bertz
6 Replies

6. Programming

Casio FX-CG50 Scientific Calculator and Python

Well, I think I must have this for Christmas! Python on the Casio FX-CG50 with Data Acquisition YT: CASIO fx-CG50 E-CON4 Movie YT: Update Your CG50 Operating System. Now Includes Python! (OS update) This is a great deal on Amazon! See YT Code Tag Video (9 Replies)
Discussion started by: Neo
9 Replies
All times are GMT -4. The time now is 06:31 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy