Tuesday, June 26, 2018

Unexpected behavior of Python in integer division

There turns out to be a number of cases where Python give you results different from your expectation, or from your implicit assumption from years of math experience. Described here are the two representative cases that gave me quite a bit of headache to figure out while trying Project Euler problems #91 and #160. In both lessons, my codes had to work since they had nothing complicated, but they didn't. Then I need to suspect every single bit of logical steps involved, double checking whether they work exactly as I expect them to.

The first case was about the division operation between integers, particularly when you have a number which is an integer multiple of another: 15 * 26 / 13 gives you 30.0 but 15 / 13 * 26 gives 29.999999999999996. The latter is probably satisfactorily close to, BUT LOWER THAN the expected value of 30. It may not be an issue if this is your final output, but it can make a big difference if you use it as intermediate result subsequently fed to a case statement executing completely different codes depending on whether it is greater or less than 30. The reason is obvious: because division operation is always executed as floating point numbers, even when both arguments are integers.

Implicit conversion to floating point numbers can cause a different type of trouble in integer division: lost significant digit / precision. Python is famous for allowing unlimited number of significant digits to represent integer, but not floating point number. You might expect that Division of A = 265252859812191058636308480000000 by B = 10 removes a zero at the end, retaining all other significant digits. But you will lost many digits by going through floating point number.

How can we avoid this pitfall? Make sure to use true inter-integer operators such as // or %. Unlike A/B, A//B gives the correct and expected answer.

C seems to be free from this issue. Both 15 * 26 / 13 and 15 / 13 * 26 gives 0.000000.

-------------------------------------------------------

Two opposite sides of programming: it can easily drive you nuts, but at the same time you can crack it all the way down to binary digit (0 vs. 1) level as long as you are willing to and are persistent.

Wednesday, June 20, 2018

iPad mount for rear-facing baby

Showing movie clips to babies is definitely a great way for them to ease the trouble of being on board for distance. The problem was that whoever sits next to the baby had to hold the iPad throughout in a quite inconvenient and awkward angle of upper body and wrist. For which time the baby sitter could have done something else like web surfing, had they have an iPad mount for rear-facing infant. Unfortunately most of the available products are for forward-facing kids, or for a car where the rear seat headrest can be pulled up to expose the shaft underneath that can be used to wrap velcro around. As my car has fixed rear headrests, the only option for me was a suction cup iPad mount. Unfortunately again, those products were designed for front windshield mount, meaning arms are typically short. So my solution was, to buy a long arm one and test mount it on the rear windshield with fingers crossed. Voila! It works quite nice. I'm so happy with the beyond-prescription tweak.

* FBA_CH-3B model manufactured by DHYSTAR.


Sunday, June 17, 2018

vtech baby phone repair

Life is full of stuffs to fix / repair. Today I had a little cute thing: a vtech baby phone. After ~half a year of infrequent use, the unit started turning off by itself a few seconds after being turned on. Battery should have been okay, since the light turns on and speaker volume (turn-on melody) was moderate for the first few seconds. I suspected lead solder for power switch cable was loose. But everything looked great when I opened the back cover. A bit of Google search says low battery can cause a number of unwelcomed behaviors. So I replaced battery just in case, and voila! It works. Probably its electric circuit has some protection against low input voltage.

Reassembly took some time since I took it apart way too much, but I am happy that the unit is back in service.

Wednesday, April 11, 2018

Project Euler #94 : Almost equilateral triangles

Here is the problem description:

It is easily proved that no equilateral triangle exists with integral length sides and integral area. However, the almost equilateral triangle 5-5-6 has an area of 12 square units. We shall define an almost equilateral triangle to be a triangle for which two sides are equal and the third differs by no more than one unit. Find the sum of the perimeters of all almost equilateral triangles with integral side lengths and area and whose perimeters do not exceed one billion (1,000,000,000).

* update on 2019/1/6:
I dropped the original contents in compliance with Project Euler policy, discouraging posting solution or hint on public space. Message me if you would need help. I believe you should know Heron's formula.

Tuesday, February 27, 2018

Retain format / style of text pasted into Gmail composition

While coding, there are cases one will want to keep important code snippets into a private repository using Gmail. That's what I wanted to do, and what I actually did. Or one may want to send it to their friends to help them.

I prefer keeping the original formatting of the IDE to losing them while pasting. Unfortunately, simple copy from the IDE and paste into Gmail sometimes worked but not always. Google search gave results mostly about how to remove annoying formatting instead of how to retain them, quite frustrating.

There is one option one to check:


If the 'plain text mode' is checked in the mail composition window (unchecked in the above image), then you will see the contents pasted there only. Formatting is all gone.

Saturday, February 24, 2018

Extending life expectancy of USB external hard drive

I have long used two desktops; one at home, one at work. For working storage, external HDD was my choice since I didn't like having my data saved on a commercial cloud service for which I have little control. (A definitely outdated and inefficient way; I now use cloud at least partially). So far I used three different external HDDs. 1TB from Seagate / Toshiba / Western Digital. The first two gradually died in about a year, but the last and the current one survived over 3 years and 4 months. In fact this baby showed some death sign recently, so I bought another one, but I didn't have to open the package.


The biggest difference is that I paid a lot of attention with the current one when disconnecting it from desktop. In the past I force unplugged USB cable even at the error message saying 'Windows can't stop your device upon ejection attempt, assuming it be okay since the read / write process should be completed. Now I have it a rule to set the HDD offline first upon ejection failure, although I still force unplugged infrequently. Probably less than 1%.

One needs to go to Control Panel -> Administrative Tools -> Computer Management -> Disk Management, right click and HDD, and then check 'offline'.


Note that, if you disconnect the HDD after setting it offline and reconnect, then the autoplay doesn't pop up, and the HDD won't be visible in the Windows Explorer. You have to manually set it back to online using the same context menu as the above. This could be a problem in particular when you come to work the next day, but do not remember that you manually have set the HDD offline. So I typically reconnect the HDD and restore to online right away.

I do not clearly remember the symptoms of dying HDD since I haven't had the issue for the last ~3 years, but I think being unable to eject your HDD normally progressively more often was one of them. By then, you might start seeing warning like 'this device can perform faster if you connect to USB 3.0 port blah blah..' even though it is connected to USB 3.0 port. Or FAT system of the HDD becomes corrupt so that you cannot read some files. In the end your entire HDD becomes inaccessible. Or you will see a message like 'this drive is not formatted. will you want to format?'.

In short, I strongly recommend you to safely disconnect your HDD; meaning that you should make sure that the HDD is truly ready to be disconnected, sometimes by manually setting if offline.

Tuesday, February 20, 2018

Imitating NYC subway control center using Google Map

I recently came to learn that, in an effort toward open data project, New York city MTA (Metropolitan Transportation Authority) offers API feeds for quasi-real-time subway traffic data. Then, wouldn't it be nice if I can plot the train locations on Google map, and make a movie of screen shots taken at an interval? It should look something like a grand monitor screen in a control center. So I started this brief self-project.

Understanding API feeds, and basic analysis of the returned data were done with Python, but the real code was written in PHP and Javascript. More specifically, back-end task such as feed query / parsing / translation of station ID into GPS latitude + longitude pair were implemented with PHP + MySQL, and front-end Google Map plot with Javascript-based Google Maps API. The entire codes are available in my GitHub repository. The outcome looks like below. You can execute yourself here. Note that the page does not update by itself; you need to manually refresh the page.

The data are not truly real-time since MTA DB is not updated every seconds or more often. (Technical reasons will be spelled out below for those who are interested). So I do NOT guarantee precision of the markers. At this point this is a just-for-fun project. I learned basic components of web development using Python / PHP / MySQL / Javascript / xml / API (MTA, Google Maps) / Node.js despite not very deep. Which made me to re-realize how much failed trial and attempts a success demands. Hope you enjoy.


[Technical issues]
1. Statistical analysis of MTA feed revealed that the data are approximately 99 seconds old (with 28 seconds of standard deviation). Quasi but not truly real time.

2. The feed returns only the last station a train stopped at the moment of query, not a location in between. That's why all trains seem to be at a station, which must not be true at all. Also that is why it is very likely for you to see multiple trains at one stop. You might need to zoom all the way in to distinguish them.

3. MTA tells user only the stop sequence number, which has to be properly translated to unique station ID. This is a complicated job since the total number of stops depend on a variety of factors such as: (1) daytime or late night? (2) weekday or weekend? (3) express? (4) any temporary service change due to construction? I could extend the application so that it can incorporate all of these variation, but not yet.

4. Raw data from MTA shows a few trains at the terminal station. They are not shown on the map just for convenience.

Philips SAECO Xsmall espresso machine repaired

I have a SAECO Xsmall espresso machine. I bought it in Dec 2014, and brewed 2~3 cups of espresso per day on the average over the last 6 year...