comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Does Ada support endiannes?
Date: Tue, 27 Dec 2011 13:35:06 +0100
Date: 2011-12-27T13:35:06+01:00	[thread overview]
Message-ID: <1qvq7hppsg0u.1oomxndbukhtr.dlg@40tude.net> (raw)
In-Reply-To: 4ef9a028$0$6622$9b4e6d93@newsspool2.arcor-online.net

On Tue, 27 Dec 2011 11:38:32 +0100, Georg Bauhaus wrote:

> On 26.12.11 11:47, Dmitry A. Kazakov wrote:
> 
>> Your code uses obscure assignments to some fields of some record type,
> 
> Or, as one might say, the expression
> 
>  256 * Incoming.Byte_2 + Byte_1
> 
> is an obscure arithmetical formula involving the magic number 256.

   Data (Index) * 2**8 + Data (Index + 1)

> It takes careful study by someone with mathematical training and also
> requires knowledge of language fine print and data representation or
> else 256 is not meaningful, but obscure!

You need mathematical training in order to understand how numbers are
encoded. Representation clauses require much *additional* understanding on
top of that. Difficulties people keep on having with understanding and
proper usage of the clauses reflect this simple fact.

> So, regarding obscurity, the formula would at first show little advantage.
> Neither will it be more readable in this sense.

It is. This is the way these things are defined in first place. E.g.

   http://en.wikipedia.org/wiki/Two_complement

There is simply no other way. The only thing you can do with a number is to
use numeric operations. Anything else is on top of that. Remember that
binary representation of a number is an encoding, itself defined through
mathematical operations.

>> 4095 -> 2#1111_1111# 2#0111_1111#
>> 4096 -> 2#1000_0000# 2#1000_0000# 2#0000_0001# 
>> ...
>> 
>> [Chain codes are used when the upper bound of the transported number is
>> unspecified and it is expected that lesser values are more frequent than
>> bigger ones. UTF-8, is chain code.]
>> 
>> Care to write a representation clause for the above?
> 
> Arguably, a chain code might not what a regular programmer would see
> as something to which a record representation clause will apply as a whole.

Regular programmer does not bother about encoding anyway. But if someone
have to, my advise is: don't waste your time on understanding
representation clauses. They won't help you. Beyond trivial cases, if you
are not a language lawyer, you will never understand why that mess works or
does not work.

> What will the concrete data type for the above chain code look like,

An integer number

> as a whole, if it is not a list or array of some sort and if, for example,
> a chain code is being used precisely because there is a lot more to link
> than can be represented in what physically corresponds to
>   range 0 .. 2**System.Word_Size?

That was another point against representation clauses: they fall short when
it comes to validity checks. The peers of the protocol like above would
impose certain implementation-specific limits on the number range. Upon
decoding (using arithmetic operations), the program would necessarily get
Constraint_Error when the limit is exceeded, and reroute to Protocol_Error.
It is a very simple and effective method handling protocols in general.

> It seems there is value in distinguishing an encoding from a representation,
> and establish their relationship.

(Ah, you know my weakness of giving definitions. (:-))

OK, here it one:

[Machine] representation is an encoding with the alphabet based on the
values of a machine type.

P.S. A not yet mentioned argument against representation clauses is their
unfriendliness to correctness proof and testing (you would likely need a
full coverage test).

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  reply	other threads:[~2011-12-27 12:35 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-12  8:57 Does Ada support endiannes? Gerd
2011-12-12  9:23 ` Niklas Holsti
2011-12-12 11:27 ` Dmitry A. Kazakov
2011-12-12 12:44   ` Gerd
2011-12-12 19:23     ` Jeffrey Carter
2011-12-13 14:25       ` Gerd
2011-12-13 14:19     ` Gautier write-only
2011-12-14 16:16       ` Gerd
2011-12-14 18:16         ` Dmitry A. Kazakov
2011-12-14 20:16         ` Gautier write-only
2011-12-15 11:27           ` Gerd
2011-12-15 13:01             ` Simon Wright
2011-12-15 13:37             ` Dmitry A. Kazakov
2011-12-15 20:12             ` Jeffrey Carter
2011-12-12 12:46   ` Gerd
2011-12-12 13:22     ` Dmitry A. Kazakov
2011-12-12 17:07       ` Charles H. Sampson
2011-12-12 18:33         ` Dmitry A. Kazakov
2011-12-14  5:19           ` Charles H. Sampson
2011-12-14  8:56             ` Dmitry A. Kazakov
2011-12-14  9:46               ` Simon Wright
2011-12-15  9:14               ` Charles H. Sampson
2011-12-15  9:46                 ` Dmitry A. Kazakov
2011-12-25 21:42                   ` Charles H. Sampson
2011-12-26 10:47                     ` Dmitry A. Kazakov
2011-12-27 10:38                       ` Georg Bauhaus
2011-12-27 12:35                         ` Dmitry A. Kazakov [this message]
2012-01-04  4:33                       ` Charles H. Sampson
2012-01-04 11:56                         ` Dmitry A. Kazakov
2011-12-12 13:33     ` Robert A Duff
replies disabled

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