comp.lang.ada
 help / color / mirror / Atom feed
From: mcsun!hp4nl!cwi.nl!jankok@uunet.uu.net  (Jan Kok)
Subject: Re: Numeric problem with Ada
Date: 14 Nov 91 09:53:45 GMT	[thread overview]
Message-ID: <4684@charon.cwi.nl> (raw)

In article <9111131948.AA20227@cnam.cnam.fr> bortz@CNAM.CNAM.FR writes:
SB>  ...
SB>procedure TEST is
SB>	Mz : integer := 7;
SB>	Z2 : float;
SB>	package FLOAT_TEXT_IO is new TEXT_IO.FLOAT_IO (float);
SB>begin
SB>	Z2 := 2.6 * float (Mz) - 0.2;
SB>	FLOAT_TEXT_IO.PUT (Z2, Aft => 6, Exp => 0);
SB>end TEST;        
SB>
SB>The program displays 17.999998 (the mathematical result is 18).  ...
SB>
SB>main ()
SB>{
SB>	int mz = 7;
SB>	float z2;
SB>	z2 = 2.6 * mz - 0.2;
SB>	printf ("%2.6f\n", z2);
SB>}
SB>
SB>displays 18.000000.
SB>
SB>Stephane Bortzmeyer           Conservatoire National des Arts et Metiers
	

   Dear Stephane,
The answer with the Ada program is correct. You might accept
this, but I suppose you want to understand the phenomenon.
You did not say that the internal representations of the results
with the two programs are different (I suppose they are not).
Could you try something like:
   	Z2 := float (18);
   	FLOAT_TEXT_IO.PUT (Z2, Aft => 6, Exp => 0);
I am suggesting this, because the explanation might be in
the PUT procedure. Conversion from internal (binary)
floating-point value to a decimal character string of finite
length requires a sophisticated algorithm, and we certainly
know about Ada I/O implementations that do not recognise all
exact decimal values. Then, while computing successive decimal
digits for the output string, some accuracy can get lost.

I can also offer you an explanation in the case that the internal
values of the Ada and C results are different, but then I would
assume that the Ada PUT procedure would show a larger difference from 18.
--Jan Kok.
-- 
Mail: Jan Kok, CWI (afd. NW), Postbus 4079, NL-1009 AB Amsterdam, Nederland
Net : jankok@cwi.nl
---------------------------------------------------------------
  Why is a computer?

             reply	other threads:[~1991-11-14  9:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1991-11-14  9:53 Jan Kok [this message]
  -- strict thread matches above, loose matches on Subject: below --
1991-11-27 16:19 Numeric problem with Ada CNAM.CNAM.FR!bortz
1991-11-21  8:57 Thomas Buenermann
1991-11-19 18:30 psinntp!vitro.com!v7.vitro.com!vaxs09
1991-11-19 14:05 Jean-Pierre Rosen
1991-11-19  0:23 Jim Giles
1991-11-13 19:48 CNAM.CNAM.FR!bortz
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox