[comp.sys.transputer] Programming in C

steveh@inmos.co.uk (Steven Huggins) (04/26/91)

The program in an article by <choudhry%mojo.uucp@mimsy.umd.edu
(Zafar Ullah Choudhry)>, has been causing quite some activity.


The situation is as follows:

a) if the program is compiled under K & R C ( 1st edition ) you will obtain
the answers that were supposed to be expected, viz. free_l[1] = -0.000002,
free_l[2] = 0.000007.  What happens is that the expression 
freea[8] * (we * yin[nsite] + xdin[nsite]); is calculated in double
precision before being cast ( behind your back ) to float.

b) if the program is compiled under ANSI C, you will obtain the answers
0.0, 0.0.

c) if the variables are changed from float to double, and the program is
then compiled under ANSI C, you will obtain the answers free_l[1] = 0.000008,
free_l[2] = -0.000033.


Using the UNIX utility "bc", which is a very-large-precision calculator,
you can find the truely CORRECT results, viz. free_l[1] = 0.0000084732152942240,
free_l[2] = -0.000032726107192564.  Even a hand-held calculator gives rounded
versions of these values.


Doing the calculation in ANSI C is THE way to do it, and in double
precision if you are in any way interested in accuracy. Note that the
values given as the ones that were expected in the original posting are
completely WRONG, even the sign of them is wrong.

Lesson: avoid K & R C; use ANSI C.


NOTE: that all of this has absolutely nothing to do with transputers,
but is simply a C language issue.



P.S.  Contact your local SGS-THOMSON/INMOS dealer if you want to buy
the INMOS ANSI C toolset for transputers.  It has been validated as
conformant with ANSI C by the British Standards Institute.

--------------------------------------------------------------------------
Steven R Huggins, Software Development Group, INMOS Ltd., 1000 Aztec West,
Almondsbury, Bristol, United Kingdom. BS12  4SQ           Tel: 0454 616616
INTERNET: steveh@inmos.com  JANET and UK source: steveh@uk.co.inmos
Other possibility: steveh@inmos.co.uk