UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 28th, 2005, 02:24 PM
smokingguns smokingguns is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 16 smokingguns User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 35 m 4 sec
Reputation Power: 0
Question absolute value fuction in unix

hi guys,
is there a function that helps in calculating the absolute value of a number????

Reply With Quote
  #2  
Old July 13th, 2005, 04:35 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,098 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 9 h 5 m 46 sec
Reputation Power: 9
Quote:
Originally Posted by smokingguns
hi guys,
is there a function that helps in calculating the absolute value of a number????


sure
i think, you cannot use abs()
so on cmd-line, try
echo "your-number/1" |bc
__________________
working on Solaris[5-9], preferred languages french and C.

Reply With Quote
  #3  
Old July 15th, 2005, 06:37 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 117 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 42 m 44 sec
Reputation Power: 6
Smile

Quote:
Originally Posted by guggach
sure
i think, you cannot use abs()
so on cmd-line, try
echo "your-number/1" |bc


I have tried 2/1 | bc

and got, of course

-2/1 | bc
ksh: -2/1: not found

which is not exactly what was required. If something is not ready made in the system, write it yourself, for instance:

:
# Function to evaluate the absolute value of a number
abs () {
# Check for numeric input
if expr $1 + 0 2>/dev/null 1>&2 ; then
# Is the number negative?
if [ $1 -lt 0 ] ; then
echo `expr 0 - $1`
else
echo $1
fi
return 0 # OK
else
return 1 # Not a number
fi
}

A=-10; abs $A || echo $A not a number
A=121; abs $A || echo $A not a number
A=1x1; abs $A || echo $A not a number

This script was tested under Linux with bash, but it must work under ksh and sh, too.

Have a fun ZL

Reply With Quote
  #4  
Old July 15th, 2005, 12:54 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,308 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 5 h 20 m 49 sec
Reputation Power: 48
try this as well:
Code:
 echo "-.233" | awk ' { if($1>=0) { print $1} else {print $1*-1 }}'

Reply With Quote
  #5  
Old July 15th, 2005, 03:42 PM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,098 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 9 h 5 m 46 sec
Reputation Power: 9
Quote:
Originally Posted by zlutovsky
I have tried 2/1 | bc

and got, of course

-2/1 | bc
ksh: -2/1: not found

which is not exactly what was required. If something is not ready made in the system, write it yourself, for instance:

:
# Function to evaluate the absolute value of a number
abs () {
# Check for numeric input
if expr $1 + 0 2>/dev/null 1>&2 ; then
# Is the number negative?
if [ $1 -lt 0 ] ; then
echo `expr 0 - $1`
else
echo $1
fi
return 0 # OK
else
return 1 # Not a number
fi
}

A=-10; abs $A || echo $A not a number
A=121; abs $A || echo $A not a number
A=1x1; abs $A || echo $A not a number

This script was tested under Linux with bash, but it must work under ksh and sh, too.

Have a fun ZL

i said
echo "333.7777/1" | bc
if you have a normal shell under a normal unix-os, you get
333

NOTA: BC and his brother DC, in unix, are known since 1978

Last edited by guggach : July 15th, 2005 at 03:44 PM.

Reply With Quote
  #6  
Old July 18th, 2005, 06:37 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 117 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 42 m 44 sec
Reputation Power: 6
Quote:
Originally Posted by guggach
i said
echo "333.7777/1" | bc
if you have a normal shell under a normal unix-os, you get
333

NOTA: BC and his brother DC, in unix, are known since 1978




But guggach, have you ever heard what the absolute value is?
You are presentig something what was not required, namely the integer part of a number. First read the question and then shoot.

Regards

Reply With Quote
  #7  
Old July 18th, 2005, 06:41 AM
zlutovsky zlutovsky is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Prague, Czech Rep.
Posts: 117 zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level)zlutovsky User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 h 42 m 44 sec
Reputation Power: 6
Quote:
Originally Posted by jim mcnamara
try this as well:
Code:
 echo "-.233" | awk ' { if($1>=0) { print $1} else {print $1*-1 }}'



Yes, awk is universal, but the question was to write a function. It is no problem for an experienced shell programmer to write some envelope to your statement to provide the required function.

Regards

Reply With Quote
  #8  
Old July 20th, 2005, 01:34 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,098 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 5 Days 9 h 5 m 46 sec
Reputation Power: 9
Quote:
Originally Posted by zlutovsky
But guggach, have you ever heard what the absolute value is?
You are presentig something what was not required, namely the integer part of a number. First read the question and then shoot.

Regards


ok, i did not check negative values

my way remain BC

val=-123.098

echo "a=$val/1;if(0>a)a*=-1;a"|bc

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > absolute value fuction in unix


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT