LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-25-2016, 05:18 PM   #1
eflint
LQ Newbie
 
Registered: May 2016
Posts: 4

Rep: Reputation: Disabled
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
 
Old 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: Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147
Just multily by -1.

In bc...

Code:
-5 * -1

Last edited by astrogeek; 05-25-2016 at 05:41 PM.
 
Old 05-25-2016, 05:48 PM   #3
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,596
Blog Entries: 1

Rep: Reputation: 510Reputation: 510Reputation: 510Reputation: 510Reputation: 510Reputation: 510
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.
 
Old 05-25-2016, 05:56 PM   #4
eflint
LQ Newbie
 
Registered: May 2016
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
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 View Post
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.
 
Old 05-25-2016, 11:33 PM   #5
dunne
Member
 
Registered: May 2014
Distribution: OpenBSD
Posts: 67

Rep: Reputation: 36
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.
 
Old 05-26-2016, 02:03 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
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.
 
Old 05-26-2016, 02:48 AM   #7
dunne
Member
 
Registered: May 2014
Distribution: OpenBSD
Posts: 67

Rep: Reputation: 36
Quote:
Originally Posted by grail View Post
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?
 
Old 05-26-2016, 03:05 AM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,184

Rep: Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584
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.
 
Old 05-26-2016, 03:29 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,437

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Quote:
Originally Posted by dunne View Post
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
 
Old 05-26-2016, 03:36 AM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,184

Rep: Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584
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?
 
Old 05-26-2016, 04:40 AM   #11
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,151

Rep: Reputation: 192Reputation: 192
Just use cut.

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

Output: 5
 
1 members found this post helpful.
Old 05-26-2016, 05:17 AM   #12
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,596
Blog Entries: 1

Rep: Reputation: 510Reputation: 510Reputation: 510Reputation: 510Reputation: 510Reputation: 510
Quote:
Originally Posted by szboardstretcher View Post
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.
Old 05-26-2016, 07:07 AM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,177

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
The OP specifically mentioned integer.
 
Old 05-26-2016, 09:26 PM   #14
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,184

Rep: Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584
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!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to convert negative integer to byte array? Basel Programming 5 10-27-2010 12:35 PM
renice (positive or negative) tincboy Linux - Newbie 3 10-20-2010 01:09 AM
Please list all the positive and negative things about GNOME and KDE ayush.27 Linux - General 9 09-09-2010 12:37 PM
Converting Photo Negative to Positive measekite Linux - Software 2 03-23-2009 10:57 AM
printing negative value as positive hubabuba Programming 5 05-03-2005 12:41 PM


All times are GMT -5. The time now is 08:24 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