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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,6a8952cbe009f3ed X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit X-Received: by 10.68.202.230 with SMTP id kl6mr366076pbc.5.1360469025232; Sat, 09 Feb 2013 20:03:45 -0800 (PST) Path: mj10ni888pbb.1!nntp.google.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!nrc-news.nrc.ca!goblin2!goblin1!goblin.stu.neva.ru!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: Numerical calculations: Why not use fixed point types for everything? Date: Mon, 04 Feb 2013 16:24:28 +0200 Organization: Tidorum Ltd Message-ID: References: <4905b963-0036-4129-8050-fb26ef0154d6@googlegroups.com> <32314026-23ae-45b8-a4c5-e589e7d79de2@googlegroups.com> <64e3c342-d042-40a2-8a16-b1f0cdff9f16@googlegroups.com> <91527f7c-0679-4c21-95c7-a07f3fff265d@googlegroups.com> <8cc51443-23f6-4736-a862-d0223998fc2e@googlegroups.com> <753a3719-e15f-4626-b3cc-ac76f7ef7499@googlegroups.com> <8ee504a6-b371-42ef-a91e-bbb70e3b81d8@googlegroups.com> <2665b0e1-0558-45ac-9a26-7eb7a1ecd689@googlegroups.com> Mime-Version: 1.0 X-Trace: individual.net nLwvjuP2JjApIIHCp+D0ZA1LdY6Gjsp/Eg2smS4cFf6cVr41OLdozMKDZ9DtaOzXnr Cancel-Lock: sha1:mTrjTSV5iQG/By/3VS4DgqBz4kw= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 In-Reply-To: X-Received-Bytes: 3480 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Date: 2013-02-04T16:24:28+02:00 List-Id: On 13-02-04 12:09 , Ada novice wrote: > On Monday, February 4, 2013 10:37:49 AM UTC+1, Niklas Holsti wrote: >> >> Number : constant Long_Float := 100.0; > > When assigning constants, I always use > > Number : constant := 100.0 > > and then lets the compiler decides how it wants to treat the constant > when say multiplying with a type such as integer, (You can't multiply 100.0 with an integer, unless you do a type conversion or define your own multiplication operator.) > float etc. Is this good practice? Depends. Such type-less "named numbers" (in Ada-speak) have different properties than typed constant objects: if you combine them in an expression, the compiler uses unbounded-precision and unbounded-range arithmetic, which may or may not be what you want; and they match any integer or floating-point type, respectively, which again may or may not be what you want. In other words, if you declare: Max_Number_Of_Apples : constant := 42; you may mistakenly use Max_Number_Of_Apples in a place where you are counting oranges. But if you define different types, Apple_Count_Type and Orange_Count_Type, and then declare Max_Number_Of_Apples : constant Apple_Count_Type := 42; then the compiler should detect if this constant is used in an orange-counting context. I use named numbers (usually integers) when I want to be absolutely sure that all computation with them is done statically, at compile-time, or when I want to give other parts of the program the ability to use the same numbers with various types (which is not often sensible). Nowadays I mostly use typed constant objects. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .