calculating the area of a circle with bc

Loïc Grenié loic.grenie at gmail.com
Sun Mar 28 19:49:05 UTC 2010


2010/3/28 Tony Arnold <tony.arnold at manchester.ac.uk>:
> Colin,
>
> Colin Law wrote:
>> On 28 March 2010 20:24, Tony Arnold <tony.arnold at manchester.ac.uk> wrote:
>>> Jozsef,
>>>
>>> Since the area of a pizza is directly proportional to its diameter, you
>>> only really need to calculate the price/diameter to find out the best
>>> value for money!
>>
>> It is proportional to the square of the diameter
>
> Oops! My bad. But the price/diameter is still good enough to determine
> the best value for money!
>
>>>> echo "$2/$(echo "scale=100; 2^($1/2) * $(echo "scale=100; 4*a(1)" | bc
>>>> -l)" | bc -l)" | bc -l
>>>>
>>>>
>>>> is there a shorter/better way?
>
> Why so many nested echo etc? Why not give bc the full expression? I
> think you also mean ($1/2)^2 instead of 2^($1/2). So you get
>
> echo "scale=100;$2/(($1/2)^2*4*a(1))" | bc -l

   Indeed, you can also simplify the division by 4 and the product by 4:

echo "scale=100;$2/($1^2*a(1))" | bc -l

  (scale=2 or 4 might be sufficient for your purpose). As said elsewhere,
  you can eliminate the "a(1)"

echo "scale=4;$2/$1^2" | bc -l

        Loïc




More information about the ubuntu-users mailing list