LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2010, 04:09 AM   #1
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Rep: Reputation: 23
cube root in bc


As in bc manual, I got
Quote:
expr ^ expr The result of the expression is the value of the first raised to the second. The second expression must be an integer. (If the second expression is not an integer, a warning is generated and the expression is truncated to get an integer value.)
So is there anyway to calculate cube root in bc? or may be expr ?
 
Old 06-14-2010, 05:45 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Strangely enough, I don't think it's possible, because it has to have an integer exponent, and even more strangely, it works fine in awk:

Code:
bash-3.1$ echo 64 | awk '{ print $1^(1/3) }'
4
 
Old 06-14-2010, 08:38 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
How do I do a cube root in GNU bc?
 
Old 06-14-2010, 09:10 AM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by ntubski View Post
I see, well it's certainly messy.
 
Old 06-14-2010, 10:23 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,681

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
Its not real messy... Basically..
x^y=e^(y * ln(x))
or
echo "e(y*l(x))" | bc -l
 
Old 06-15-2010, 06:22 AM   #6
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Original Poster
Rep: Reputation: 23
ah, thank you guys!
I tried michaelk's method but ran into some error and conclude, its not possible.
thank you all.
 
Old 06-15-2010, 07:00 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
I tried michaelk's method but ran into some error and conclude, its not possible.
Well personally I would say it is a big call to ask for help and then dismiss the answer!!

Maybe if you explained your error someone might help?

I have run the code and whilst there is a small rounding issue on my machine it does return an answer, all be it a very close approximation.
 
Old 06-16-2010, 12:09 AM   #8
RudraB
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 264

Original Poster
Rep: Reputation: 23
Quote:
Well personally I would say it is a big call to ask for help and then dismiss the answer!!
Probably you misinterprit me....as(i guess)english is not my native language.
What i meant is i tried michaelk's method(and conclude the impossiblity) before posting in this forum.
After getting his reply I retried and succeed.
I am sorry if I hurt some one (michaelk or other who helped).
Thank you
 
Old 04-30-2014, 12:09 PM   #9
Karl Godt
Member
 
Registered: Mar 2010
Location: Kiel , Germany
Distribution: once:SuSE6.2,Debian3.1, aurox9.2+3,Mandrake?,DSL? then:W7st,WVHB, #!8.10.02,PUPPY4.3.1 now:Macpup
Posts: 314

Rep: Reputation: 45
After googling for bc third root the first suggestion was this thread .

I stumbled about two and a half problems :

1) Since I never had to use the -l --mathlib option to bc, I did not use it .
2) I wrongly read the letter ' small "L" ' at http://phodd.net/gnu-bc/bcfaq.html#bccbrt
as it states
Code:
echo 'e(l({number})/3)' | bc -l
as a ' number ONE "1" '
3) I also used {number} or {27} instead of just 27 .

Code:
bash-3.2# bc
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'. 
e(l(27)/3)
Runtime error (func=(main), adr=6): Function l not defined.
quit
bash-3.2# 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'. 
e(l(27)/3)
2.99999999999999999998
quit
But thanks for this thread !
 
  


Reply



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
desktop cube only works with root account on dell latitude d810 opensuse 11.2 magnuje Linux - Laptop and Netbook 7 01-22-2010 03:52 AM
Ubuntu 9.10 cube is not turning to a cube??? XxRepti1exX Linux - Newbie 6 11-07-2009 06:34 PM
Desktop Cube isn't a cube gannggstaz Linux - Newbie 6 10-22-2009 03:46 PM
the cube Clarence27 Ubuntu 1 07-03-2007 09:53 PM
Cube crashes on startup (Unless run as root) jimdaworm Linux - Games 2 05-11-2004 11:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:28 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
Open Source Consulting | Domain Registration