From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Numerical calculations: Why not use fixed point types for everything?
Date: Mon, 04 Feb 2013 16:24:28 +0200
Date: 2013-02-04T16:24:28+02:00 [thread overview]
Message-ID: <an9uksFhgjpU1@mid.individual.net> (raw)
In-Reply-To: <ee696908-df30-4e46-a9f4-645749d3a388@googlegroups.com>
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
. @ .
next prev parent reply other threads:[~2013-02-04 14:24 UTC|newest]
Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-17 10:33 Numerical calculations: Why not use fixed point types for everything? Ada novice
2013-01-17 14:40 ` Nasser M. Abbasi
2013-01-17 16:16 ` Adam Beneschan
2013-01-17 17:00 ` Georg Bauhaus
2013-01-17 16:25 ` Adam Beneschan
2013-01-18 9:17 ` Ada novice
2013-01-18 17:24 ` J-P. Rosen
2013-01-18 17:52 ` Jeffrey Carter
2013-01-18 18:15 ` Dennis Lee Bieber
2013-01-18 18:59 ` Adam Beneschan
2013-01-19 4:41 ` Dennis Lee Bieber
2013-01-19 6:26 ` Jeffrey Carter
2013-01-19 14:14 ` Robert A Duff
2013-01-25 12:16 ` Paul Colin Gloster
2013-01-24 10:55 ` Ada novice
2013-01-24 11:47 ` Simon Wright
2013-01-24 14:21 ` Ada novice
2013-01-20 0:05 ` Robin Vowels
2013-01-18 23:06 ` Robin Vowels
2013-01-18 19:09 ` Adam Beneschan
2013-01-18 21:39 ` Randy Brukardt
2013-01-19 7:02 ` Ada novice
2013-01-25 12:09 ` Paul Colin Gloster
2013-01-25 12:23 ` Paul Colin Gloster
2013-01-28 9:09 ` Ada novice
2013-02-01 10:53 ` Ada novice
2013-02-01 15:01 ` Shark8
2013-02-02 18:55 ` Ada novice
2013-02-03 4:05 ` Shark8
2013-02-04 6:23 ` Ada novice
2013-02-04 6:43 ` Niklas Holsti
2013-02-04 7:27 ` Ada novice
2013-02-04 9:37 ` Niklas Holsti
2013-02-04 10:09 ` Ada novice
2013-02-04 14:24 ` Niklas Holsti [this message]
2013-02-04 16:44 ` Jeffrey Carter
2013-02-04 21:12 ` Niklas Holsti
2013-02-04 17:31 ` Robert A Duff
2013-02-04 21:20 ` Niklas Holsti
2013-02-02 21:08 ` Nasser M. Abbasi
2013-02-04 6:17 ` Ada novice
2013-02-05 2:27 ` Randy Brukardt
2013-02-06 7:11 ` Ada novice
2013-02-07 6:03 ` Randy Brukardt
2013-02-07 8:43 ` Shark8
2013-02-08 3:17 ` Randy Brukardt
2013-02-08 6:20 ` Ada novice
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox