comp.lang.ada
 help / color / mirror / Atom feed
* Re: Idea: Merging types in Ada 200X
       [not found]   ` <3595808B.6646F86A@cl.cam.ac.uk>
@ 1998-07-23  0:00     ` Frank Klemm
  0 siblings, 0 replies; only message in thread
From: Frank Klemm @ 1998-07-23  0:00 UTC (permalink / raw)


On Sun, 28 Jun 1998 00:30:19 +0100, Markus Kuhn <Markus.Kuhn@cl.cam.ac.uk> wrote:
>
>I agree that requesting from the compiler a silent conversion from float to
>integer is a very debatable idea from a software engineering point of few.
>I remember many C programming bugs I made just because I accidentially used
>integer division where I had ment float division, and the compiler never
>warned me but silently converted the integer division result into float.
>

One major pitfall in C ist not the automatic type conversation from integer
to float but the same syntax for a integer division and a floating point
division. So the following C-Code

  double pi = 355 / 113;
  
assigns the value of 3 to pi instead of the better approach 3.1415929... .
355 and 113 are both integers, so the `/' is a integer division with a result of 3.

To get the value of 3.1415929... you must write:

  double pi = 355.0 / 113;			or
  double pi = 355 / 113.0;			or
  double pi = (double)355 / 113;		or
  double pi = 355 / (double)113;		or
  double pi = 1.0 * 355 / 113;
  
to convert one integer to a float, so the `/' gets the fuction of a floating
point division. Really a ugly pitfall, especially in conjunction with the
preprocessor.

Automatic conversation from int to float is only be done in the following cases:

  * binary operation with one operand being always a float
  * call of a function with floating point arguments with integer values
  
Normally int => float is not a loss of precission, but floating points are
often slower. But the opposite direction is dangerous.

Example:
-------------------
#include <stdio.h>
#include <math.h>

/*	gamma function, but only for integer values 
	Note, that gamma is defined for all x elem R \ {0,-1,-2,...} 
	and gamma(n+1) = n!
*/						

double gamma(int x)	
  { double prod = 1.0;

    if (x <= 0)
      return MAXDOUBLE;
    while (--x > 1)
      prod *= x;			
    return prod;		
  }

int main(void)
  {
    printf("gamma(%f) = %f (the correct value is %f)\n",3.5,gamma(3.5),sqrt(M_PI)*0.5*1.5*2.5);
    return 0;
  }
---------------------
Compiling with highest level of warning and result:

pfk@schnecke:/home/pfk > gcc -O2 -o example example.c -Wall -ansi
pfk@schnecke:/home/pfk > ./example
gamma(3.500000) = 2.000000 (the correct value is 3.323351)
pfk@schnecke:/home/pfk > _
  

>But then I do not suggest that such conversions become a default feature of
>Ada, just one that a programmer who really wants it can activate it.
>Language designers should not be so arrogant to decide what is good for the
>programmer and what not but let the programmer decide what is appropriate.
>Ada's type system could with just a dozen of these silent conversion
>declarations be degraded to a C style type system. Whether this is
>appropriate for a certain project is something the people who set up the
>style guides for this project have to decide.
>
Note that too many manual type conversations are also a productive and never
drying up source of bugs and errors. To reduce errors it is senseful to sum up
type conversations belonging together.

-- 
Frank Klemm

 /------\  /-----------------------------------------------------\
| eMail: || pfk@uni-jena.de | home: pfk@schnecke.offl.uni-jena.de |
| Tel:   ||                 | home: +49 (3641) 390545             |
| sMail: ||  Frank Klemm, Ziegesarstr. 1, D-07747 Jena, Germany   |
 \------/  \-----------------------------------------------------/




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1998-07-23  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <3594CBA2.179A3645@cl.cam.ac.uk>
     [not found] ` <dewar.898963944@merv>
     [not found]   ` <3595808B.6646F86A@cl.cam.ac.uk>
1998-07-23  0:00     ` Idea: Merging types in Ada 200X Frank Klemm

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