Trigonometric calculations with bc

  • by

bc, the basic calculator under Linux and UNIX environments, provides commands for doing arbitrary precision mathematical calculations. The syntax for basic arithmetic functions is intuitive. Here, we look at the commands for doing trigonometric calculations.

Scale

The global scale variable gives the number of digits after the decimal point. By default, the value of scale is 0.

$ 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'. 
2/3
0
print scale, "\n"
0
scale=2
2/3
.66
scale=8
2/3
.66666666

Math functions

The math functions can be executed once bc is loaded with the math library, using the -l option. With the math library, the scale is, by default, set to 20. The mathematical functions are,

bc - Math functions
Function Description
s (x) The sine of x, where x is in radians.
c (x) The cosine of x, where x is in radians.
a (x) The arctangent of x, The returned value is in radians.
l (x) The natural logarithm of x
e (x) The exponential function, raising e to the power x.
j (n, x) The Bessel function of integer order n of x.

The sine, cosine and arctangent functions work with angle arguments and return values in radians. However, angle values in radians are easily converted into degrees by noting that Π radians correspond to 180° and tan (45°) is 1. So to convert a value in radians into degrees, a factor of 180 / (4 * arctangent (1)) is to be multiplied to the value. For example,

$ 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'. 
a(768/1366)*180/(4*a(1))
29.34580364456776608792

The above calculation tells that the smaller angle of a right angled triangle with base 1366 units and height 768 units, which being the same as that of base, is 29.346°.

Subscribe
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You may like these, also

  • POSIX message queues in LinuxPOSIX message queues in Linux 1.0 POSIX Message queues POSIX interprocess comunication (IPC) was introduced in the POSIX.1b standard (IEEE Std 1003.1b-1993) for real time…
  • POSIX Shared Memory in LinuxPOSIX Shared Memory in Linux 1.0 Shared Memory Shared memory is the fastest method of interprocess communication (IPC) under Linux and other Unix-like systems. The…
  • POSIX Semaphores in LinuxPOSIX Semaphores in Linux 1.0 Semaphores Semaphores are used for process and thread synchronization. Semaphores are clubbed with message queues and shared memory under…
  • fork and exec system calls in Linuxfork and exec system calls in Linux 1.0 fork and exec system calls Suppose we wish to write a "shell program" which would execute another program. Now,…
  • Connecting two computers with Ethernet LAN cableConnecting two computers with Ethernet LAN cable Quite often, we wish to connect two computers back to back using an Ethernet LAN cable. It may be because…
  • D-Bus TutorialD-Bus Tutorial 1.0 D-Bus D-Bus is a mechanism for interprocess communication under Linux and other Unix-like systems. D-Bus has a layered architecture.…
  • Socket programming using the select system callSocket programming using the select system call 1.0 Client-Server Paradigm The Client-Server paradigm divides the software architecture of a system in two parts, the server and its…
  • System V message queues in LinuxSystem V message queues in Linux 1.0 Message queues Message queues are one of the interprocess communication mechanisms available under Linux. Message queues, shared memory and…
  • POSIX Threads Synchronization in CPOSIX Threads Synchronization in C 1.0 POSIX Threads Synchronization POSIX Threads provide multiple flows of execution within a process. The threads have their own stacks…
  • System V Shared Memory in LinuxSystem V Shared Memory in Linux 1.0 Shared Memory Shared memory is one of the three interprocess communication (IPC) mechanisms available under Linux and other Unix-like…