How to Make a Time-b, I mean, Timepiece

—or— Why People Who Get Scared About Things They Know Nothing About Make Me Want to Cry

Here’s a pretty standard radio alarm clock made by Timex. It does a few things and it does them well. It has a time display, which includes the date and indicators of what kinds of alarms have been set. It has two FM tuners and one AM tuner. These can also be turned on as an alarm clock. It also can play soothing sounds.

A harmless looking alarm clock
A harmless looking alarm clock

Here is it taken apart, 14 minutes later after I dug out my tools, disassembled, and laid it all out into position to photograph. No harm was done in the disassembling and reassembling of this device. What does it look like? If I brought this into a classroom, what would a teacher think? What would the kids think? What if I put it in a different case? What if I detached the piezoelectric buzzer and speaker and mounted all of it into a box so all the parts were on display?

Oh no! It's infrastructure for a bomb!
<sarcasm>Oh no! It’s infrastructure for a bomb!</sarcasm>
Oh look! A digitally controlled RF tuner!
The power supply is in the foreground. The clock’s digitally controlled RF tuner is on the backside of the LCD readout. It’s slightly blurry and obscured, but some of the tuner’s components are visible.

Despite how it may look like to some people out there, it’s pretty harmless. It’s especially harmless since there are no explosives in it, nor would there be a way to set off explosives with it without making some extensive modifications. The worst part about it is plugging it in while it’s like this. If I touch the right part, it will hurt me (the part being the step-down transformer’s terminals and some of the capacitors’ terminals after the diodes acting as a rectifier).

<sarcasm>Here is another bomb. The explosives are implanted in the man’s arm.</sarcasm> Oh, wait. Sorry, it’s just a cyberpunk watch made by a cool guy.
Now, how do I know that? I know that because I’m familiar with the electronics inside of it. I wouldn’t have taken it apart and put it back together if I didn’t know I could handle what was inside of it. However, I wasn’t always that certain about electronics. I was just super curious and broke things. As a very young kid, I found my dads tools and snuck things like radios and clocks into my room and took them apart. When I first started doing this, I never could seem to put them back together again.

My dad worked repairing fire and security alarms at McClellan Air Force Base. This gave me two advantages. For one, he know enough about electronics so he could put things back together — if I had all the parts in tact. Two, he could also teach me about the innards themselves and about how electronics worked. He had also worked as an electrician. If I managed to blow a circuit breaker at home, he could show me how to fix it.

Eventually I learned about batteries, resistors, capacitors, inductors, diodes, and transistors. Then I went on to learn about Boolean logic and building digital circuits. This ranged from building simple one binary digit adders to clocks to the basics of building a computer. I learned about communication systems, too. How radios worked. How telephones worked. How the phone networks worked. How modems worked. It’s amazing what can be built from simple parts.

My little self with backpack full of electronics and maybe a book or two.
My little self with backpack full of electronics and maybe a book or two.
I used to take things I had taken apart to school. I had a bright yellow backpack that I used to stuff with electronics kits and other crazy things. Teachers must have seen a mess of wires sticking out of my bag at one point. This was Sacramento, CA in the 90s. It was before the 1993 bombing of the World Trade Center, but I continued to bring things to school even after that. I went to a private Christian (Seventh-day Adventist) school. I don’t know what impact that made on things, but no one seemed to be worried about me carrying around a bomb or parts for a bomb. Was it the era’s societal and cultural attitudes? Was it my whiteness and disarming smile? Was it the trusting and forgiving culture at the school? Was it the fact that my dad worked for the federal government? Probably all of those things. The important thing was that I hadn’t yet gotten into trouble (I did get into trouble years later, but the school dealt with me without the cops).

Current Paranoia

There’s, of course, the current tale of Ahmed Mohamed and his clock. He’s a 14 year old boy of Sudanese descent who happens to be an American kid and Muslim (why blame a kid for his religion?). His father happens to have run for presidency in Sudan, twice. Should any of that matter in this case? No, but it seems to have mattered. According to CNN, the teacher to whom he showed the clock was the one who felt threatened. Now, it might be arguable it was just the appearance of an eight inch wide metal pencil box with a time readout that caused the worry. I would say that’s an argument made out of ignorance and poor education regarding electronics. Given the circumstances, it would appear the whole context that caused the worry was:

a Sudanese-American Muslim kid + cultures and religions are foreign and scary + weird McCarthyism like propaganda about Muslim beliefs + electronic parts on display + ignorance about common household items and how the are made = this must be a threat

This isn’t an isolated incident in that school district. There’s been plenty of other incidents that don’t turn out so well. Here’s a great blog post on the Irving school district turning kids into criminals.

<sarcasm>It's bomb! No, wait! It's an advertising ploy!</sarcasm>
It’s bomb! No, wait! It’s an advertising ploy!
There are other incidents of people feeling threatened by electronic devices they don’t understand. Take, for instance, the 2007 Boston bomb scare. Adult Swim wanted to advertise their Aqua Teen Hunger Force Colon Movie Film for Theaters with an awesome guerrilla marketing campaign. They had worked with an organization to build these electronic devices that, in the afternoon, turned on an image of one of the characters holding up a middle finger. The display was made of large LED pixels. People got scared. Police investigated. Since it was a company, no one was hurt. The investigators insisted the devices were similar to bombs: they had an “identifiable power source, a circuit board with exposed wiring, and electrical tape.”

There’s another recent crazy school related incident that has nothing to do with bombs. It is the case of a gifted sixth grader attending Bedford Middle School in Virginia and Japanese Maple leaf or something similar. He was charged with possession of marijuana and suspended from school for a year. He’s now attending another school with none of his old friends and has to be searched for drugs before and after school. The school maintains its strict policy where anything claimed to be a drug or is believed to be a drug, the student possessing the item in question is treated like it is a drug. No exceptions.

A Common Thread?

My first reaction to these instances is that it is just plain ignorance. Ignorance in policy making. Ignorance of other cultures. Ignorance of other religions and spiritualities. Ignorance of science education. It makes me want to remedy a few of those things, and I’ll try to do so.

The longer I think about it, the more I realize it has less to do with ignorance and more to do with beliefs. Once people are told something and believe that, it becomes difficult for that person to change their beliefs. If they learn certain falsehoods about other cultures and people, they will only propagate those beliefs and they most likely wont be convinced of anything else.

If they come to believe that electronics is difficult and over their head, they won’t want to learn anything about it. In fact they’ll be convinced they can’t learn anything about it. Unless they can be convinced that they are smart enough to learn it — and there are ways of doing that — they won’t be able to learn anything. If one gives them small victories, through small lessons and challenges, things will start to change.

If people are convinced that strict authoritarianism is the solution and proper disciplinary methodology, things won’t change. Things will remain terrible. Kids’ lives will continue to be ruined.

It is up to each of us to enable others to have these small learning victories. It is our job to let people learn that they can be curious and can easily learn new things. It’s up to us to take advantage of those “teachable moments” to make the world a better place, one small step at a time.

An eight inch pencil box with the guts of a store bought alarm clock is not infrastructure for a bomb. It’s cool looking to some people and frightening to others. It’s only frightening because it’s misunderstood. A misunderstanding is not something over which a kid’s life should be ruined.

How do we solve this problem? How will you begin to understand another very different person? How do we unite these fragmented states and communities of America, blown apart by the swift reaction of misunderstanding?

Bonus Material!

Now, to really understand the difference between a clock and a bomb, read on. I’ve written some simple descriptions. I’m sure it’s understandable to the layperson; if not all of it, most of it. Go ahead, read on, I’ll wait.

What Makes a Clock a Clock?

An hourglass. Unabashedly taken from Wikipedia.
Figuring out what makes a clock is a fun task. So what is a clock? Is it a plate falling in water at a certain rate? Is it a certain number of grains of sand falling through an hour glass? Is it a pendulum swinging back and forth at a certain frequency? Is it a complicated set of springs gears weights and counter weights in a tiny wearable form? Is it a cesium atom?

At it’s most basic form a clock is a way of measuring time. It can take any number of forms from an hourglass to a sundial to an atomic clock. It doesn’t even have to tell one the time of day, but I think people have come to expect that from a clock.

I want to focus on the digital clock and how it’s made. A digital clock uses a power source to power all of its internal circuits. This power source can be either a battery or a wall plug that goes through a special circuit made to condition the power from the outlet for the rest of the innards in the clock.

The next most important part is a circuit that generates a signal that turns on and off at a specific rate. This signal is actually called a clock signal. The rate that it turns on and off is the clock frequency. Also long as this frequency is greater than one on/off cycle each second (1Hz), we can build a clock that displays hours, minutes, and seconds.

The part that comes after this is the counting circuits. These circuits normally feed into each other. A circuit will take the clock signal and convert it into a signal that turns on/off once per second. One circuit will take that signal and count seconds. Another circuit will count minutes. Yet another will count hours. Each time a seconds circuit counts to 59, it resets to 00 and passes a signal to the minutes circuit. The minutes circuit counts from 00 to 59, resetting to 00 each time and passing a signal to the hours circuit. The hours circuit counts the number of times the minutes reset to 00. It only goes from 01 to 12 and back again.

Each of these circuits hook up to another that takes the value of each counting circuit and maps it to a set of LEDs hooked up as seven segment displays for hours, minutes, and seconds. If I wanted to set the time on the clock, I would just have to adjust the value in each of the time counting circuits and they’ll start counting from that time. If the power is removed, it’ll reset all the circuits to 00. Which means 12 for the hours most of the time — this is why a VCR (oh, I mean DVD player) and digital clock radio will start flashing 12:00am when the power goes out.

What about adding an alarm to the clock? That’s pretty simple. Just add another set of circuits that hold the hour, minute, and second values to set the alarm. Add another circuit that compares those values to see if they’re the same. When they are the same make it send a signal to another circuit that switches power on to a piezoelectric buzzer circuit until a button is pushed to turn it off.

Of course that piezoelectric buzzer circuit could actually be anything. It could be a sprinkler system. It could be a light. It could be a motor. It could be a — use a wild imagination, just don’t get diabolical. Many of the same circuits used to build a digital alarm clock are the same circuits used for building processors and many other systems we all count on.

What Makes a Bomb a Bomb?

Disclaimer: I don’t condone building bombs. You’ll probably blow yourself up before anyone else. In many cases the explosives needed are also illegal to transport or take off your property (or make in a rental unit) unless one has a Federal Explosives License (and permission from all landowners involved — which isn’t going to happen in an apartment complex). If you want to learn more about potential threats and what they could look like (including how much they would weigh), take a look at the National Counter Terrorism Center website. Please also realize that terrorist attacks in the US are near nonexistent, so don’t be alarmist like the folks at the Irving, TX school. Let the professionals do their thing. Don’t try this at home. Respect the forces of nature. Don’t be a jerk. Etc.

Nitrogen Triiodide explosion captured by vastibadastoy on Flickr. Used without permission.
Explosives go boom! Explosives are interesting technologies. We couldn’t have made the freeway system we have without them. Most demolitions couldn’t happen with them. We also wouldn’t have Mount Rushmore without explosives. We also wouldn’t have some crazy movie stunts or special effects without them. We also couldn’t mount the wars for which we seem to have so much of a penchant if we didn’t have explosives. Not to mention, society probably wouldn’t have any gasoline or advanced electronic devices without the explosives used in mining.

Some explosives are very unstable and other are more stable. When I say unstable, I mean that it takes very little energy to make it explode. Nitrogen triiodide is a perfect example of this, because it explodes as soon as it is touched. I encourage watching a video on it with a fantastic demonstration of it. Stable explosives would be something like commercial TNT. It can be touched and handled without setting it off, so long as the right procedures are followed.

Fireworks are technically explosives — they’re just not very powerful ones (though they are still very dangerous). What makes an explosive powerful? An explosion, just like a fire, is a chemical reaction where a lot of heat is released. There is also a lot of pressure generated. Just like a fire, it takes something to start an explosion. Typically, this is something like a fuse, igniter, or a detonator. These can be chemical, mechanical, or they can be electrical.

Let’s say someone is building a bonfire and they want to make it huge. They’ll start with a small amount of wood, and will gradually add larger pieces to it in order to keep it going and make it larger. The same sort of thing is true for explosives. The amount of explosive needed is directly related to how large the explosion will be. Other factors impact the size and types of damage done: how directional the blast is, how much energy was used to start the explosion, etc.

A fairly common explosive use is in guns. Most police officers carry around a gun that has a bunch of bullets in it. A bullet is a projectile lodged in a casing loaded with gunpowder (an explosive). The casing also has a special type of contact explosive in the back of it, this is called a primer. Someone pulling a gun’s trigger releases a spring loaded “hammer” that hits the primer. This creates a release of heat and builds pressure inside the chamber of the shell (casing), this causes the gunpowder to explode directionally. Sometimes it is just the gunpowder that acts as the propellant. Other times, it is the pressure wave of the explosion in the chamber which launches the bullet through the barrel, sometimes at a speed faster than sound itself.

Atomic test footage showing the effect of such a powerful shockwave.
Something that is explosive has the the power to compress the air around it so much that this forms a pressure wave so intense it’s called a shockwave. This shockwave can destroy things in its wake. One of the most impressive demonstrations of this shockwave has to be the nuclear bomb test footage. One can see dirt rising off the ground and then the entire structure get pushed one way for a few seconds then pushed and twisted in the other direction as the air pressure returns to normal. The process of the air pressure lowering is called rarefaction — the opposite of compression. Things closest to the center of the explosion will be damaged the most. This is partly from the intensity of the pressure and partly from the intense heat. In the case of a nuclear explosion, the radiation also causes intense damage.

An explosive device of any size is concerning to have in a public space. It can hurt people. It should be reported. The problem of course, is knowing what is explosive and what isn’t. I think that people should be more concerned about a large backpack or suitcase laying around than an eight inch box with electronics in it.

Turning a Digital Alarm Clock into a Trigger Device is Insane

Lets say I wanted to turn my alarm clock into a model rocket launcher. I’ll make it launch the rocket when the buzzer goes off. I’d need to make a circuit that can take the buzzer’s signal and turn on a digital switch (debouncer + toggle flip-flop + power transistor) which would allow a power source’s energy to flow through the model rocket igniter, burning a small wire igniting the material around the wire. This of course, will launch the rocket — which is basically a controlled explosion.

I’d also need a much larger battery in order to set off the rocket igniter. It’s two AAA batteries would most likely not be enough, unless I used another component like a capacitor to store an electric charge big enough. However, that’s probably not feasible. It might not be able to store enough of a charge soon enough — so it is either more batteries or a larger battery.

Really died answering a cellphone.
Why is this stupid? Most clocks have small glitches in them once the piezoelectric element is switched out and sometimes even as is. Most of the time, one wouldn’t hear this. I’ve done several modifications to alarm clocks and every time, I either had to figure out how to filter the random pops, hisses, and other noise before my modifications could use the signal; or I would just live with it. The work around introduced more complexity and more circuitry, without which it would randomly turn on whatever was attached (or make a clicking sound through my stereo amplifier). So if I wasn’t careful with setting up my model rocket launcher, it could easily ignite the engine way before I wanted it to launch. It shouldn’t be too difficult to determine why this is a terrible idea.

Takeaway

Sure, anyone can claim that a clock is really infrastructure for a bomb. It can also be argued that it is infrastructure for a large, cobbled-together computer. The reality is that no one is going to use an unsophisticated and unreliable control like that unless it’s a screenplay’s plot or they really don’t care about the outcome. If they don’t care, something would probably happen without warning. It’s equally possible they would also just be sloppy and get caught before anything happens.

Abbreviating Huge and Minuscule Numbers with Math and JavaScript

The Problem

Let’s say we want to write an algorithm for formatting very large and very small numbers using the standard SI decimal prefixes (because we really, really do!). We want something that will take numbers and create the abbreviated output, as below:

  1. 123784692876928714 → ‘123.78P’
  2. 1237846924 → ‘1.24G’
  3. 0.0000000000000002342 → ‘23.42f’
  4. 0.0000000002342 → ‘23.42n’
  5. 0.00000002342 → ‘234.22µ’
  6. 0.000012345 → ‘123.45m’
  7. 1234 → ‘1.23k’

I’m sure there are quite a few ways to do this. What if we could write something with only two non-repeated conditionals using a few math concepts in eight lines of code? Would you be more interested in learning about the math behind it? Let get started. First, let take a look at this algorithm and the lookup table it uses:

The Proposed Solution

// This is the list of standard SI unit prefixes
var symbols =  {
  '-8': 'y',
  '-7': 'z',
  '-6': 'a',
  '-5': 'f',
  '-4': 'p',
  '-3': 'n',
  '-2': 'µ',
  '-1': 'm',
   '0':  '',
   '1': 'k',
   '2': 'M',
   '3': 'G',
   '4': 'T',
   '5': 'P',
   '6': 'E',
   '7': 'Z',
   '8': 'Y',
   '9': 'H'  // Though not official, 'hella' is hella big → 10^(9*3) or 10^27
};

function formatNumber(val, decimalPlaces) {
  var exponent = Math.log(val) / Math.log(10);
  var magnitudeExp = Math.floor(exponent);
  var hasIntegerComponent = magnitudeExp >= 0;
  var sign = (hasIntegerComponent) ? -1 : 1;
  var adjustment = sign * (magnitudeExp % 3);
  var significand = val / Math.pow(10, magnitudeExp + adjustment);
  var index = (hasIntegerComponent) ? Math.floor(magnitudeExp / 3) : Math.ceil(magnitudeExp / 3);
  return significand.toFixed(decimalPlaces) + symbols[''+index];
}

That’s it! That’s all there is to the algorithm! It will handle numbers having up to 29 digits and small numbers having a significant decimal place with 24 zeros in front of it. If that’s not good enough, an index and symbol for it should be added in the lookup table.

If math like this is foreign, a little head scratching might be in order. The math is actually pretty simple. There’s some interesting things about number bases and logarithms here.

Number Bases

Number bases? As a programmer, one might be familiar with a few number bases — like decimal, hexadecimal, binary, and, perhaps, octal. A number base signifies how many symbols can be used to represent one digit. For instance, in decimal — or base 10 — there are 10 symbols that can represent a digit. These digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. In binary, two symbols are used — 0 and 1. In Hexadecimal, 16 symbols are used — 0-9 and A-F. In octal, the digits 0-7 are used — though, octal is rare these days.

Each digit in a number in a certain base has a certain value, which is related to the number base. Each number position, starting with the ones place in the 0th position (the rightmost side) and continuing to the last or nth position, has a value which is a multiple of the base. Here are examples in a few number bases.

Decimal

1,234,567

Position: 6 5 4 3 2 1 0
Digit value: 1 2 3 4 5 6 7
Place value: 10^6 10^5 10^4 10^3 10^2 10^1 10^0
Position value: 1,000,000 200,000 30,000 4,000 500 60 7

To get the value of the number in decimal, simply add the values up:

\displaystyle 1,000,000 + 200,000 + 30,000 + 4,000 + 500 + 60 + 7 = 1,234,567

So each position has a value of the place times the value of the digit. In decimal, there is the ones place, the tens place, the hundreds place, the thousands place, the ten thousands place, and so on. Each digit happens to have the value that we normally give it, because the this is what we normally use and are used to.

What about other systems? Let us take a look.

Binary
10101010
Position: 7 6 5 4 3 2 1 0
Digit value: 1 0 1 0 1 0 1 0
Place value: 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
Position value: 128 0 32 0 8 0 2 0

To get the value of 10101010 in decimal, simply add the values up:

\displaystyle 128 + 0 + 32 + 0 + 8 + 0 + 2 + 0 = 170

Again, the value of the position if the value of the digit times the place value. It only has two digits and they still have the same values as the decimal versions of those digits. What about a system with more symbols than ours?

Hexadecimal

DEAD

“Wait, what!? That doesn’t even look like a number!”

In hexadecimal, it’s a perfectly valid number. The digits A-F are used in addition to digits 0-9. In fact, the digit A comes right after 9 and it has a value of 10. The digits B-F have the values 11-15, respectively.

Position: 3 2 1 0
Digit value: D = 13 E = 14 A = 10 D = 13
Place value: 16^3 16^2 16^1 16^0
Position value: 13 \times 4,096 14 \times 256 10 \times 16 13 \times 1

To get the value of the number in decimal, simply add the values up:

\displaystyle 53,248 + 3,584 + 160 + 13 = 57,005

Summary

Whew! What a whirlwind tour of number systems! This is all of the basic knowledge needed to understand what comes next. This covered the basics of number systems:

  • Each number system in base b uses b digits.
  • Each digit is in a position. Starting from the rightmost digit at 0 and increasing with each digit moving leftward.
  • Each digit in a number system has a value from 0 to b - 1 .
  • Each position, p , has a place value, or weight, which is b^{p} .
  • A digit with value, v , in a position, p , has a position value of v \times b^{p}
  • Number has a value of the sum of the digit values times their weights.

This also means that every number is actually a polynomial in disguise:

\displaystyle number = \sum^{places}_{i=0}{digit_{i} \times b^{i}}

The number of digits or places in a number indicates the order of the polynomial.

Logarithms

What is a logarithm? A logarithm is a special mathematical function that gives us a special number. Normally, it is written \log or \ln — the second means the base is \mathrm{e} (Euler’s constant). It is a special function that looks like this when graphed:

Graph ln

This graph conveys a few useful facts about the logarithm: The logarithm of a value is always less than the value. If an identity line of y = x was drawn on the above graph of y = \ln x , the two graphs would actually never intersect. \ln x is only zero at x = 1 . It could also be argued that \ln x doesn’t have a value at x = 0 . The heart of a logarithm lies in the mathematics defining it.

The base b logarithm gives the value of x in the equation:

\displaystyle b^x = n

The logarithm in base b of a number n is written:

\displaystyle x = \log_{b} n

In the case of JavaScript’s Math.log(), it is a natural logarithm. It will solve for x in this equation:

\displaystyle \mathrm{e}^x = n

The base 10 logarithm, \log_{10} , can be found by taking \ln n / \ln 10 . Generally, the base b logarithm can be found by \log_{b} n = \frac{\log n}{\log b} , where \log is a logarithm in any base available for you to use. Most likely it will be \ln , since it is so useful in math.

Now why is a logarithm useful?

Logarithms are useful for numerous reasons:

  • They can turn multiplication into addition. For instance, a^{x} \times a^{y} = a^{x+y} . Anyone can take the shortcut and mentally add up the numbers, but a computer can’t. However it can be programmed to use the properties a logarithms to change simplify parts of calculations without having to compute full values of repeated exponentiation until the final step. This also makes sure the numbers are smaller until the last step.
  • If one wants to encode a certain number of states as digits in a system, one would like to know how many digits that many states would take up:
    • In base 2, \log_{2} 2 = 1 . We can use 1 bit to represent two states. We can use 3 bits to represent 8 states (000, 001, 010, 011, 100, 101, 110, 111), \log_{2} 8 = 3 .
    • In base 10, \log_{10} 10 = 1 . This means we can use one decimal digit to represent 10 states — the range 0-9 encompasses ten digits. We can represent 1000 states with 3 digits (0-999), log_{10} 1000 = 3 .
    • Generally, digitsForNStates_{b} = \log_{b} N where N is the number of states to represent in base b .
  • In general, 1 + \log_{b} n gives us the length of digits used to represent a number n in the base b number system. For example 1 + \log_{2} 256 = 9 , which makes sense since we can represent 256 values from 0 through 255 with 8 bits. We cannot represent 256 with 8 bits, but we can with 9.
  • In physics and information theory, a logarithm is related to a value called the entropy of the system.

The primary reason why a logarithm is useful in this case is because it yields an exponent that encodes a lot about a number. Here’s a an example with a number in decimal:

\displaystyle 1,234 = 1 \times 10^{3} + 2 \times 10^{2} + 3 \times 10^{1} + 4 \times 10^{0}

This could also be written as:

\displaystyle 1.234 \times 10^{3}

In this case, 1.234 is the significand and 10^{3} is the magnitude.

Taking the base 10 log of 1,234 yields an interesting number. This number will be between 3 and 4. Why is it between 3 and 4? Well, \log_{10} 1,000 = 3 and \log_{10} 10,000 = 4 and this is somewhere between those two numbers. For instance, \log_{10} 1,000,000,000 = 9 for any number n between 1,000,000,000 and 10,000,000,000 (not including 10,000,000,000), the log_{10} n will be between 9 and 10 (not including 10).

This is also a valid relationship:

\displaystyle 1,234 = 10^{(3 + fraction)}

Now why is it 3 and a fraction? Taking the integer part of \log_{10}1,234 gives three. Taking 10^{fraction} gives 1.234. This means 10^{3} \times 10^{fraction} = 1,234 . So taking this and turning it into a bunch of math gives us everything anyone would ever want to know about the number and the different logarithms. It also means this information can be used to derive the magnitude of the number and get it’s most significant digits for any number.

Here is every identity and step used to formally derive these quantities:

  1. value = significand \times magnitude
  2. significand = 10^{significandExp}
  3. magnitude = 10^{magnitudeExp}
  4. value = 10^{significandExp} \times 10^{magnitudeExp}
  5. \log_{10} value = \log_{10} 10^{magnitudeExp} \times \log_{10} 10^{significandExp}
  6. \begin{array}[b]{l}      \log_{10} 10^{magnitudeExp} \times \log_{10} 10^{significandExp}\\      =\log_{10} 10^{(magnitudeExp + significandExp)}    \end{array}
  7. \log_{10} value = \log_{10} 10^{(magnitudeExp + significandExp)}
  8. exponent = \log_{10} value
  9. exponent = \log_{10} 10^{(magnitudeExp + significandExp)}
  10. exponent = magnitudeExp + significandExp
  11. value = 10^{exponent}
  12. value = 10^{magnitudeExp + significandExp}

By definition, magnitudeExp is an integer. The floor operation on the \log_{10} yields magnitudeExp . Also by definition, significandExp must be less than one or greater or equal to zero. But why must it be greater than or equal to zero and less than one? A decimal significand is going to be between 1 and 9. If the significand was zero, the whole number would be zero. If the significand was less than 1, then the magnitude was wrong. If the significand is greater than 9, the magnitude was wrong. These numbers map to values of 10^{x} , where 0 \leq x < 1 .

And of course, there’s the most straight forward way to get the significand:

  1. value = significand \times magnitude
  2. magnitude = 10^{magnitudeExp}
  3. value = significand \times 10^{magnitudeExp}
  4. significand = \frac{value}{10^{magnitudeExp}}

This method, just using exponentiation and division, is probably more accurate than using the significandExp in most cases, since finding the exponent using the Math.log function is just an approximation. Using it to extrapolate the significant digits can lead to errors, especially with very large or small numbers.

Back to the algorithm

The first few lines should be pretty obvious, given the explanation above:


var exponent = Math.log(val) / Math.log(10);
var magnitudeExp = Math.floor(exponent);

This figures out a few things:


var hasIntegerComponent = magnitudeExp >= 0;
var sign = (hasIntegerComponent) ? -1 : 1;
var adjustment = sign * (magnitudeExp % 3);
  • If the number has just a fractional component (magnitudeExp is less than zero) or if it has a whole number part, too (magnitudeExp is greater than zero).
  • The sign of the adjustment. If the number has a whole number part, the sign is negative and the adjustment is subtracted. If the the number is a fraction, the sign is positive and the adjustment is added.
  • The adjustment itself. This is how many digits away from a grouping of 3 digits the number would be given the magnitudeExp.

If scientific notation was all that was required, the significand and magnitudeExp would satisfy the problem. This algorithm is not actually converting to scientific notation. The algorithm should not show only one leading digit and a few decimal places. It should group digits into a maximum of three leading digits. Numbers between 000 – 999 will always be displayed before the suffix. It should be the equivalent of dividing the number by 1000, 1000000 and so forth (these numbers have counts of zeros which are multiples of three). This will group numbers into groups of 3 digits at most before the decimal place — this is where subtracting magnitudeExp % 3, (“magnitudeExp modulo 3”, the remainder of dividing by 3) comes from. That makes sure the abbreviations are only for place holders for the thousands, millions, billions, and so forth and never a placeholder for, say, ten thousand or a hundred million.

This code takes the adjustment into account when figuring out the significand to make sure that there is a group of at most 3 digits in the significand:


var significand = val / Math.pow(10, magnitudeExp + adjustment);

This code makes sure the suffix appended to the number matches up with the grouping of three most significant digits:


var index = (hasIntegerComponent) ? Math.floor(magnitudeExp / 3) : Math.ceil(magnitudeExp / 3);
return significand.toFixed(decimalPlaces) + symbols[''+index];

Depending on the logarithm of the number being positive or negative, we either take the floor or ceiling of magnitudeExp / 3, respectively. This makes sure we get the right integer for the index in the suffix lookup table.

That integer part of \frac{magnitude}{3} is actually the 1000, 1000000, 1000000000, and so forth in disguise. In fact Math.pow(10, Math.floor(magnitude / 3)) gives those values. This compresses the table of symbols so we don’t have something like the following, but rather the look-up table in the topmost code listing:


{
  ...
  -1: 'm', -2: 'm', -3: 'm',
   0:  '',  1:  '',  2:  '',
   3: 'k',  4: 'k',  5: 'k',
  ...
}

I suppose that the algorithm embodies a lot of knowledge about number systems, bases, and logarithms; but it’s very elegant. 😀 I haven’t tested how efficient it is compared to other methods, but, given the flexibility it has from the way it’s derived, it’s probably a decent trade-off.

Thoughts On Death

I’ve had an interesting understanding of death all my life. I’ve always understood life and death in a materialistic sense. We are physical beings and when we die we no longer exist, but return to earth from which we came. Of course their was a spiritual side to it, but it was practical — even if it was Christian or just plain biblical. What is a spirit? It’s our breath. In most ancient literature “spirit” is synonymous with wind, air, or a driving force  — like wind filling a sail. In Greek it is πνεῦμα (pneuma)[1][2], where we get the root for the word “pneumatic”. In Hebrew, it’s וּחַ (ruach)[3]. In this context, what is death? It is what happens when someone stops breathing — their breath leaves them. To me, breath is spirit and breathing is living.

It’s interesting that this view of life, death, and spirituality came out of a Christian upbringing. I was Seventh-day Adventist and our faith was strong enough to not need a concept of the “Stuff Of Unending Life”[4] that transmigrates from one reality to another that is so darling to other ways of believing. For us, “God” would simply resurrect someone by recreating that person out of the ground and breathing life into them. This semi-materialistic view[5][6] shaped a lot of my beliefs — especially after I left my church and became an atheistic secular humanist. It also didn’t require a giant change in beliefs about spirits and souls.

A soul? What is a soul? To me, it’s a breathing, living being. One does not posses a soul, one is a soul. In Hebrew נֶ֣פֶשׁ  (nephesh)[7] is a being. That Hebrew word is used to refer to a being, a creature, and life. I really like the Hebrew word for it, even if the concept is muddied with other notions from Christianity (evidenced in the way most Bibles translate it). The Greeks also had word for it: ψυχή (psuche)[8]. Of course this word should look familiar, it’s the root of the word “psyche”. The greek word has more of the connotations of the English words “soul” and “spirit”, but it also means “life” and “self”.

Self, consciousness, is an emergent phenomena of our being continuously taking in energy from the environment and pushing itself out of equilibrium. It does an elaborate balancing act to keep itself in homeostasis. The end result is this amazingly intricate machine[9]. We’re alive as long as our complex organs and organ systems are working well and we’re breathing. But what makes someone special? It’s the embodied information of their genetics, interactions with their environment, their elaborate biochemistry, and labyrinthine neuronal connections. Our sense and experience of self is most likely so pronounced because we have several orders of magnitude more connections between neurons within the neurocortex than to neurons going to and from our senses. There’s an astounding amount of information in all of those connections. When we interact with someone each of us triggers different parts. Each of us experiences and interacts with others in a different way.

As long as a soul’s intricate inertial dance of life perpetuates itself, this amazing ability for us to connect with that unique being is possible. Our own physical being interacts with their systems in several intricate and deeply physical ways. I don’t believe the magic of a human being’s existence is diminished at all by this view. I believe it makes it more mind bogglingly unbelievable once we see the true complexity and wonder of what a human actually is. It also creates a physical basis for understanding death and what happens to someone once they die (and also how we connect on so many physical levels while they’re alive).

Before I share what I believe death entails, I must share a few prerequisites. Physics has led us to figure out a few things:

  • The law of conservation of energy; the law that energy can neither be created nor destroyed, just transformed from one form to another.
  • The equivalence principle; the law which states mass and energy can be converted from one form to the other.
  • The conservation of information at the quantum level[10]; transformations are unitary — this means that if you know the dynamics of the system you can recover the inputs from the outputted information.
  • The second law of thermodynamics; Entropy never decreases[11][12].

I would say that entropy has a relation to information like work has a relation to energy — except it’s more nuanced. If a physical process transforms one bit of information into another bit of information and the process is reversible then the entropy of the process is zero and it can spontaneously oscillate between those two states forever. If the physical process is irreversible (or takes energy to reverse it) then entropy increases. In a way entropy is like meta-information about a physical process containing the instructions of how it got to the current state[13] — like a knot being tied through space-time.

As far as the non-emergent properties of a person go, their body does indeed return to the earth. We are all made of the same stuff those who came before us were made of. We breathe the same air. Our matter continues to be a part of this world.

Every person who has ever lived lives on in the entropy[14] of the universe and their effect upon our intellect, affect, and physical being. This world would be a very different place if it weren’t for each and every person who lived and the pattern they weaved into the fabric of our reality. The world becomes a different place when each of us weaves our own patterns of entropy into the fabric of reality. When people die, this pattern is still there encoded in our universe’s entropy. Unfortunately for us, we can’t directly perceive these patterns; we only have our memories of a person and the effects they had on us. Fortunately, communities can come together and celebrate someone’s life. Each participant sharing more of a picture of the one being remembered and creating a more complete picture of that persons interactions and influences.

It is up to us to decide what happens to the memory of the deceased. Each of us has our role models and some of them are dead, but we allow ourselves to embody the behaviors and interactions of those who are dead. Unfortunately, sometimes the deceased is a counterexample of how to live. Whose effects are we enhancing?

Notes

  1. <http://www.perseus.tufts.edu/hopper/text?doc=Perseus%3Atext%3A1999.04.0058%3Aentry%3Dpneu%3Dma>
  2. <http://www.perseus.tufts.edu/hopper/text?doc=Perseus%3Atext%3A1999.04.0057%3Aentry%3Dpneu%3Dma>
  3. See <http://biblesuite.com/hebrew/ruach_7307.htm> and related וְר֣וּחַ (veruach) waw-conjuctive form <http://biblesuite.com/hebrew/veruach_7307.htm> and <http://en.wikipedia.org/wiki/Prefixes_in_Hebrew#Conjunctions>.
  4. Afterlife is a cute game where you get to play god and decide where souls go <http://en.wikipedia.org/wiki/Afterlife_(video_game)>.
  5. <http://ssnet.org/qrtrly/eng/99b/less04.html>
  6. <http://www.sdanet.org/atissue/books/27/27-07.htm>
  7. http://biblesuite.com/hebrew/nefesh_5315.htm
  8. See <http://www.perseus.tufts.edu/hopper/text?doc=Perseus%3Atext%3A1999.04.0057%3Aentry%3Dyuxh%2F>. Coincidentally, it is the present subjunctive third person singular form of ψύχω <http://www.perseus.tufts.edu/hopper/text?doc=Perseus%3Atext%3A1999.04.0057%3Aentry%3Dyu%2Fxw> (eg, ἐάν ψυχή, “if he breathes”).
  9. <http://jap.physiology.org/content/104/6/1844>
  10. <http://motls.blogspot.com/2005/07/hawking-and-unitarity.html>
  11. <http://en.wikipedia.org/wiki/Second_law_of_thermodynamics>
  12. <http://en.wikipedia.org/wiki/Entropy_production>
  13. In fact, I’m pretty sure one could derive this from Kolmogorov complexity and only using vector clocks or matrix clocks instead of local time and Lorentz transformations .
  14. Maybe there should be another term for this, since it’s the length of the algorithm (or sequence of operators) which can be use to calculate entropy and not pure entropy — but maybe they really are the same thing?

θάνατοι: In Memoriam

Name Date & Location Born Date & Location Deceased
Eni Korbeci March 19, 1980 Tirana, Albania January 12, 2002 Edmond, Oklahoma
Augustine “Auggie” Ariza March 2, 1925 June 28, 2005 Thousand Oaks, California
Brandon M. Fenton June 21, 1983 Grand Island, Nebraska June 25, 2009 Grand Island, Nebraska
Brandon Lee Glovatsky April 12, 1985 Grassy Butte, North Dakota June 9, 2010 Portland, Oregon
Eleanor Montané Ariza June 28, 1925 Santa Maria, Vera Cruz, Mexico January 5, 2011 Loma Linda, California
Joseph Paul Bingman November 21, 1948 Portland, Oregon April 3, 2011 Portland, Oregon
Tomás Pitagoras Gouverneur December 14, 1978 Berkeley, California March 13, 2011 Corvallis, Oregon
Kathleen Bosibori Sagini February 10, 1988 Lansing, Michigan July 26, 2012 Edmond, Oklahoma
Zachary Konowalchuk July 21, 1988 Florida October 8, 2012 Newport, Oregon
Kent Ryan Hall 1950 Fairbury, Nebraska February 20, 2013 Lincoln, Nebraska
Igal Koshevoy December 13, 1975 Moscow, USSR April 9, 2013 Portland, Oregon
Yan Forrest Hendersen January 29, 1958 January 22, 2014 Portland, Oregon

Moral Imperatives and God — or — “Why I Sometimes Want to Place You in Harm’s Way.”

C. S. Lewis would have thought the moral sense referred to in this meme was one of the strongest evidences of God’s existence.

One can, like C.S. Lewis, posit this evolved moral sense is a signifier of God’s existence. However, this is misusing a perceived correlation as a causation. Even this error hinges on interesting factors for it to even be made.

Tribal societies or hunter gatherers had a great incentive to treat their own members nicely and have fond feelings for one another. The biological mechanism backing this is mediated by oxytocin.1 One would be tempted to say that this extends to all members of our society. Sadly, as evidenced by people’s tendencies through history and by modern biological research, oxytocin plays a dual role.

An increase in oxytocin tends to produce fond feelings only for those that are part of your tribe. Those who share your culture or share a more similar appearance (a phenotype symbolizing a desirable genotype, suitable for reproduction). The other side of this, is an increase in oxytocin also increases the instinct to distrust others who do not share a simliar phenotype. It breeds xenophobic tendencies in people.2

Many other regulators of behaviors exist, but it turns out that this is a fairly primary motivator of a healthy and happy group of people:

As it turns out, the needs that are most linked with everyday satisfaction are interpersonal ones, such as love and respect. Our troubles, conversely, relate most to lack of esteem, lack of freedom, and lack of nourishment. Only when we look back on the quality of our lives thus far do basic needs become significant indicators for well-being.3

The other regulators basically cluster around having real and felt needs met. A lot of these needs are met through society and access to the proper resources. If these needs aren’t met for prolonged periods of time, the organizational unit (either the individual or group) will exhibit behaviors associated with self preservation and increased stress. This means acting more out of self interest than out of respect for others or any other moral imperative (theft, war, violence, distrust, etc. are all game here).

I think it is safe to say human biology doesn’t change much over time unless there is an adaptation to some external change or new constraint in the environment — in other words evolution. Even then, biology has noted the similarities of certain basic structures and chemistries of living organisms. I think it’s safe to extrapolate our modern biology back some 10,000 years (since that is a fairly short time scale, evolutionarily speaking). Under this extrapolation using the young earth hypothesis as a postulate, it is a logical conclusion that our biology was the same or very similar at creation. Which implies that the only way for our biology to have changed so suddenly is through an act of God.

If this was by God’s design, who said he intended us to love our neighbor and by extension all people, there is a severe disconnect between the biology we were “created” with and how he intended us to behave. If this was the case then the garden of eden would not have been perfect. As soon as groups split off there would be fighting in a perfect world and not as a result of “the fall in the garden”. There was no need of an apple for this to happen — and it is doubtful that an apple could change a human’s biology that much.

If God intended to rescue a race that was sinful, because of it’s own choice, why would he choose to change their biology intentionally to make sure they were inherently prone to distrust? A change like this would lessen the efficacy of free will to choose a moral imperative over natural tendencies. Also, why would this change in biology persist past the time of what Christianity defines as the final sacrifice for all time, meant to atone for all sin?

A contradiction such as this is most readily remedied by looking to evolutionary processes, where such tendencies would tend to promote group cohesion and the desire to protect itself when encountering a foreign group, thus ensuring its survival and the ability to reproduce and grow. As a result of continuing growth and constant interaction and breeding with differing phenotypes the level of hostility toward different groups has decreased over time. For more on this, please see Steven Pinker’s excellent TED talk on the myth of violence. 4

Through this process of increased interaction with foreign groups, moral imperatives regarding the treatment of people from other cultures arose. These moral imperatives are abstract high level constructs people create for themselves outside of and, sometimes, in opposition to their natural tendencies. Moral imperatives are not natural and take energy and practice to maintain. They are only good so long as reason can beat the lizard brain. As studies with Tibetan monks have shown, it takes time to develop strong pathways between the prefrontal cortex and the rest of the brain (the prefrontal cortex being the area that is understood to mediate higher level planning, decision making, and reason).5

This ability to use reason over natural tendencies is willpower. Willpower is the basic determining factor if someone will choose to act on a higher moral imperative or if they will go with their own path of least resistance. It has recently been observed that willpower is limited. This ability quickly depletes if the underlying biological mechanisms are no longer being fully supported. Willpower is entirely dependent on all of the biology supporting the brain and requires one’s body to be well nourished and in good health.6

Where scarcity is not a daily concern, it is easy to see how humans are becoming better people. We are able to make better decisions because we are not in survival mode. Without scarcity and with plenty of close social interaction, it is easy to think moral imperatives are natural. It however takes a “leap of faith” to believe they signify God’s existence.

  1. http://en.wikipedia.org/wiki/Oxytocin
  2. http://www.nytimes.com/2011/01/11/science/11hormone.html
  3. http://www.theatlantic.com/health/archive/2011/08/maslow-20-a-new-and-improved-recipe-for-happiness/243486/
  4. http://www.ted.com/talks/steven_pinker_on_the_myth_of_violence.html
  5. http://www.news.wisc.edu/13890
  6. http://www.nytimes.com/2011/08/21/magazine/do-you-suffer-from-decision-fatigue.html?pagewanted=all

What to Look for in PHP 5.4.0

PHP 5.4.0 will arrive soon. The PHP team is working to bring to some very nice presents to PHP developers. In the previous release of 5.3.0 they had added a few language changes. This version is no different. Some of the other changes include the ability to use DTrace for watching PHP apps in BSD variants (there is a loadable kernel module for Linux folks). This release features many speed and security improvements along with some phasing out of older language features (Y2K compliance is no longer optional). The mysql extensions for ext/mysql, mysqli, and pdo now use the MySql native driver (mysqlnd). This release also improves support for multibyte strings.

Built-in Development Server

In the past, newcomers to PHP needed to set up a server. There was no built in server like a few other languages/web frameworks already had. If developing on *nix, a server needed to be set up with the right modules and the the files to tested needed to be copied over to the document root. Now, you can just run PHP with some options to get a server:

$ php -S localhost:1337

It runs in the current directory, using index.php or index.html as the default file to serve. A different document root can be specified as either an absolute or relative path:

$ php -S localhost:1337 -t /path/to/docroot

The server will log requests directly to the console. Interestingly, this server will not serve your static files unless your script return false. Existing frameworks will need to be modified to add in functionality that is commonly in rewrite rules. This is really all that is needed:

// If we're using the built-in server, route resources
if (php_sapi_name() == 'cli-server') {
  /*
   * If the request is for one of these image types, return false.
   * It will serve up the requested file.
   */
  if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"]))
    return false;
}
// Process the rest of your script

One of the inconveniences of the server is lack of support for SSL. Granted, it is meant for development purposes only. However, some projects I’ve worked on required testing with SSL. Perhaps there will be demand for this once it’s out there.

An Overview of Traits

Traits are bits of code that other objects can use. Traits allow composing objects and they promote code reuse. The Self programming language, one of the precursors to the JavaScript language, introduced them. JavaScript, strangely, does not directly implement traits; instead it allows one to extend objects directly with other objects.

In PHP (and other languages), traits cannot be instantiated, only used to compose other objects. Traits do not imply inheritance, they just add methods to classes. They can be used with inheritance and interfaces. Traits could be used as standard implementations of interfaces, then one could easily compose classes that comply with certain interfaces.

Here is an example, demonstrating a simple use of traits:

<?php
/**
 * Define a trait called runner
 */
trait runner {
  // Does all the work
  public function run() {
    echo "Run, ".$this->name().", run!\n";
  }
  // Gets the name of the runner
  // Must be implemented by the class using the trait.
  abstract public function name();
}
/**
 * Define a class that uses the runner trait
 */
class runningPerson {
  // Use the runner trait
  use runner;
  // Used to store the person's name
  protected $name; // Constructor, assigns a name to the person
  public function __construct($name) {
    $this->name = $name;
  }
  // Retrieves the name of the person, required by runner trait
  public function name() {
    return $this->name;
  }
}
$gump = new runningPerson("Forrest");
$gump->run();

When a class implements a method that is also defined in a trait it uses, the method in the trait takes precedence and overrides the class method. If two traits implement the same method, the conflict needs to be resolved using the insteadof keyword, or given a new signature (only visibility and name can be changed) using the as keyword.

<?php
/**
 * Define traits with conflicting function names
 */
trait A {
  public function do_something(){
    echo "In A::do_something():\n";
    for($i = 0; $i < 10; $i++) {
      echo $i." ";
    }
    echo "\n";
  }
}
trait B {
  public function do_something(){
    echo "In B::do_something():\n";
    echo "Something else.\n";
  }
}
/**
 * Define a class using both traits
 * Prefers one over the other
 */
class ExampleA {
  use A, B {
    B::do_something insteadof A;
  }
}
/**
 * Define a class using both traits
 * Renames a function to use both
 */
class ExampleB {
  use A, B {
    A::do_something insteadof B;
    B::do_something as something_else;
  }
}

//Run examples
$testA = new ExampleA();
$testB = new ExampleB();
$testA->do_something();
$testB->do_something();
$testB->something_else();

More examples reside at the current PHP documentation for traits. If that documentation is lacking in substance, Wikipedia’s article on traits links plenty of background info.

Changes to Anonymous Functions

In PHP 5.3.x, working with anonymous functions needed a work around when stored in an array. The function needed to be stored in a temporary variable before it could be called. For instance:

$functions = array();
// assign an anonymous function to an array element
$functions['anonymous'] = function () {
  echo "Hello, the parser needs to make up a name for me...\n";
};
// to call it you had to do this:
$temp = $functions['anonymous'];
$temp();

Now, anonymous functions stored in an array can be called directly without first storing them in a temporary variable:

// assume $functions[] is still around.
$functions['anonymous']();

Closures in Classes

Closures defined inside of a class are automatically early bound to the $this variable. If a class method returns a closure, it retains access to the original class that defined it (along with all the public member properties and methods) no matter where it is passed. If assigned to a member property and called as a method, PHP issues a warning if it was called directly. If called as a local variable or by calling the Closure::__invoke() method (which it inherits), PHP issues no warning.

<?php
/**
 * Class that generates closures that reference a public method.
 */
class ClosureTest {
  private $value;

  public function setValue($value) {
    $this->value = $value;
  }

  public function getValue() {
    return $this->value;
  }

  public function getCallback() {
    return function() {
      return $this->getValue();
    };
  }
}

/**
 * Create a class that calls a closure.
 */
class ClosureCaller {
  private $callback;

  public function setCallback($callback) {
    $this->callback = $callback;
  }

  public function doSomething() {
    // Since this is a member variable, call Closure::__invoke().
    echo $this->callback->__invoke() . "\n";
  }
}

// Set up a class to generate closures that reference itself
$test = new ClosureTest();
$test->setValue(42);
$closure = $test->getCallback();
echo $closure() . "\n";

// Test calling a closure from another class
$testCaller = new ClosureCaller();
$testCaller->setCallback($test->getCallback());
$testCaller->doSomething();

Closures allow changing what object scope $this is bound to by calling the bindTo() method and passing in the new object to use as $this.

Currently, no consensus exists around letting closures bound to an object access the private and protected methods of that class. Additionally, PHP still needs to iron out the details around binding closures to static classes. One can find more details about closures, $this, and Closure::bindTo() at https://wiki.php.net/rfc/closures/object-extension

Outlook

There are a lot of established projects that may not immediately start taking advantage of these features, unless the community sees an obvious benefit to drastically changing their projects. When PHP 5.3.0 was released with namespace support and anonymous functions, new frameworks sprung up like Laravel (anonymous functions), FLOW3 (namespaces), Lithium (namespaces), and Symfony2 (namespaces). After PHP 5.4.0 is released, I’m sure new frameworks (or new versions, like Symfony2 vs. Symfony) will spring up around using traits to compose functionality and using the new $this functionality in closures defined in classes. The built in server definitely has some potential for making it easier for developers to debug their apps. It’s just a matter of time before frameworks start taking advantage of it.

Reference

PHP 5.4.0 Release Candidate 2 News

Installing Darwin 8.0.1 Using QEMU

When Darwin was first ported over to the x86 architecture, I thought I could get it running inside of a VM. I was stopped because QEMU was not quite ready for it. I tried it twice over a period of at least 4 years. I just tried it again and it worked. During the time since then, a patch was made to QEMU to make it work properly — which also got absorbed into releases after v0.10. Another big problem was figuring out the exact install process and that one could not really just boot it off the CD image and expect it to work. There is a specific process. I’m pretty altruistic, so I’ve documented it here.

First things first, grab the Darwin ISO image from Apple’s site. While you’re at it you can peruse the release notes. When it’s done, unzip it into a directory.

If you haven’t installed QEMU, please do so. To install Darwin you need to create a disk file to use with QEMU. For this, type: qemu-img create -f vmdk darwin-disk.vmdk 3G. The release notes recommend 3GB as a minimum hard disk size, but feel free to make it larger if you desire.

When you are ready to start installing Darwin, type: qemu -hda darwin-disk.vmdk -cdrom darwinx86-801.iso -boot d -m 1024. This will start QEMU and boot from the CD image. It will churn its cogs and then ask you which disk you want to install it on. If you’ve followed my directions, there will only be one disk available to choose from.

Next, it will ask you to partition the drive. Since this is a new disk, make it auto-partition. When it asks you for a volume name, it will fail. Start the machine over again. Choose to use the existing partitions, then continue with installation. When it asks for which partition to use, it should show you two options. Type in the second option. Now, wait for a really long time while it unpacks all of the files.

Once it’s done, it will ask you for a root password. Enter it and confirm it. Then it will ask if you want to create a user, start a shell or restart. Create a user, then restart. It should power the machine down.

When you want to run the disk image type: qemu -hda darwinx86-801.vmdk -cdrom darwinx86-801.iso -m 1024. This will start the machine from the disk image we just installed Darwin and the installation CD will be available to mount, too. The Network should be up and running. If that doesn’t work right, and it just sits there spinning, try re-installing it. I had to do that. The second install to the image proved flawless.

Interesting associated reading:

  1. Installing Darwin in a VM <http://althenia.net/wiki/darwin> — I found this after I published this. The instructions given for upgrading the compiler are interesting, also has more detailed steps to follow.
  2. Help with mounting disk images, especially .dmg files <http://www.puredarwin.org/developers/diskimages>