strange problem with gawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting strange problem with gawk
# 1  
Old 11-09-2007
Bug strange problem with gawk

Hello Freinds,

I am facing a starange problem with gawk .

My programme is as follows

#!/bin/sh

gawk -v AMPLITUDE=$1 -v freq=$2 ' \
BEGIN{
for(i=0; i<=2559; i++){
validec = sprintf("%1.3f", AMPLITUDE * sin( (i/2559) * 2* freq * 3.14159 ) )
validec = floor(validec);
print "=> "validec
itxdata = toHEXA(validec,3)
}
}
#================
#Function For flooring
#================
function floor(x,r)
{
r = int(x) ;
print "x=>" x
print "r=>" r
if (r > x)
r--;
return ( r + 0 );
}
'
I m running the script by giving a two arguments like
./<scriptname> 2000 10

My problem is when the floor function is going for a negative number
such as when it passes -2.450 it returns me -2 instead of -3. But in nawk it is ok.

And when I am using the floor function in a separate programe alone

as below it gives me the correct answer ie, -3

#!/bin/sh
gawk ' \
BEGIN{
val = floor(-2.45);
print "AFTER Flooring " val
}
function floor(x,r)
{
r = int(x) ;
print "x=>" x
print "r=>" r
if (r > x)
r--;
return ( r + 0 );
}
'

Actually in the first programe gawk the statement if(-2 > -2.45) why goes false I ve no idea.

I m curious to know what is the problem with gawk Please help..
Thanks in Adavance
user_prady

Last edited by user_prady; 11-09-2007 at 02:33 AM..
# 2  
Old 11-09-2007
In the first example you are passing 'x' as a string, in the second as a number.
So that's why the 'if' does not behave as you expect.
# 3  
Old 11-09-2007
Quote:
Originally Posted by Ygor
In the first example you are passing 'x' as a string, in the second as a number.
So that's why the 'if' does not behave as you expect.
Thnaks for your kind reply ..

Oh is it the problem !!!

So if that the problem I ll multiply the string with one(1) then I ll pass it and it should give me my expected result.

But I tried that one it won't work , Is there any other way that I can send it as number not as a string..

user_prady

Last edited by user_prady; 11-09-2007 at 04:34 AM..
# 4  
Old 11-09-2007
This may or may not be an issue but ...

Your floor function is expecting two parameters i.e. floor(x,r).
You are passing in only one parameter i.e. x.
# 5  
Old 11-09-2007
Quote:
Originally Posted by fpmurphy
This may or may not be an issue but ...

Your floor function is expecting two parameters i.e. floor(x,r).
You are passing in only one parameter i.e. x.
The second parameter is defined to be used as a local variable.

Jean-Pierre.
# 6  
Old 11-11-2007
Quote:
Originally Posted by user_prady
But I tried that one it won't work , Is there any other way that I can send it as number not as a string..

user_prady
Passing a number a number works as expected:
Code:
$ gawk ' \
> BEGIN{
> val = floor(-2.45);
> print "AFTER Flooring " val
> }
> function floor(x,r)
> {
> r = int(x) ;
> print "x=>" x
> print "r=>" r
> if (r > x)
> r--;
> return ( r + 0 );
> }
> '
x=>-2.45
r=>-2
AFTER Flooring -3

Passing a string does not work:
Code:
$ gawk ' \
> BEGIN{
> val = floor("-2.45");
> print "AFTER Flooring " val
> }
> function floor(x,r)
> {
> r = int(x) ;
> print "x=>" x
> print "r=>" r
> if (r > x)
> r--;
> return ( r + 0 );
> }
> '
x=>-2.45
r=>-2
AFTER Flooring -2

Adding a zero to the parameter will convert it from a string to a number:
Code:
$ gawk ' \
> BEGIN{
> val = floor("-2.45" + 0);
> print "AFTER Flooring " val
> }
> function floor(x,r)
> {
> r = int(x) ;
> print "x=>" x
> print "r=>" r
> if (r > x)
> r--;
> return ( r + 0 );
> }
> '
x=>-2.45
r=>-2
AFTER Flooring -3

# 7  
Old 11-12-2007
Bug

Quote:
Originally Posted by Ygor
Passing a number a number works as expected:
Code:
$ gawk ' \
> BEGIN{
> val = floor(-2.45);
> print "AFTER Flooring " val
> }
> function floor(x,r)
> {
> r = int(x) ;
> print "x=>" x
> print "r=>" r
> if (r > x)
> r--;
> return ( r + 0 );
> }
> '
x=>-2.45
r=>-2
AFTER Flooring -3

Passing a string does not work:
Code:
$ gawk ' \
> BEGIN{
> val = floor("-2.45");
> print "AFTER Flooring " val
> }
> function floor(x,r)
> {
> r = int(x) ;
> print "x=>" x
> print "r=>" r
> if (r > x)
> r--;
> return ( r + 0 );
> }
> '
x=>-2.45
r=>-2
AFTER Flooring -2

Adding a zero to the parameter will convert it from a string to a number:
Code:
$ gawk ' \
> BEGIN{
> val = floor("-2.45" + 0);
> print "AFTER Flooring " val
> }
> function floor(x,r)
> {
> r = int(x) ;
> print "x=>" x
> print "r=>" r
> if (r > x)
> r--;
> return ( r + 0 );
> }
> '
x=>-2.45
r=>-2
AFTER Flooring -3

Yes that's the answer . I am now getting the exact answer..
Thank You all again...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem using gsub in gawk

I know that most of you guys probably won't reply to this, because you'll feel like my post doesn't even deserve a glance. However, for those of you that do decide to help- thank you! TRAILS TA 2700 E Main St ALBERT LEA , MN 56007 507-373-4200 TRUCKER'S INN State Hwy 30 AT I 35 N... (6 Replies)
Discussion started by: seanmonaco
6 Replies

2. Shell Programming and Scripting

Gawk field separator problem

I'm trying to run a code using gawk and I'm having some trouble with it. What I'm trying to do is pull out value11 from the following input: value11,value12,value13 value21,value22,value23 I have successfully done this before using awk with the following code: awk 'NR == 1 {FS=",";... (4 Replies)
Discussion started by: KomjongShawn
4 Replies

3. Shell Programming and Scripting

strange problem

hello all, i am having problem in accessing a directory.I dont think its a permission issue.can anyone help me out. I am using korn sell code: $ ls -ltr sc* lrwxrwxrwx 1 essbase essbase 21 Oct 8 2010 sc_ssp -> /work/nfs/nas2/sc_ssp $ cd sc_ssp ksh: sc_ssp: not found $ (6 Replies)
Discussion started by: manid
6 Replies

4. Shell Programming and Scripting

Strange Problem

Hi All, I am encountering an unexpected problem while running my process, using daemon placed in /etc/init.d/. I have a process that is opening shared library using dlopen. When I run my process on the command line, it is able to open the library. But when I used daemon to run my process, I... (0 Replies)
Discussion started by: qwerty-1
0 Replies

5. Shell Programming and Scripting

Strange problem.

Well, my script started off to do what i wanted. Now, i think its not recognizing the pattern so its not moving anything. What i have to do is execute my script command for the move to take effect. So i did that and yayy it worked. Strange thing is that my DESTDIR was empty to begin with.... (2 Replies)
Discussion started by: oxoxo
2 Replies

6. Shell Programming and Scripting

Strange problem

I am using SunOS 5.9 and I don't know why all my commands are getting executed as if an extra 'enter' has been pressed. What could be the reason and how to correct it? Please help. Asty (2 Replies)
Discussion started by: Asty
2 Replies

7. Linux

very strange problem

I have installed Fedora Core on a Toshiba Satellite Pro4600 laptop recently I have experienced a rather mysterious problem if I touch anything specially the keyboard or mouse I see this stuff “67yujhnmyyy” straight away some time it won't stop for while like this... (5 Replies)
Discussion started by: kemobyte
5 Replies

8. UNIX for Dummies Questions & Answers

strange...problem

Hi.. Some of my application were not running properly due to lack of virtual memory.....so wht i did add one free harddisk as swap file system...and increased the swap memory.. But since than my root file system is showing 100% full thr is no space left...is thr any link between these two..... (1 Reply)
Discussion started by: Prafulla
1 Replies

9. UNIX for Dummies Questions & Answers

Strange Problem

I did an installation of Redhat 7.1 and it seem to install smoothly. I go and reboot and I get the following message: "INIT: Id "x" respawing too fast: disabled for 5 minutes" The screen flashes 4 times every 10 seconds or so. I can't seem to figure why this does this. It might be a simple... (2 Replies)
Discussion started by: forjohnny
2 Replies

10. UNIX for Dummies Questions & Answers

Strange problem.Please Help !

I¡¯m a network operator, mine is an IBM PC server 320, operating system SCO unix 3.2v4.2, triton 3.1 of Baan. Recently, my server went dead every a few hours, no sign & signal shows malfunction suddenly. It looks like a sudden power failure, but the indicator of main power supply is on. Normal... (1 Reply)
Discussion started by: lyhsm
1 Replies
Login or Register to Ask a Question