|
Grex > Jellyware > #85: Rounding numbers between not so arbitrary values |  |
|
| Author |
Message |
nharmon
|
|
Rounding numbers between not so arbitrary values
|
Aug 30 19:30 UTC 2007 |
Premise: In the United States, aircraft flying in class A airspace above
flight level 410 (41,000 feet above mean sea level) are required to fly
at specific altitude levels. Aircraft flying in a easterly direction
(000 to 179 degrees) are required to fly at altitudes starting at 41,000
and increasing in 4000ft intervals (FL410, 450, 490, 530, etc). Aircraft
flying in a westerly direction (180 to 359 degrees) are required to fly
at altitudes beginning at 43,000 and also increasing in 4000ft intervals
(FL430, 470, 510, 550, etc).
Problem: When checking an altitude variable in a flight planning
program, make sure the altitude meets the above requirements. If it does
not, round the altitude down to the next lowest compliant value.
Hint: The appropriate altitude levels can be expressed as sequences:
000-179: 41000 + 4000x where x is from 0 to 4
180-359: 43000 + 4000x where x is from 0 to 4
|
| 9 responses total. |
nharmon
|
|
response 1 of 9:
|
Aug 30 19:32 UTC 2007 |
This response has been erased.
|
nharmon
|
|
response 2 of 9:
|
Aug 30 19:42 UTC 2007 |
This response has been erased.
|
nharmon
|
|
response 3 of 9:
|
Aug 30 19:46 UTC 2007 |
This response has been erased.
|
nharmon
|
|
response 4 of 9:
|
Aug 30 19:52 UTC 2007 |
This is the first way I came up with for doing this:
if ( $direction < 180) {
for ( $i=1; $altitude >= (41000+4000*$i); $i++ ) {}
$altitude = 41000 + 4000 * ($i-1);
}
else {
for ( $i=0; $altitude >= (43000+4000*$i); $i++ ) {}
$altitude = 43000 + 4000 * ($i-1);
}
And here is the second:
if ( $direction < 180) {
$altitude = (floor(($altitude-1000)/4000))*4000 + 1000;
}
else {
$altitude = (floor(($altitude+1000)/4000))*4000 - 1000;
}
|
djdoboy
|
|
response 5 of 9:
|
Aug 31 01:08 UTC 2007 |
1)First off you shouldn't be using magic numbers.
2)Depending on the rest of the crap code, you might have a possible overflow
in your else statement
3)You are a fucking moron
4)You are a fucking moron
5)You are a fucking moron.
|
cross
|
|
response 6 of 9:
|
Sep 2 00:45 UTC 2007 |
Regarding #4; Ick; I wouldn't do this in Perl, if you can avoid it. You code
could be restructured as follows:
my $adj = 1000;
my $mul = 4000;
if ($direction < 180) {
$adjust = -1000;
}
my $altitude = floor(($altitude + $adj) / $mul) * $mul - $adj;
(Hopefully I didn't reverse the signs by accident.)
I'm not convinced that this is correct, though. What if $altitude == 100,000?
|
scholar
|
|
response 7 of 9:
|
Sep 2 00:48 UTC 2007 |
what about cats?
|
cross
|
|
response 8 of 9:
|
Sep 2 00:48 UTC 2007 |
Regarding #6; Whoops; $adjust in the if should be $adj.
Regarding #7; What about them?
|
trancequility
|
|
response 9 of 9:
|
Sep 2 14:51 UTC 2007 |
The way I understood perl, the language regex is native like + and -. I say
native because Perl doesn't have to resort to calling objects when dealing
with pattern matching. I'm sure for an extra 2 dollars, I could probably crank
out some code illustrating that perl regex is better than the whole OOP shit
taken by Java and Ruby.
|