The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Inline::BC - Inline ILSM for bc the arbitrary precision math Language

SYNOPSIS

  use Inline BC;
  print x(int(rand(time())));
  __DATA__
  __BC__
  define x(a){
    scale = 20;
    return (a*3.456789);
  }
 

DESCRIPTION

Inline::BC is an ILSM (Inline Support Language Module ) for Gnu bc, the arbitrary precision numeric processing language. Inline::BC - like other ILSMs - allows you to compile (well - render to byte code ), and run Gnu bc code within your Perl program.

From the Gnu BC README:

bc is an arbitrary precision numeric processing language. Syntax is similar to C, but differs in many substantial areas. It supports interactive execution of statements. bc is a utility included in the POSIX P1003.2/D11 draft standard.

This version was written to be a POSIX compliant bc processor with several extensions to the draft standard. Option flags are available to cause warning or rejection of the extensions to the POSIX standard. For those who want only POSIX bc with no extensions, a grammar is provided for exactly the language described in the POSIX document. The grammar (sbc.y) comes from the POSIX document. The Makefile contains rules to make sbc. (for Standard BC)

"end of quote"

Further documentation about Gnu bc can be found at: http://www.gnu.org/software/bc/bc.html http://www.gnu.org/manual/bc/html_mono/bc.html

one thing to note is that you should be careful with setting the global bc parameters like ibase, obase, scale etc. You should not set these in the global code - instead, set them in each function, to avoid the chaos that would follow.

Looking at the test suite - there are examples of several different ways of invoking Inline::BC:

(1) code in the DATA statement use Inline BC; print x(4) == 5.3 ? "ok 2\n" : "not ok 2\n"; __DATA__ __BC__ define x (a) { scale = 20 return (a * 1.5); }

(2) inline code with here document use Inline BC => <<'END_BC'; define z (a, b) { scale = 6 t = a * .357; t = b / t; return ( t ); } END_BC print z(4, 7) > 4 ? "ok 3\n" : "not ok 3\n";

(3) code in an external file use Inline BC => './tools/test.dat'; print aa() =~ /[0\n]/s ? "ok 4\n" : "not ok 4\n";

CONFIG OPTIONS

Inline::BC provides the following config options.

MATH_LIB => 0|1

When Inline::BC is invoked with the config option 'MATH_LIB => 1', then the GNU bc processor is initialised with its builtin math library. The math library offers the following builtin functions:

s(x)

The sine of x, x is in radians.

c(x)

The cosine of x, x is in radians.

a(x)

The arctangent of x, arctangent returns radians.

l(x)

The natural logarithm of x.

e(x)

The exponential function, raising e to the value x.

j(n,x)

The Bessel function of integer order n of x.

Example: Calculating the hyperbolic sine of a value.

use Inline BC => "DATA", MATH_LIB => 1;

my $r = bc_sinh(4.712);

__END__

__BC__ define bc_sinh (u) { scale = 12 t = (e(u) - e(-u)) * 0.5 return ( t ) }

An interesting point to note:

To calculate the square root of a number, GNU bc provides the expression sqrt(x). Because GNU bc treats it as an expression rather than a function, there is no need to load the math library in order to use sqrt(x).

FILTERS => [ "filtnam1", ... ]

The 'FILTERS' option allows the BC code to be pre-processed before Inline passes it to the BC interpreter. To use existing pre-processing filters with Inline::BC the module Inline::Filters must be installed. This module provides the filters Strip_POD, Strip_Comments, and Preprocess.

The filter Strip_POD is used to remove POD documentation contained in the BC code. Strip_Comments will remove all comments found in the BC code. The final filter, Preprocess, allows the programmer to embed C preprocessor directives in the BC code. The filter processes these directives by invoking the C preprocessor to generate code that can then be passed to the BC interpreter.

In the following example, POD documentation is embedded in the BC code. This must be removed before the BC code is passed to the BC interpreter.

  use Inline BC => "DATA",
             MATH_LIB => 1,
             FILTERS  => [ qw(Strip_POD) ];

  my $r = bc_sinh(4.712);

  __END__

  __BC__
  =head1 BC Functions

  The following functions are defined in GNU bc language.

  =cut

  /*************************************/
  =pod

  Function Name: bc_sinh(x)
  Description  : Calculates the hyperbolic sine of real value x.
  Return       : The calculated result with 12 fractional decimal
               : digits of precision.

  =cut

  /*************************************/
  define bc_sinh (u) {
    scale = 12
    t = (e(u) - e(-u)) * 0.5
    return ( t )
  }

VERSION

Inline::BC 0.08

AUTHOR

Piers Harding - piers@cpan.org

SEE ALSO

 man bc
 perldoc Inline
 perldoc Inline::Filters

COPYRIGHT

Copyright (c) 2002, Piers Harding. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.