Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org How can I convert a negative integer to a positive number using bc
 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

 05-25-2016, 05:18 PM #1 eflint LQ Newbie   Registered: May 2016 Posts: 4 Rep: How can I convert a negative integer to a positive number using bc I'm never use bc that much, but If I have an integer of -5 can I use bc to make it a positive number? much appreciated
 05-25-2016, 05:39 PM #2 astrogeek Moderator   Registered: Oct 2008 Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2} Posts: 4,079 Blog Entries: 1 Rep: Just multily by -1. In bc... Code: `-5 * -1` Last edited by astrogeek; 05-25-2016 at 05:41 PM.
 05-25-2016, 05:48 PM #3 Shadow_7 Senior Member   Registered: Feb 2003 Distribution: debian Posts: 2,596 Blog Entries: 1 Rep: Why not use sed to remove the "-"? A slow way would be to square then square root it. Or multiply by -1 and take the higher value of the original versus post bc comparison. And probably many other tricks. Although bc might have an abs() function for absolute value. Or bash if you're not doing floating point numbers. I'm not that up on either to know though.
05-25-2016, 05:56 PM   #4
eflint
LQ Newbie

Registered: May 2016
Posts: 4

Original Poster
Rep:
Quote:
 Originally Posted by astrogeek Just multily by -1. In bc... Code: `-5 * -1`
I found this on the web.

echo -"(-5)" | bc -l and it output the number 5

Quote:
 Originally Posted by Shadow_7 Why not use sed to remove the "-"?
I need to use the code in a math situation in a bash script.

Much appreciate to both

Last edited by eflint; 05-25-2016 at 06:16 PM.

 05-25-2016, 11:33 PM #5 dunne Member   Registered: May 2014 Distribution: OpenBSD Posts: 67 Rep: More generally: Code: ```define test (n) { if (n < 0) n *= -1 return n }``` So in a shell script you could do: Code: `(echo 'define test (n) {' ; echo ' if (n < 0) n *= -1 ; return n}' ; echo "test(\$myvar)") | bc` Need two echoes for function defintion because newline expected after "{". EDIT: Thinking about it, bc can read a file of function defintions, so you could put this and other functions into a file, and invoke: Code: `bc myfunctionsfile` Last edited by dunne; 05-26-2016 at 02:51 AM.
 05-26-2016, 02:03 AM #6 grail LQ Guru   Registered: Sep 2009 Location: Perth Distribution: Manjaro Posts: 9,437 Rep: All looks a bit complicated to me. If it is bash and we are simply using integers, something like: Code: ```x=-5 (( x = x < 0 ? x * -1 : x )) echo \$x``` You can put it in a function or use where necessary.
05-26-2016, 02:48 AM   #7
dunne
Member

Registered: May 2014
Distribution: OpenBSD
Posts: 67

Rep:
Quote:
 Originally Posted by grail All looks a bit complicated to me
The question was how to do it in bc.
And why on Earth would anyone use a behemoth like Bash to do simple maths?

 05-26-2016, 03:05 AM #8 szboardstretcher Senior Member   Registered: Aug 2006 Location: Detroit, MI Distribution: GNU/Linux systemd Posts: 4,184 Rep: A quick absolute value script would be: Code: ```[sysop@ArchTerminal Downloads]\$ 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) 2``` And of course in bash you can quickly do something like: Code: ```[sysop@ArchTerminal Downloads]\$ asdf=-1 [sysop@ArchTerminal Downloads]\$ echo \$asdf -1 [sysop@ArchTerminal Downloads]\$ asdf=\${asdf/#-/} [sysop@ArchTerminal Downloads]\$ echo \$asdf 1``` Last edited by szboardstretcher; 05-26-2016 at 03:14 AM.
05-26-2016, 03:29 AM   #9
grail
LQ Guru

Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep:
Quote:
 Originally Posted by dunne The question was how to do it in bc. And why on Earth would anyone use a behemoth like Bash to do simple maths?
Gee, maybe it had something to do with the OP saying:
Quote:
 I need to use the code in a math situation in a bash script.
And as it is a newbie forum he/she may have thought that bc was their only option

 05-26-2016, 03:36 AM #10 szboardstretcher Senior Member   Registered: Aug 2006 Location: Detroit, MI Distribution: GNU/Linux systemd Posts: 4,184 Rep: I would say in this case,. bc is a behemoth and complete overkill,. You are already IN the bash shell, and bash is completely capable of 'simple maths' .. so why break out a calcuating program that requires an entirely new language to use?
05-26-2016, 04:40 AM   #11
JJJCR
Senior Member

Registered: Apr 2010
Posts: 1,151

Rep:
Just use cut.

Quote:
 echo "-5" | cut -c 2 Output: 5

1 members found this post helpful.
05-26-2016, 05:17 AM   #12
Senior Member

Registered: Feb 2003
Distribution: debian
Posts: 2,596
Blog Entries: 1

Rep:
Quote:
 Originally Posted by szboardstretcher I would say in this case,. bc is a behemoth and complete overkill,. You are already IN the bash shell, and bash is completely capable of 'simple maths' .. so why break out a calcuating program that requires an entirely new language to use?
Because bash doesn't do floating point math. Unless you shift the decimal and add it back into your number (visually) with string manipulation. Although it is disheartening that a kernel build now requires bc.

\$ echo \$(( -5 * -1 ))
5

\$ echo \$(( 5 / 10 ))
0

\$ echo "."\$(( 50 / 10 ))
.5

1 members found this post helpful.
 05-26-2016, 07:07 AM #13 syg00 LQ Veteran   Registered: Aug 2003 Location: Australia Distribution: Lots ... Posts: 15,177 Rep: The OP specifically mentioned integer.
 05-26-2016, 09:26 PM #14 szboardstretcher Senior Member   Registered: Aug 2006 Location: Detroit, MI Distribution: GNU/Linux systemd Posts: 4,184 Rep: Looks like OP has gotten a lot of good answers. It's really up to them to decide on the way that works for them. @eflint, Let us know if you have further need or explanation!

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Basel Programming 5 10-27-2010 12:35 PM tincboy Linux - Newbie 3 10-20-2010 01:09 AM ayush.27 Linux - General 9 09-09-2010 12:37 PM measekite Linux - Software 2 03-23-2009 10:57 AM hubabuba Programming 5 05-03-2005 12:41 PM

All times are GMT -5. The time now is 08:24 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -