A fixed point basic calculator for DASH.


 
Thread Tools Search this Thread
Operating Systems OS X (Apple) A fixed point basic calculator for DASH.
# 8  
Old 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

function readnum {
        IFS="." read N FRAC <<<"$1"
        [ "$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
}

readnum "0.5"

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  
Old 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

function readnum {
        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
}

readnum "0.5"

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:
# 10  
Old 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"'.'

read -r INT FLOAT <<EOF
$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  
Old 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:
# 12  
Old 11-07-2018
Hi MadeInGermany...

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  
Old 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  
Old 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A dash to GOTO or a dash from GOTO, that is the question...

Well, guys I saw a question about GOTO for Python. So this gave me the inspiration to attempt a GOTO function for 'dash', (bash and ksh too). Machine: MBP OSX 10.14.3, default bash terminal, calling '#!/usr/local/bin/dash'... This is purely a fun project to see if it is possible in PURE... (3 Replies)
Discussion started by: wisecracker
3 Replies

2. UNIX for Beginners Questions & Answers

How to create a new mount point with 600GB and add 350 GBexisting mount point? IN AIX

How to create a new mount point with 600GB and add 350 GBexisting mount point Best if there step that i can follow or execute before i mount or add diskspace IN AIX Thanks (2 Replies)
Discussion started by: Thilagarajan
2 Replies

3. Shell Programming and Scripting

Fixed mount point for a USB cardreader (Raspberry Pi, UDEV)

Hey all! :) I'm trying to create a fixed mount point for an usb cardreader. I've found a script on a raspberry pi forum which does the following: usb stick is plugged in -> script checks the mount point for data -> script starts copying the files automatically -> script unmounts the... (0 Replies)
Discussion started by: Eomer
0 Replies

4. Post Here to Contact Site Administrators and Moderators

How to sum up data in fixed width file with decimal point?

HI Everyone, I have below source file AAA|NAME1|ADDRESS1|300.20 BBB|NAME2|ADDRESS2|400.31 CCC|NAME3|ADDRESS3|300.34 I have requirement where I need to sum up fourth field in above fixed width pipe delimited flat file. When I use below code, it gives me value 1001.00 But I am expecting... (1 Reply)
Discussion started by: patricjemmy6
1 Replies

5. Shell Programming and Scripting

How to perform a hexdump using dd from start point to end point?

hi, I would like to ask or is it possible to dump a hex using dd from starting point to end point just like the "xxd -s 512 -l 512 <bin file>" I know the redirect hexdump -C but i can't figure it out the combination options of dd. Hope someone can share their knowledge.. Thanks in... (3 Replies)
Discussion started by: jao_madn
3 Replies

6. UNIX for Dummies Questions & Answers

dash after ampersant

Hi! I'm new in these forums and more or less new with Unix. So... here is the question: does anyone know where is redirected the output of a command when you put >&- after it? Does it means any standard file descriptor? Thanks! (2 Replies)
Discussion started by: csecnarf
2 Replies

7. UNIX for Advanced & Expert Users

Fibre connection Point to Point SUN

Anyone know of a guide or instructions for Solaris I got to configure a SBUS HBA to talk to a tape robot. I have done this on a switch but not point to point. just going HBA >>>>> TAPE Fibre simple two nodes Kie (6 Replies)
Discussion started by: kie
6 Replies
Login or Register to Ask a Question