LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cube root in bc (https://www.linuxquestions.org/questions/programming-9/cube-root-in-bc-814009/)

RudraB 06-14-2010 04:09 AM

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 ?

H_TeXMeX_H 06-14-2010 05:45 AM

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


ntubski 06-14-2010 08:38 AM

How do I do a cube root in GNU bc?

H_TeXMeX_H 06-14-2010 09:10 AM

Quote:

Originally Posted by ntubski (Post 4003087)

I see, well it's certainly messy.

michaelk 06-14-2010 10:23 AM

Its not real messy... Basically..
x^y=e^(y * ln(x))
or
echo "e(y*l(x))" | bc -l

RudraB 06-15-2010 06:22 AM

ah, thank you guys!
I tried michaelk's method but ran into some error and conclude, its not possible.
thank you all.

grail 06-15-2010 07:00 AM

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.

RudraB 06-16-2010 12:09 AM

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

Karl Godt 04-30-2014 12:09 PM

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 !


All times are GMT -5. The time now is 03:30 AM.