Home
Man
Search
Today's Posts
Register

OS X is a line of Unix-based graphical operating systems developed, marketed, and sold by Apple.

A fixed point basic calculator for DASH.

Tags
basic, calculator, dash, point

Login to Reply

 
Thread Tools Search this Thread
# 8  
Old 1 Week Ago
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 1 Week Ago
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; 1 Week Ago at 11:58 AM..
The Following User Says Thank You to Corona688 For This Useful Post:
wisecracker (1 Week Ago)
# 10  
Old 1 Week Ago
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; 1 Week Ago at 06:56 PM.. Reason: Add my own text mode thumbs up...
# 11  
Old 1 Week Ago
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; 1 Week Ago at 07:24 PM..
The Following User Says Thank You to MadeInGermany For This Useful Post:
wisecracker (1 Week Ago)
# 12  
Old 1 Week Ago
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 1 Week Ago
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 1 Week Ago
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 to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Fixed mount point for a USB cardreader (Raspberry Pi, UDEV) Eomer Shell Programming and Scripting 0 04-17-2016 01:15 PM
How to sum up data in fixed width file with decimal point? patricjemmy6 Post Here to Contact Site Administrators and Moderators 1 02-24-2015 07:51 PM
Exclude dash in grep Subbeh Shell Programming and Scripting 3 01-21-2013 06:58 AM
How to perform a hexdump using dd from start point to end point? jao_madn Shell Programming and Scripting 3 04-08-2012 04:32 AM
email address starting with dash akaq AIX 1 06-26-2009 05:52 AM
remove dash melanie_pfefer Shell Programming and Scripting 1 01-26-2009 06:05 AM
ignoring a dash in file name LumpSum UNIX for Dummies Questions & Answers 5 08-04-2008 01:47 PM
dash after ampersant csecnarf UNIX for Dummies Questions & Answers 2 03-12-2008 04:47 PM
double-dash options dhinge Shell Programming and Scripting 1 01-10-2007 10:32 PM
Fibre connection Point to Point SUN kie UNIX for Advanced & Expert Users 6 05-20-2003 12:20 PM


All times are GMT -4. The time now is 11:41 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password