Sponsored Content
Full Discussion: Decimals in TCSH
Top Forums Shell Programming and Scripting Decimals in TCSH Post 302389008 by drl on Friday 22nd of January 2010 06:36:25 AM
Old 01-22-2010
Hi.

Here's one alternative:
Code:
#!/usr/bin/env tcsh

# @(#) s1	Demonstrate alternative for tcsh floating-point arithmetic.

echo
setenv LC_ALL C ; setenv LANG C
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility version)"
sh -c "version >/dev/null 2>&1" && version "=o" tcsh
echo

alias acalc 'awk "BEGIN{ print \!* }" '

# START RI LOOP
set RI_min = 10
set RI_max = 30
set RI_inc = 1

set RI = $RI_min
while ($RI <= $RI_max)

# @ PI=$RI / 10
set PI = `acalc $RI / 10`
# echo "PI = " $PI "g cm^-3"
# echo $RI
echo " RI = $RI,  PI = " $PI "g cm^-3"
@ RI = $RI + $RI_inc

end

exit 0

producing:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility version)
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
tcsh 6.14.00

 RI = 10,  PI =  1 g cm^-3
 RI = 11,  PI =  1.1 g cm^-3
 RI = 12,  PI =  1.2 g cm^-3
 RI = 13,  PI =  1.3 g cm^-3
 RI = 14,  PI =  1.4 g cm^-3
 RI = 15,  PI =  1.5 g cm^-3
 RI = 16,  PI =  1.6 g cm^-3
 RI = 17,  PI =  1.7 g cm^-3
 RI = 18,  PI =  1.8 g cm^-3
 RI = 19,  PI =  1.9 g cm^-3
 RI = 20,  PI =  2 g cm^-3
 RI = 21,  PI =  2.1 g cm^-3
 RI = 22,  PI =  2.2 g cm^-3
 RI = 23,  PI =  2.3 g cm^-3
 RI = 24,  PI =  2.4 g cm^-3
 RI = 25,  PI =  2.5 g cm^-3
 RI = 26,  PI =  2.6 g cm^-3
 RI = 27,  PI =  2.7 g cm^-3
 RI = 28,  PI =  2.8 g cm^-3
 RI = 29,  PI =  2.9 g cm^-3
 RI = 30,  PI =  3 g cm^-3

See Floating Point for a brief explanation of the alias.

Note that many people will advise you to avoid tcsh scripting. Using Bourne-shell relatives seems to work best for most projects. Here's an example of zsh illustrating built-in floating-point arithmetic (in later versions of ksh as well):
Code:
#!/usr/bin/env zsh

# @(#) s2	Demonstrate zsh floating-point arithmetic.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) ksh
set -o nounset

echo
i=1
while (( i < 3.0 ))
do
  print " i = $i"
  (( i+=0.5 ))
done

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
zsh 4.3.6
ksh 93s+

 i = 1
 i = 1.5
 i = 2.
 i = 2.5

Best wishes ... cheers, drl
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

comparing two numbers with the decimals

Can someone tell me how do I comapre two numbers with the decimals in UNIX shell scripting I understand "-gt" can be used only for integers Regards, Giri (4 Replies)
Discussion started by: chittari
4 Replies

2. Shell Programming and Scripting

Multiplying Floats/Decimals

Is there a way that i can get something like this to work: Number=`expr 80 \* 10.69` i.e. To multiply an integer by a decimal or a decimal by a decimal etc...? thanks (10 Replies)
Discussion started by: rleebife
10 Replies

3. Shell Programming and Scripting

handle decimals

Hi All, How we can handle decimals in (Float) in UNIX. a=73 b=5 c=`expr a / b` i am getting 14 but i need full 14.6 . Can any one help me pls? (1 Reply)
Discussion started by: subin_bala
1 Replies

4. Shell Programming and Scripting

Bourne and decimals??

I need to get 15% of the variable exer1 to be added to other exercises so far, i've got exer1=$1 aver=`expr $exer \* .15` but i keep getting an error that an integer value was expected. Is there anyway around this? (1 Reply)
Discussion started by: kdyzsa
1 Replies

5. Shell Programming and Scripting

removing and rounding up decimals

Hi Experts, I have a command that gives me the output as below root@ckpgpay11core> cat sara | awk '{ sum += $1} ; END { print sum }' | awk {'print $1/90'} 8.88889 how do i remove the decimal spaces so that the figure will round itself to 9? Thanks. (3 Replies)
Discussion started by: aismann
3 Replies

6. Shell Programming and Scripting

convert Regular decimals to Packed decimals

Hi, I am trying to find if there is a way to convert regular decimal values to Paced decimal values. I tried to find a c program but I could get a Packed converted to regular decimal not the other way round. If not unix please let me know if any other progrimming language I can use to do... (2 Replies)
Discussion started by: mgirinath
2 Replies

7. UNIX for Dummies Questions & Answers

Regarding Decimals in Cshell

Hello... I am new to unix and I am wondering if in a C-shell script , Are we supposed to use only whole numbers........ for example..if a program needs to calculate the average of some numbers........ @ avg = (($1 +$2 + $3)/3)) is returning a whole number.........How can a decimal be achieved... (1 Reply)
Discussion started by: ravindra22
1 Replies

8. Shell Programming and Scripting

Round up the decimals

Hi All, I would like to do the following in the shell script 561.76 to 562 I tried using this echo 'scale=0; 749 * 75 /100 ' | bc but just returned only 561 Please help me . I appreciate your help Thanks rajeevm (13 Replies)
Discussion started by: rajeevm
13 Replies

9. Shell Programming and Scripting

Getting date in seconds with decimals

I am trying to get date to display decimal Desired output 1350386096256.12 I know this can be done with printf, but are not able to make it work. I have tested this and many otherprintf "%.2f" $(($(date +%s%N)/1000000)) (8 Replies)
Discussion started by: Jotne
8 Replies

10. Shell Programming and Scripting

how to find the field has more than 2 decimals

Hi Gurus, I have below sample file, I need find the line which 2rd field has more than 2 decimals. in sample file, I need to find xyz, 123456.789 abc, 1234.45, def xyz, 123456.789, xxx bce, 1234.34, xxx thanks in advance (13 Replies)
Discussion started by: ken6503
13 Replies
foreach(n)						       Tcl Built-In Commands							foreach(n)

__________________________________________________________________________________________________________________________________________________

NAME
foreach - Iterate over all elements in one or more lists SYNOPSIS
foreach varname list body foreach varlist1 list1 ?varlist2 list2 ...? body _________________________________________________________________ DESCRIPTION
The foreach command implements a loop where the loop variable(s) take on values from one or more lists. In the simplest case there is one loop variable, varname, and one list, list, that is a list of values to assign to varname. The body argument is a Tcl script. For each element of list (in order from first to last), foreach assigns the contents of the element to varname as if the lindex command had been used to extract the element, then calls the Tcl interpreter to execute body. In the general case there can be more than one value list (e.g., list1 and list2), and each value list can be associated with a list of loop variables (e.g., varlist1 and varlist2). During each iteration of the loop the variables of each varlist are assigned consecutive values from the corresponding list. Values in each list are used in order from first to last, and each value is used exactly once. The total number of loop iterations is large enough to use up all the values from all the value lists. If a value list does not contain enough elements for each of its loop variables in each iteration, empty values are used for the missing elements. The break and continue statements may be invoked inside body, with the same effect as in the for command. Foreach returns an empty string. EXAMPLES
This loop prints every value in a list together with the square and cube of the value: set values {1 3 5 7 2 4 6 8} ;# Odd numbers first, for fun! puts "Value Square Cube" ;# Neat-looking header foreach x $values { ;# Now loop and print... puts " $x [expr {$x**2}] [expr {$x**3}]" } The following loop uses i and j as loop variables to iterate over pairs of elements of a single list. set x {} foreach {i j} {a b c d e f} { lappend x $j $i } # The value of x is "b a d c f e" # There are 3 iterations of the loop. The next loop uses i and j to iterate over two lists in parallel. set x {} foreach i {a b c} j {d e f g} { lappend x $i $j } # The value of x is "a d b e c f {} g" # There are 4 iterations of the loop. The two forms are combined in the following example. set x {} foreach i {a b c} {j k} {d e f g} { lappend x $i $j $k } # The value of x is "a d e b f g c {} {}" # There are 3 iterations of the loop. SEE ALSO
for(n), while(n), break(n), continue(n) KEYWORDS
foreach, iteration, list, looping Tcl foreach(n)
All times are GMT -4. The time now is 10:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy