Re: Computer programmers' habits in electronics
- From: Puckdropper <puckdropper@xxxxxxxxx>
- Date: 22 Dec 2005 10:58:19 GMT
"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
.
- References:
- Re: Computer programmers' habits in electronics
- From: onehappymadman
- Re: Computer programmers' habits in electronics
- From: Rich Grise, but drunk
- Re: Computer programmers' habits in electronics
- From: Rich Grise, but drunk
- Re: Computer programmers' habits in electronics
- From: Tim Wescott
- Re: Computer programmers' habits in electronics
- From: Spehro Pefhany
- Re: Computer programmers' habits in electronics
- From: Mike Young
- Re: Computer programmers' habits in electronics
- From: Spehro Pefhany
- Re: Computer programmers' habits in electronics
- From: Mike Young
- Re: Computer programmers' habits in electronics
- From: Puckdropper
- Re: Computer programmers' habits in electronics
- From: Mike Young
- Re: Computer programmers' habits in electronics
- Prev by Date: Re: I'm surprised...
- Next by Date: Re: Pizza Tonight
- Previous by thread: Re: Computer programmers' habits in electronics
- Next by thread: Re: Computer programmers' habits in electronics
- Index(es):
Relevant Pages
|