|
![]() |
|
* FAQ * Tools + Games + GNU bc + Interesting + Stuff + Links |
GNU bc FAQLooking through my site logs, I realise that many people who find the GNU bc page are looking for something that isn't actually here. This page is for precisely those things that people apparently want answers to. Without further ado, here are some of the questions asked of search engines that people haven't found the answer to on the GNU bc page:
How do I do a square root in bash?I reckon this is a trick question - do you guys want to use bc or not? :) Okay... Lets assume both and work from there: bash square roots - the hard way - no bc!You really honestly and truthfully don't want to do this, surely? The bash shell is only capable of integer arithmetic, so even if you could take a square root, you'll only get the integer portion of the answer... i.e. sqrt(10) = 3 to the nearest whole number. If that's all you need, then this shell function is for you:
That's all on one line.
Once you've entered the above into a script or your interactive session, the command
Be warned though, the above contains no checking for negative numbers, and the function will crash with an error when given one. This is probably for the best. bash square roots - the easy way - using bc
The simplest way to do this is to pipe some bc syntax to
...making the obvious replacement of {number}. Yes. It's that simple!
How do I do a square root in GNU bc?If you've been paying attention class, the answer to this has already been used in the previous answer!
bc has its own built in How do I do floating point numbers in GNU bc?
It concerns me greatly the number of people who don't know about the For instance; The example from the question before last might have been written:
Note the lack of the -l flag this time. Also; Changing that 20 will change
the number of decimal places in the answer printed.
How do I do a logarithm in bash?Again... not sure whether the requestor wants bash or bc code, so it's twice the work for me :): bash logarithms - the hard way - no bc!Bearing in mind that bash is integer-only, this shell function will return the rounded down logarithm in bash:
As before, it's all on one line.
Once you've entered the above into a script or your interactive session, the command
The 'clever' bit is that the command Beware again - this works only when fed positive integers. Anything else is not checked for and may cause this function to behave bizarrely. bash logarithms - the easy way - using bc
Guess what? You can pipe some bc syntax to or
...making the obvious replacements.
How do I do a cube root in GNU bc?
The easiest way to do a cube root in bc is to use the Of course, you may just want to use the following quick-and-dirty trick that both of those functions are based upon. Again, this example is for the bash shell:
This works on the mathematical principle that:
n√x = x(1/n) = e(ln x)/nIn bc you might like to use this to write your own cube root function:
And then use it as you would the built in sqrt().
As you might guess then, the
| |
![]() | ||
|
This site should work with most browsers though there are some advanced features that will only work with more recent software | that mail thing | web{}phodd.net | |