LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-08-2010, 03:03 AM   #1
ram87pune
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
bc -l help


Hi guys,

I am taking the difference of two numbers,both are negative.Now the difference is coming as negative because one number is greater than the other e.g -2.01-(-1.68) so the answer i am getting is -.33

How can I get this answer as a positive value say some modulus command and also I want the answer to be 0.33 not .33

Please help
 
Old 12-08-2010, 04:12 AM   #2
GlennsPref
Senior Member
 
Registered: Apr 2004
Location: Brisbane, Australia
Posts: 3,366
Blog Entries: 33

Rep: Reputation: 216Reputation: 216Reputation: 216
Hi, bc is great for transforming numbers from one type to another.

Like, having a number(int) transposed from hex to octal, or binary or digital/metric.

But to sum two ints "let" maybe of more use.

Let is a bash plugin and adds or subtracts, or divides, or modulus, or multiplic... you get my meaning?


So checkout man let, it will bring up the bash man file, scroll down till you get to "let".
Quote:
let arg [arg ...]
Each arg is an arithmetic expression to be evaluated (see ARITHMETIC EVALUATION above).
If the last arg evaluates to 0, let returns 1; 0 is returned otherwise.
Here is an example....using variables

Code:
#!/bin/bash
#sum-calls inspired by wlayton27 Atomicmpc.com.au forums.

#sum="0"
    for num in `cat /home/glenn/build/ibm-unix/calls03.csv`; do
	#let "sum=$sum+$num"
	let "sum += $num"
	  done
	    echo "Result in kb's: "$sum;
	# # # bash division
	let "mbs=$sum/1024"
	#
	    echo "Result in Mb's: " $mbs;
	#
	let "gbs=$mbs/1024"
	# 
	    echo "Result in Gb's: " $gbs;
	exit
I've just found about this function, please forgive me if I'm off topic.

Regards Glenn
 
Old 12-08-2010, 04:13 AM   #3
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,845
Blog Entries: 36

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Absolute value or a comparison. For instance:
Code:
#absolute value
x=abs(-2.01-(-1.68))

#comparison
x=-2.01-(-1.68)
if(x<0){
x=x*(-1)
}
The method/syntax varies depending on what language you're programming in. You can easily do the comparison in bc from reading the man page. I've never programmed in bc but the concept should still hold.

For the other part of your question I found this thread.

Last edited by sag47; 12-08-2010 at 04:26 AM.
 
1 members found this post helpful.
Old 12-08-2010, 06:03 AM   #4
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,397
Blog Entries: 1

Rep: Reputation: 64
You can define new functions in bc. take a look at bc's man page.
Code:
miguel@quadbit:~$ bc -l
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
define abs(x) {if (x<0) {return -x}; return x;}
abs(-2.01 - ( -1.68 ))
.33
quit
miguel@quadbit:~$
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off




All times are GMT -5. The time now is 09:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration