adding float numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding float numbers
# 1  
Old 05-10-2005
adding float numbers

how to add 2 float values to each other?
i tried this and it doesnt work:
Code:
#!bin/bash
numone=1.234
numtwo=0.124
total=`expr $numone + $numtwo`
echo $total

thanks
# 2  
Old 05-10-2005
Try:

total=`echo $numone + $numtwo | bc`
# 3  
Old 05-10-2005
thanks for that
# 4  
Old 08-29-2005
Error

How would I read in values from a text file and do the exact thing?

Text file contains 2 values: test.txt

1.51 -2.76

I want to read these values from test.txt and assign them to numone and numtwo:

# !bin/bash
numone=test.txt-value1
numtwo=test.txt-value2
total=`echo $numone + $numtwo | bc`
echo $total
# 5  
Old 08-29-2005
Code:
#!/bin/ksh

read numone numtwo < test.txt
total=$(echo "scale=2; ${numone} + ${numtwo}" | bc)

echo "[${numone}] + [${numtwo}] = [${total}]"

For the future, pls create a new thread for the unrelated questions.
# 6  
Old 08-29-2005
Thanks. Will do.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help about using variables of float numbers in sed

Hi, I need to run a Fortran program which reads a input file with a fixed name many times, each time I need to change a number (real) in that input file, this is how I currently do it and I know it is not elegent at all: cp inputfile.dat backup.dat sed -i 's/28.0/0.01/g' inputfile.dat ./myCode... (3 Replies)
Discussion started by: dypang
3 Replies

2. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

3. Shell Programming and Scripting

help with adding up numbers

I have a file which has following contents which I want to add up. 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 28170.24 139038.72 139038.72 139038.72 139038.72 (5 Replies)
Discussion started by: aksijain
5 Replies

4. Shell Programming and Scripting

Random float numbers in BASH

Hi people :) I'm learning shell scripting using bash and I want to generate 4 floating point number with 5 decimal places and write them to a file and a variable. I've done all this except the $RAMDOM enviroment variable does not generate a float number but a integrer. I hope you could... (3 Replies)
Discussion started by: pharaoh
3 Replies

5. Programming

comparison between float numbers

Hi, i have a simple control like this: if(sum>1.0)... If i try to print sum i get 1.000000 but the check returns true. I think it depends on float precision. How can i modify the check? thanks (1 Reply)
Discussion started by: littleboyblu
1 Replies

6. Solaris

can array store float point numbers

Hi all, I have doubt can array in a shell script can store floating point numbers. i have tired. but i unable to work it out. Please help me regarding this Thank U Naree (1 Reply)
Discussion started by: naree
1 Replies

7. Solaris

i cannot assign float point numbers to an array in solaris

total=0 declare -a sum limit=`iostat -En | grep -i size | awk '{print $2}' | sed -e 's/GB//g' | wc -l` echo "Limit is equal to $limit" ara="`iostat -En | grep -i size | awk '{print $2}' | sed -e 's/GB//g'`" for (( i=1; i<=$limit; i++ )) do sum=`echo $ara | cut -d " " -f $i` echo ${sum}... (11 Replies)
Discussion started by: naree
11 Replies

8. Programming

math.h: float ceilf(float x)

Good morning, I'm testing the use of ceilf: /*Filename: str.c*/ #include <stdio.h> #include <math.h> int main (void) { float ceilf(float x); int dev=3, result=0; float tmp = 3.444f; printf("Result: %f\n",ceilf(tmp)); return 0; } (1 Reply)
Discussion started by: jonas.gabriel
1 Replies

9. Shell Programming and Scripting

Adding 2 numbers

I would like to write a script with BASH to add two numbers (integer) and write the result to the standard output. Please help! (7 Replies)
Discussion started by: Viper01
7 Replies
Login or Register to Ask a Question