Thursday, June 13, 2019

No Thirty-Seconds

Partly because I work with computers, the number thirty-two has often been on my mind. With thirty-two bits one can address about four billion locations in memory, or express a positive signed integer as large as about two billion. Going on forty-five or fifty years ago, the computer industry discovered that it needed thirty-two bits in an address. Tracy Kidder's book The Soul of a New Machine tells how Data General made its 32-bit computer. (Now and for some years, sixty-four bits has been standard, but quite recently I've heard from techies who couldn't process a file of size larger than two gigabytes.)

And the compass has thirty-two points. In "boxing the compass", one recites them in order clockwise from North to North by West. Any high school geometry student should be about to make a compass rose with the points using only compass and straight edge.

The other day, though, I discovered a property of thirty-two that hadn't occurred to me: it is the smallest positive integer that cannot be a day of a month. I was looking to validate some input to a script we use, and discovered that JavasSript will happily parse '2/31/2019' as a date, but not '2/32/2019'.  The former becomes March 3, 2019, the latter is not a valid date. The script was to run under the Microsoft Scripting Host (cscript.exe), but I found that this works the same in a browser console. In what follows, the output in in italic:
for (var i = 29; i < 33; i++) {
    var dateString = '2/' + i + '/2019';
    var d = new Date(dateString);
    console.log(dateString + ' -> ' + d);
}
2/29/2019 -> Fri Mar 01 2019 00:00:00 GMT-0500 (Eastern Standard Time)
2/30/2019 -> Sat Mar 02 2019 00:00:00 GMT-0500 (Eastern Standard Time)
2/31/2019 -> Sun Mar 03 2019 00:00:00 GMT-0500 (Eastern Standard Time)
2/32/2019 -> Invalid Date
I'm not sure what to say about this. Obviously, JavaScript has "thirty days hath September" worked out, and it is not saving time or space by skipping leap year calculations:
for (var i = 29; i < 33; i++) {
    var dateString = '2/' + i + '/2020';
    var d = new Date(dateString);
    console.log(dateString + ' -> ' + d);
}
2/29/2020 -> Sat Feb 29 2020 00:00:00 GMT-0500 (Eastern Standard Time)
2/30/2020 -> Sun Mar 01 2020 00:00:00 GMT-0500 (Eastern Standard Time)
2/31/2020 -> Mon Mar 02 2020 00:00:00 GMT-0500 (Eastern Standard Time)
2/32/2020 -> Invalid Date
I know how to work around this without much effort. I'm just a little surprised that I should have to.

[Edit: changed "two million" to "two billion"]

2 comments:

  1. “With thirty-two bits one can address about four million locations in memory” Million? Oops!

    ReplyDelete