problem with floating point number loops


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with floating point number loops
# 1  
Old 06-18-2010
problem with floating point number loops

Hey,

I guess I am just to stupid and am not seeing the "wood for the trees", but I am always getting strange errors.

I want to create a mesh with coordinates like:

x y z
3.1 3.0 0.75 0 0 1
3.1 2.9 0.75 0 0 1
3.1 2.8 0.75 0 0 1
3.1 2.7 0.75 0 0 1
3.0 3.0 0.75 0 0 1
3.0 2.9 0.75 0 0 1
3.0 2.8 0.75 0 0 1
3.0 2.7 0.75 0 0 1

Here is my skript:

Code:
echo "Abstand Berechnungspunkte in Meter="
read abstand

z=0.75
a=0
b=0
c=1

for (( i=$abstand; i<0; i=$i+$abstand )) 
do
			for (( h=$abstand; h<4.61; h=$h+$abstand )) 
			do
						x=`echo "-3.616+$i" |bc -l `
						y=`echo "-0.085+$h" |bc -l `
						print $x " " $y " " $z " " $a " " $b " " $c
			done
done

or I also tried this one:

Code:
echo "Abstand Berechnungspunkte in Meter="
read abstand 

    z=.75
    a=0
    b=0
    c=1
    ab=$abstand
    i=-3.616
    h=0
    maxx=0
    maxy=4.61
    
    while [ $i -le $maxx ]
    do 
            x=`echo "-3.616+$i" |bc -l`
            i=`echo "$i+$ab" |bc -l`
    done

But it is always creating the same problem regarding numbers with commata. Becasue my input is e.g., 0.02
line 11: ((: i=0.1: syntax error: invalid arithmetic operator (error token is ".1")

Does anyone has an idea? Thanks for any help and sorry for the stupid question,
Sam
# 2  
Old 06-18-2010
The shell will not allow you to loop using a floating point value. As a general rule floating point values should not be used in loop counters even when the language allows it.
# 3  
Old 06-18-2010
ok. thanks.

Do you then have an idea how to realize such a grid by a loop easily?

And thanks for your reply, sam
# 4  
Old 06-18-2010
A possible work-around: convert all meter into centimeter values, e.g. by means of sed or awk Smilie
# 5  
Old 06-18-2010
Quote:
Originally Posted by ergy1983
ok. thanks.

Do you then have an idea how to realize such a grid by a loop easily?

And thanks for your reply, sam
My Deutsch isn't all that great, so to make sure I'm clear on what you're doing here - you want to find the number of steps between between two constant values.

Isn't this essentially a division problem? For example if the abstand=2, your xmax is 6 and your ymax is 4 the output table would be:

Code:
6 4 0.75 0 0 1
6 2 0.75 0 0 1
6 0 0.75 0 0 1
4 4 0.75 0 0 1
4 2 0.75 0 0 1
4 0 0.75 0 0 1
2 4 0.75 0 0 1
2 2 0.75 0 0 1
2 0 0.75 0 0 1
0 4 0.75 0 0 1
0 2 0.75 0 0 1
0 0 0.75 0 0 1

Also, are you limited to using a shell script? The only way I know of to do floating point comparisons in bash is pretty kludgy. If you have perl available and my understanding is correct I could definitely create a script to handle this.

Quote:
Originally Posted by dr.house
A possible work-around: convert all meter into centimeter values, e.g. by means of sed or awk Smilie
This would only push the failure point to millimeters.
# 6  
Old 06-18-2010
Even floating point has a failure point. The question is how much accuracy is necessary.
# 7  
Old 06-18-2010
Quote:
Originally Posted by Corona688
Even floating point has a failure point. The question is how much accuracy is necessary.
Math::BigFloat could express atoms in meters, so I figured that wouldn't be an issue. Likewise, I assumed that more than a couple of decimal places were necessary. If this is just for a limited degree of accuracy an expr or bc kludge would be sufficient, but I wouldn't want to maintain it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to print the smallest floating point number in a row that is not 0

Hello, I have often found bash to be difficult when it comes to floating point numbers. I have data with rows of tab delimited floating point numbers. I need to find the smallest number in each row that is not 0.0. Numbers can be negative and they do not come in any particular order for a given... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. Shell Programming and Scripting

Convert floating point to a number

Hello Guys, I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value #!/bin/bash #... (9 Replies)
Discussion started by: skatpally
9 Replies

3. Shell Programming and Scripting

[BASH] Regex for floating point number

Hey again, I have a basic regex that tests if a number is a float. Thank you. (5 Replies)
Discussion started by: whyte_rhyno
5 Replies

4. Programming

Floating Point

Anyone help me i cant found the error of floating point if needed, i added the code complete #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> typedef struct { int hh; int mm; int ss; char nom; int punt; }cancion; typedef struct... (9 Replies)
Discussion started by: Slasho
9 Replies

5. Shell Programming and Scripting

floating point number problem

Hello folks I Hope everyone is fine. I am calculating number of bytes calculation from apache web log. awk '{ sum += $10 } END { print sum }' /var/httpd/log/mydomain.log 7.45557e+09 it show above number, what should i do it sow number like 7455, i mean if after decimal point above 5 it... (5 Replies)
Discussion started by: learnbash
5 Replies

6. Shell Programming and Scripting

Arithmetic in floating point

is it not possible to simply di aritmetic without using bc or awk i have tried folllowing operatrions but they support only integer types plz suggest me code for floating using values stored in the variables.the ans i get is integer and if i input floating values i get error numeric constant... (6 Replies)
Discussion started by: sumit the cool
6 Replies

7. Shell Programming and Scripting

floating point numbers in if

# if > then > echo "1" > else > echo "2" > fi -bash: How can i compare floating point numbers inside statement? (15 Replies)
Discussion started by: proactiveaditya
15 Replies

8. Shell Programming and Scripting

using bc with floating point number in files

Hi, I' using bash and I would like to use "bc" to compute the ratio of of two numbers and assign the ratio to a variable. The numbers are in a file, e.g. 196.304492 615.348986 Any idea how to do it? N.B. I cannot change the file to have 196.304492 / 615.348986 as the file is produced by... (14 Replies)
Discussion started by: f_o_555
14 Replies

9. Programming

floating point problem

Hi all! Hi all! I am working with a problem to find the smallest floating point number that can be represented. I am going in a loop ,stating with an initial value of 1.0 and then diving it by 10 each time thru the loop. So the first time I am getting o.1 which I wanted.But from the next... (4 Replies)
Discussion started by: vijlak
4 Replies

10. Shell Programming and Scripting

problem with floating point numbers in awk

hi all, i have the following problem using awk in a script i want to read the values from a column with real numbers and calculate the mean.the problem is that when i use a statement such as this num = $4 i cant find a way to convert the variable from string to floating point to perform... (7 Replies)
Discussion started by: kanagias
7 Replies
Login or Register to Ask a Question