[comp.sources.d] bc with logical functions

cjosta@taux01.UUCP (Jon Sweedler) (05/18/88)

I need a program with the infinite precision and programmability of bc
but tailored more towards logical and hex arithmetic.  I would like
AND, OR, XOR, SHIFT, etc. functions.  I am now simulating SHIFT and
NOT with bc but other functions are a bit too difficult.

Also, I am using hex arithmetic and I have run into problems when
performing calculations with fractions.  It seems that bc is geared
towards decimal arithmetic and has problems with hex.

The following example shows an example bc output.  The first operation
with an integer number shows what the correct result should be.  As I
add 0's to my input operand the precision of the result gets better.
This increasing precision occurs with decimal arithmetic but the result
given is the infinitely precise result truncated (at least for simple
examples like this one).  For hex arithmetic the result is not a
truncated version of the infinitely precise result.  Furthermore,
setting the "scale" variable to 20 should give us enough precision for
the given operation, but obviously it does not.

obase=16
ibase=16
scale=20
1234^2           = 14B5A90
1.234^2          = 1.4AF2
1.2340^2         = 1.4B4C59
1.23400^2        = 1.4B59C532
1.234000^2       = 1.4B5A84120
1.2340000^2      = 1.4B5A8F85D74
1.23400000^2     = 1.4B5A8FE791743
1.234000000^2    = 1.4B5A8FFB1D173D
1.2340000000^2   = 1.4B5A900000000000

I would call this a bug in bc, but I can understand that it was probably
not designed to do fractional hex arithmetic.  Anyway, if anyone has
any leads to a "hex-bc" I would appreciate hearing about them.  Thanks.

-- 
Jon Sweedler   =====   National Semiconductor (Israel)
UUCP:   {ames!amdahl,ihnp4!hplabs,pyramid,sun,decwrl}!nsc!taux01!cjosta
Domain: cjosta@taux01.nsc.com
Paper:  6 Maskit st., P.O.B. 3007, Herzlia B 46104, Israel