A fixed point basic calculator for DASH.

 Tags basic, calculator, dash, point

# 8
11-06-2018
Quote:
Originally Posted by Corona688
Here's a quick bash-only version for positive numbers. The trick is reading the values in. Once you do that the rest is straightforward.

Code:
```DP=3
MOD=1000

[ "\$N" = "0" ] && N=""
for((X=0; X<DP; X++))
do
D="\${FRAC:\$X:1}"
[ -z "\$D" ] && D="0"
N="\$N\$D" # Prepend digits to N
done
}

echo "Integer \$N is fixed point \$((N/MOD)).\$((N%MOD))"```

I can see what you are doing, but forgive my ignorance; apart from the obvious bashisms, how do I use 'read' to input values directly from command line arguments without disk thrashing in POSIX 'dash'?
Everything MUST centre around 'dash' or POSIX 'sh'.
As for 'zsh', I can't be responsible for a non POSIX compliant shell.
This is the whole point of this [futile] exercise to see what is possible and what better than Fixed Point Arithmetic.
And I already assume a "fractional" part of 1000000000 and use "%.9f" to add any zeros to the end.
Anyhow I will try out your "MINUS" modification tomorrow.
Multiplication and division are harder still in 'dash'.

Back tomorrow.
HTH.

Bazza.
# 9
11-07-2018
I'm sure DASH can divide and multiply absolutely fine. It has * and /.

Quote:
And I already assume a "fractional" part of 1000000000 and use "%.9f" to add any zeros to the end.
I noticed, but the way you're doing it is perhaps the hardest possible way. If you just keep your number as an integer, you can calculate it as an integer, because it is an integer. Addition and subtraction work completely straight. Multiplication and division will need correction before and afterwards respectively. And you will have to stay away from the integer limit because the result of multiplying 64-bit MAXINT by 64-bit MAXINT is a 128-bit number.

Anyway, you can use a here-document to feed values into read. There's another, older trick though, which works especially well in functions and should work anywhere.

Code:
```DP=3
MOD=1000

OLDIFS="\$IFS"
IFS="."
set -- \$1 # Set \$1, \$2, etc, splitting on "."
IFS="\$OLDIFS"

N=\$1
FRAC=\$2

[ "\$N" = "0" ] && N=""

for((X=0; X<DP; X++))
do
D="\${FRAC:\$X:1}"
[ -z "\$D" ] && D="0"
N="\$N\$D" # Prepend digits to N
done
}

echo "Integer \$N is fixed point \$((N/MOD)).\$((N%MOD))"```

Last edited by Corona688; 11-07-2018 at 11:58 AM..
This User Gave Thanks to Corona688 For This Post:
wisecracker (11-07-2018)
# 10
11-07-2018
Hi Corona688...

Quote:
Anyway, you can use a here-document to feed values into read.
Code:
```#!/usr/local/bin/dash
# heredoc.sh
IFS="\$IFS"'.'

\$1
EOF
echo "\$INT \$FLOAT"```

WOW! I didn't find anything about this on the mighty WWW. This earns you a big thank you and certainly does open up new avenues...
Results on my usual gear:
Code:
```Last login: Wed Nov  7 19:10:03 on console
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> chmod 755 heredoc.sh
AMIGA:amiga~/Desktop/Code/Shell> ./heredoc.sh 123.456
123 456
AMIGA:amiga~/Desktop/Code/Shell> _```

Quote:
There's another, older trick though, which works especially well in functions and should work anywhere.
Now this I would be interested in, please post an example ASAP.
I will re-do my code accordingly and check against Python. Gimme a bit of time.

This is why I am an amateur and you are a professional, my knowledge is far less than yours and others in this sphere.

<thumb-up-smilie-here>
EDIT:
Done my own. ;o)
Code:
```	        .--.
\   \
.   .
|   |
|   |
|   |
____/     `-----------.
/                       )
|           +----------.'
|                       )
|           +---------.'
|                      )
|           +--------.'
\                    )
'------------------'```

Bazza...

Last edited by wisecracker; 11-07-2018 at 06:56 PM.. Reason: Add my own text mode thumbs up...
# 11
11-07-2018
Code:
`IFS="\$IFS"'.'`

has a permanent effect, so you often do
Code:
```oldIFS=\$IFS
IFS="\$IFS"'.'
# shell code that needs the modified IFS
# restore the old IFS
IFS=\$oldIFS
# normal shell code```

Sometimes you can use a sub shell:
Code:
```(
# this is a sub shell
IFS="\$IFS"'.'
# shell code that needs the modified IFS
)
# in the main shell there is the old IFS```

read is a command, and you can set the environment just for the command
Code:
```IFS="\$IFS"'.' read ...
# after the command there is the old IFS```

Unfortunately set is not a command. (You can call it a pseudo command or special command. The shell does not fork/exec like with a normal command, therefore IFS must be set beforehand.)

Last edited by MadeInGermany; 11-07-2018 at 07:24 PM..
This User Gave Thanks to MadeInGermany For This Post:
wisecracker (11-07-2018)
# 12
11-07-2018

I was well aware of 'IFS' being permanent but the test code was proof of concept rather than a dedicated program.
If C688 had not mentioned the heredoc method then then I would not have known about it. My searching prowess must be poor using Google.
Having said that this is neat too, as it is also new to me:
Code:
```(
# this is a sub shell
IFS="\$IFS"'.'
# shell code that needs the modified IFS
)
# in the main shell there is the old IFS```

So you get a thumbs up too:
Code:
```	        .--.
\   \
.   .
|   |
|   |
|   |
____/     `-----------.
/                       )
|           +----------.'
|                       )
|           +---------.'
|                      )
|           +--------.'
\                    )
'------------------'```

Bazza.
# 13
11-08-2018
Quote:
Originally Posted by wisecracker
Now this I would be interested in, please post an example ASAP.
I did? It is set -- arg1 arg2 arg3 ... which sets the \$1 \$2 \$3 variables.

If you run it like set -- \$variable it will split upon IFS, which is whatever you please, giving an easy way to split a string upon "." even in ancient shells which lack most modern string built-ins.

It works especially well in functions because otherwise you'd have to worry about overwriting the global \$1 \$2 etc, but inside a function you're only dealing with local ones, which will be gone soon anyway.
# 14
11-08-2018
Quote:
Originally Posted by wisecracker
Code:
```(
# this is a sub shell
IFS="\$IFS"'.'
# shell code that needs the modified IFS
)
# in the main shell there is the old IFS```

Beware that not only IFS is preserved -- every variable is preserved. Variable assignments inside the new shell don't copy into the old one. It's just like running commands behind a pipe.