## strange problem with gawk

strange problem with gawk
# 1
11-09-2007
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.

Last edited by user_prady; 11-09-2007 at 02:33 AM..
# 2
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.
 Ygor View Public Profile for Ygor Find all posts by Ygor
# 3
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.

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..

Last edited by user_prady; 11-09-2007 at 04:34 AM..
# 4
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.
 fpmurphy View Public Profile for fpmurphy Find all posts by fpmurphy
# 5
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.
 aigles View Public Profile for aigles Find all posts by aigles
# 6
11-11-2007
Quote:
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..

Passing a number a number works as expected: Passing a string does not work: Adding a zero to the parameter will convert it from a string to a number:
 Ygor View Public Profile for Ygor Find all posts by Ygor
# 7
11-12-2007

Quote:
Originally Posted by Ygor
Passing a number a number works as expected: Passing a string does not work: Adding a zero to the parameter will convert it from a string to a number:
Yes that's the answer . I am now getting the exact answer..
Thank You all again...

## 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...

## 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=",";...

## 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 \$

## 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...

## 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....

## 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

## 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...

## 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.....

## 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...