Published by dpk on Jul 31st, 2008 in General with No Comments
I’ve learned something today. Apparently, there’s more than one way to round a number. There’s the easy, “common” method that everyone learns, where you simply look at the digits you want to zero-out, and if the first digit is >= 5 you round up, and <= 4 you round down.
And then there’s the number-nerd way called “round-to-even”. From Wikipedia’s article on Rounding:
- Decide which is the last digit to keep.
- Increase it by 1 if the next digit is 6 or more, or a 5 followed by one or more non-zero digits.
- Leave it the same if the next digit is 4 or less
- Otherwise, if all that follows the last digit is a 5 and possibly trailing zeroes; then change the last digit to the nearest even digit. That is, increase the rounded digit if it is currently odd; leave it if it is already even.
For example:
- 3.013 rounded to hundredths is 3.01 (because the next digit (3) is 4 or less)
- 3.015 rounded to hundredths is 3.02 (because the next digit is 5, and the hundredths digit (1) is odd)
- 3.045 rounded to hundredths is 3.04 (because the next digit is 5, and the hundredths digit (4) is even)
This came up for me while trying to figure out how MySQL rounds numbers. Rather than create a new ROUND() function, they modified its behavior between versions. This is documented, at least, and provides a more predictable result independent of the OS. However, I am not clear on the unspecified distinction between an “exact value” and an “approximate value”. More number-nerd stuff, I assume.
Published by dpk on Jul 28th, 2008 in General with 1 Comment
Kelmanskiy and 125 other soldiers assigned to Fort Lewis’ newest Stryker Brigade Combat Team are part of a new program to teach soldiers rudimentary Arabic. The idea is that once deployed to Iraq, they’ll be able to communicate with local Iraqis to help their units better distinguish between allies and enemies.
Fort Lewis soldiers learn Arabic to better prepare for war
Just in time, too. I hear we’re talking about launching an armed conflict in Iraq. It should only last a few months but anything we can do in advance to try to smooth out communication problems should be worthwhile.
Published by dpk on Jul 25th, 2008 in Code with No Comments
This may be old news for people more familiar with perl internals.
dpk@dpk1:~$ perl -version | head -2
This is perl, v5.8.8 built for i486-linux-gnu-thread-multi
dpk@dpk1:~$ cat test1.pl
#!/usr/bin/perl
use strict;
my $x = 0;
while ($x < 100)
{
&doit();
$x++;
}
sub doit() {
my $foo = 'foobar'; my $x = 0; my $y = 0;
while ($x < 1000000)
{
if ($foo eq 'foofoo')
{
$y++;
}
$x++;
}
}
dpk@dpk1:~$ perl -d:DProf test1.pl;dprofpp
Total Elapsed Time = 52.07938 Seconds
User+System Time = 52.07938 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
99.9 52.07 52.070 100 0.5207 0.5207 main::doit
0.00 - -0.000 1 - - strict::bits
0.00 - -0.000 1 - - strict::import
0.00 - -0.000 1 - - main::BEGIN
And then:
dpk@dpk1:~$ cat test2.pl
#!/usr/bin/perl
use strict;
my $x = 0;
while ($x < 100)
{
&doit();
$x++;
}
sub doit() {
my $foo = 'foobar'; my $x = 0; my $y = 0;
while ($x < 1000000)
{
if ('foofoo' eq $foo)
{
$y++;
}
$x++;
}
}
Total Elapsed Time = 51.31948 Seconds
User+System Time = 51.30948 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
100. 51.31 51.310 100 0.5131 0.5131 main::doit
0.00 - -0.000 1 - - strict::bits
0.00 - -0.000 1 - - strict::import
0.00 - -0.000 1 - - main::BEGIN
I’ve repeated this little test multiple times, with different several different inputs, but the result is always the same. if ('string' eq $variable) is faster than if ($variable eq 'string')
Published by dpk on Jul 23rd, 2008 in Linux, Rants with 1 Comment
One of the more annoying things to come about in the Linux/open source world is the use of “info” pages, replacing the old workhorse standard of “man” pages. In doing so, developers often ignore documenting crucial elements in a man page, instead leaving this gem at the end:
SEE ALSO
The full documentation for date is maintained as a Texinfo manual. If
the info and date programs are properly installed at your site, the
command
info date
should give you access to the complete manual.
Isn’t that great? The documentation for date says that as long as date is “properly” installed, I should have the real documentation. Why not put it all in the man page? Maybe it’s because this sort of shit wouldn’t fly with the die-hards, quoted from page 1 of date’s info documentation:
27 Date input formats
*********************
First, a quote:
Our units of temporal measurement, from seconds on up to months,
are so complicated, asymmetrical and disjunctive so as to make
coherent mental reckoning in time all but impossible. Indeed, had
some tyrannical god contrived to enslave our minds to time …
WTF.
Published by dpk on Jul 6th, 2008 in Education with No Comments
An educational update:
I finished spring quarter with some good grades, bringing my credit total to 20: 10 credits of math, 5 of English, and 5 of philosophy. I decided to take it easy in the summer quarter, and am taking two online classes, which means I am home for dinner every night (a big plus!).
One of the classes is beginning programming (CSC 110, JavaScript). I’ve been programming since I was a kid, so the JavaScript class is straightforward (although I have caught myself making big mistakes..). In order to complete an AS degree, I have to have at least 5 CSC credits, and “unfortunately” they do not allow you to test out. I put unfortunately in quotes because it’s actually pretty nice to take it a bit easier, and because it means I’ll be earning at least 10 CSC credits, which will probably look better should I transfer to a 4 year school some day. I am taking the opportunity to update my knowledge of XHTML, and just generally reinforce good coding practices.
The other class is introduction to logic (PHIL 106) and is pretty interesting. I’m learning how to read and comprehend arguments presented as symbols, and how to identify valid and invalid arguments. By taking this class relatively early on, I hope to use the skills in later classes, to ensure what I write is at least valid if not correct.
I wasn’t entirely sure that I was going to complete a degree when I first decided to take some classes. If I had not received good grades in the first quarter, I think it’s fair to say I would have stopped right then. Now I feel pretty sure that I am going to complete a 2 year degree, and I might do it in exactly 2 years. I was feeling ambitious, and registered for three classes in the fall: Java programming (CSC 142, online), calculus I (MATH &151), and sociology (SOC 101, online). If I finish all three, that’ll bring me to a total of 45 credits in my first year. Woo.
Published by dpk on Jun 25th, 2008 in Rants, minipost with 2 Comments
Do blog authors realize how stupid “after the jump” looks when people view the article in full? I must frequently view entire posts, because I don’t remember the last time I saw the phrase where it was followed by a “read more” link.
I hope the phrase dies a choking death.
Published by dpk on Jun 20th, 2008 in General with No Comments
Goatseasaurus was once here, but Google “did evil” and took it down. Anyone happen to know where else it goes? I wonder if Spore can be easily hacked to upload videos to non-YouTube sites.
Published by dpk on Jun 4th, 2008 in Education with No Comments
I think I am missing that bone in the brain that allows you to prove trigonometric identities. I just got done with the first half of a final, and I totally botched one of the questions, which was “Prove tan (pi / 4 + x) = (1 + sin2x) / cos2x”. I got as far as changing the left side to (cosx + sinx) / (cosx - sinx), but then I got stuck. Wasted a full sheet of paper trying different ways to solve it.
What really gets me is I know we’ve done this problem (or something similar) a couple of times before, but I’ve been unable to figure it out each time. I know all of the standard identities and half-angle/double-angle/addition/subtraction equations but I can’t make them work here. It’s probably something completely obvious, too.
Update: Yeah. It was obvious.
- tan (pi / 4 + x) = (tan (pi / 4) + tan (x)) / (1 - tan (pi / 4) tan (x))
- tan (pi / 4) = 1, so (1 + tan (x)) / (1 - tan (x))
- Multiply by cos (x) / cos (x): (cos (x) + sin (x)) / (cos (x) - sin (x))
- Multiply by the (cos (x) + sin (x)) / (cos (x) + sin (x))): (cos^2 (x) + 2 sin (x) cos (x) + sin^2 (x)) / (cos^2 (x) - sin^2 (x))
- cos^2 (x) + sin^2 (x) = 1, 2 sin(x) cos (x) = sin (2x), cos^2 (x) - sin^2 (x) = cos (2x), so:
- Finally we have LHS (1 + sin (2x)) / cos (2x), same as RHS.
All I needed was a few days to ruminate over it. Bah.