Re: Computer programmers' habits in electronics



"Mike Young" <boat042-spam@xxxxxxxxx> wrote in
news:cytqf.40824$tV6.37073@xxxxxxxxxxxxxxxxxxxxxxxxxx:

*snip*

>> Ada doesn't have an unsigned type, so I'm using an integer instead.
>> The code would be the same regardless.
>
> I recall vaguely that Ada lets you define ranges? Something like type
> uint32 is integer[0..4billionsumthing]? In any case, integer would be
> less restrictive. Is this an advantage or disadvantage in the
> application?

It's both... It's a major advantage in that I don't have to mess with
anything extra, it's also a disadvantage in that what happens with
negative results (say from an integer overflow) is undefined. I should
have used "Natural" instead.

You can define other types, but that caused trouble with some of the type
casting I had to do. (My solution involved using a log which Ada only
seems to have defined as float.)

Anyway, to emulate a unsigned int on my machine at least, you'd do:
type unsigned is new long_long_integer range 0..4294967295;



*snip code sement *
>
> That returns true for even numbers. (I think. Is rem the modulus
> operator? returning the remainder of division by 2?)

Not exactly... rem and mod are equal for positive numbers, but different
for negative values. (Close enough in this case.)

> I was looking for
> true if val is 2 raised to some integer power, 2^n, where n is a
> positive integer. How would you write that in Ada?

My mistake. I misunderstood the question. I get some mathematic terms
messed up at times. Here's the code that does as you wish:

function IsPowerOf2(val: integer) return boolean is
subtotal: float;
result: integer;
result2: integer;
begin
subtotal := log(float(val), 2.0);
result := integer(subtotal);
result2 := 2**result;
return val = result2;
end IsPowerOf2;

Basically what I'm doing is taking the log of a number, raising it to the
base again and comparing results. If there's no floating point part,
then the numbers should be equal.

>
> Also, this seems a good time to discuss the read-mostly intent of
> Ada's more verbose coding style, versus the (alleged) "write-only"
> brevity of some other languages.

I like "write-fast" rather than "write-only." You can make C-based code
as readable as read-mostly languages, it just takes care.

> For example, I would likely write the
> above function in C++ as a one liner:
>
> bool IsEven(unsigned val)
> {
> return 0 == val % 2;
> }
>

function IsEven(val: natural) is
begin
return 0 = val rem 2;
end IsEven;

> Bit-operation equivalence aside, what are your thoughts on the
> readability of both versions, in context of both a large application
> and just this one simple function in isolation?
>

As implied above, any language's code (well, except purposely obfuscated
ones such as White Space (-;) can be written so as to be very readable.
IMO, the begin and end in Ada makes reading (especially for debugging)
nested code much easier because you don't lose yourself in a fluery of
end braces - }.

I also feel that Ada demands good code while C++ is just happy to have
something to compile. In C++, assigning a float to an integer is as
simple as float = integer while in Ada you have to explicity cast it:
float := integer(integer)

Puckdropper
--
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we
still enjoy using the old computers. Sometimes we want to see how far a
particular system can go, other times we use a stock system to remind
ourselves of what we once had.

To email me directly, send a message to puckdropper (at) fastmail.fm
.



Relevant Pages

  • Re: Ada array vs C pointer (call by reference)
    ... I have a C library function which takes a float * as a parameter. ... where data is an array of float which size is determined by pwmCount. ... In Ada, I have: ... I've never heard of an Ada compiler that doesn't, ...
    (comp.lang.ada)
  • Re: Reading Float Data from a binary file into ada
    ... but store it into a 32 bit Float. ... Any reason you can't call this C function from Ada? ... a round about syntax to help me get started in setting up the memory ...
    (comp.lang.ada)
  • Re: Ada array vs C pointer (call by reference)
    ... Ada type "Float", to ensure that you're using the same kind of float ... that the C routine expects. ... Ada can't guarantee it directly. ...
    (comp.lang.ada)
  • Re: Reading Float Data from a binary file into ada
    ... Essentially I was given a Visual Basic program that dumps a binary configuration file with all of the variables in a set. ... The variables are each 32 bit floats, with the first 16 bits being the integer part and the second 16 bits being a representation of the fraction (I'm not sure if this is stanard - but its just how VB dumps the data). ... There is a C counterpart to this that makes use of a 'union' to grab the data 1 byte at a time, put them into a char array of size 4, then use a 32 bit float to reference the data. ... Is there somehow I can do this in ada as well? ...
    (comp.lang.ada)
  • Re: urgent question - generics
    ... -- types of calls both Ada and non-Ada procedure. ... return float is ... pragma Import (Ada, tst); ... What I did for my first Ada assignment after modifying it, ...
    (comp.lang.ada)