Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

It looks like bc doesn't support float operations, when I do echo 1/8 | bc it get me a zero.

I checked bc (1), but it doesn't even mention float, so I wonder if it's supported?

share|improve this question
4  
echo "scale=4; 1/8" | bc –  LiuYan 刘研 Mar 4 '13 at 2:20
    
This question and in particular the accepted answer might be interesting to you. –  Emanuel Berg Mar 6 '13 at 1:21
add comment

2 Answers

up vote 8 down vote accepted

bc doesn't do floating point but it does do fixed precision decimal numbers. The -l flag Hauke mentions loads a math library for eg. trig functions but it also means

[...] the default scale is 20

scale is one of a number of "special variables" mentioned in the man page. You can set it:

scale=4

Anytime you want (whether -l was used or not). It refers to the number of significant digits used in a decimal. In other words, subsequent solutions will be rounded to that number of digits after the decimal scale (== fixed precision).

The default scale sans -l is 0, meaning rounded to whole numbers.

share|improve this answer
add comment

man page says:

If bc is invoked with the -l option, a math library is preloaded [...]

The comprehensibility of that could be improved, indeed...

share|improve this answer
2  
Report it upstream as a bug... –  vonbrand Mar 4 '13 at 3:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.