From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d5423113b0057756 X-Google-Attributes: gid103376,public From: pfk@schnecke.offl.uni-jena.de (Frank Klemm) Subject: Re: Idea: Merging types in Ada 200X Date: 1998/07/23 Message-ID: #1/1 X-Deja-AN: 374465937 X-Nntp-Posting-Host: schnecke.offl.uni-jena.de Sender: news@fsuj50.rz.uni-jena.de (News System) References: <3594CBA2.179A3645@cl.cam.ac.uk> <3595808B.6646F86A@cl.cam.ac.uk> Organization: Pbzchgre fvaq hasruyone, Qnir Newsgroups: comp.lang.ada Date: 1998-07-23T00:00:00+00:00 List-Id: On Sun, 28 Jun 1998 00:30:19 +0100, Markus Kuhn 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 #include /* 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 | \------/ \-----------------------------------------------------/