Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-08-2010, 03:03 AM   #1
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
Senior Member
Registered: Apr 2004
Location: Brisbane, Australia
Distribution: pclinuxos slackware64 tails kali
Posts: 3,382
Blog Entries: 33

Rep: Reputation: 217Reputation: 217Reputation: 217
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".
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

#sum-calls inspired by wlayton27 forums.

    for num in `cat /home/glenn/build/ibm-unix/calls03.csv`; do
	#let "sum=$sum+$num"
	let "sum += $num"
	    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;
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
Senior Member
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,857
Blog Entries: 36

Rep: Reputation: 457Reputation: 457Reputation: 457Reputation: 457Reputation: 457
Absolute value or a comparison. For instance:
#absolute value

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
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,414
Blog Entries: 1

Rep: Reputation: 65
You can define new functions in bc. take a look at bc's man page.
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 ))


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:39 AM.

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