Wednesday, July 30, 2008

Symmetry

Just wanted to save a nice expression I read about yesterday:

  1. 1 << n >> 1  


The value returned is 2^n/2, or 2^(n-1). I just thought it looked lovely :)

Thursday, July 17, 2008

On 0.999...

While trying to explain to someone that 0.999... aka 0.(9) is equal to 1 - not almost equal, but equal - one of the "arguments" he used was that recurring decimals are not really numbers, you can't do mathematical operations with them. While the idea is mind-boggling - 0.(1) is 1/9 which is a rational number - I thought of a proof that doesn't use recurring decimals: using base 9.

0.(1)10 = (0.1)9

(0.0)9 + (0.1)9 = (0.1)9
(0.1)9 + (0.1)9 = (0.2)9
(0.2)9 + (0.1)9 = (0.3)9
(0.3)9 + (0.1)9 = (0.4)9
(0.4)9 + (0.1)9 = (0.5)9
(0.5)9 + (0.1)9 = (0.6)9
(0.6)9 + (0.1)9 = (0.7)9
(0.7)9 + (0.1)9 = (0.8)9

Finally,

(0.8)9 + (0.1)9 = ?

Well... in any numeration base, when you finished the digits you restart at 0 and add 1 to the next order, so:

(0.8)9 + (0.1)9 = (1.0)9

Recasting the above in base 10, we have

0.(8) + 0.(1) = 1.0

However, in base 10 we still have a digit left, so it is equally valid to write

0.(8) + 0.(1) = 0.(9)

just as it is valid to write 0.(7) + 0.(1) = 0.(8).

Given that the left side is identical, it follows that 1 = 0.(9). QED.

Tuesday, July 15, 2008

You want horror? Here's horror!

Jeff Atwood latest article, Maybe Normalizing Isn't Normal is a real beauty. If you like idiots, that is. (Cue Fabian Pascal.)

This is by far the worst Jeff has ever wrote. I cannot believe that someone who claims to offer advice to programmers can say anything but "NEVER denormalize". For fuck's sake, educate yourself before you discuss a subject as anything but "hey, I heard somewhere that shooting yourself in the foot is a good idea, what do you think?". (Maybe I should have realized how smart he is when he associated with Joel "exceptions are evil" Spolsky.)

Let me quote a comment that explains the problem very succinctly:

Speaking from long experience, if you don't normalize, you will have duplicates. If you don't have data constraints, you will have invalid data. If you don't have database relational integrity, you will have orphan "child" records, etc. Everybody says "we rely on the application to maintain that", and it never, never does.

A. Lloyd Flanagan on July 15, 2008 06:34 AM


Jeff, stay away from databases. If your programming skills are similar to your logical skills, stay away from programming too...


Oh... let me start checking the articles he uses to support his idea.

First article

Our feeling is that this is ultimately far more scalable than black-box clustering.

Wow. Imagine that. Their feeling. Definitely a good argument.

The third is already ridiculous: they denormalize the database for "performance" and then discover the reason normalization exists: they lose consistency and they have to write a lot of code (that, one would assume, will take a lot of time) to fix that:

a lot of the flickr code infrastructure deals with ensuring data is consistent and well balanced and finding and repairing it when it's not

Note that: not if, when.

Good job. Yeah, denormalization ftw!

I'll stop here. I believe I made my point.

Thursday, July 10, 2008

Static virtual in C#

Argh, this is annoying. I need a Handler type with a virtual string GetProduct() method so that derived types "know" what product they are for. However, I don't want to have to create an instance of the actual class every time I need to ask for its product... I want it to be a virtual method. Too bad - C# doesn't allow virtual static methods.

What I ended up with was this: each class derived from Handler has a read-only static string property Product, each of them returning the product that class can handle. In the method where I need that product (when populating a drop-down for example) I use reflection to get the value of the property:

  1. if (handlerType.BaseType != typeof (Handler))  
  2.   return;  
  3.   
  4. var product = (string) handlerType.GetProperty("Product").GetValue(nullnull);  


I'm not really happy with this - I miss Delphi's "class" type which allowed a much more elegant solution. I'll edit this if I find something I like better.

Edit: Heh, I found a solution I like better: attributes. I define the following class:

  1. [AttributeUsage(AttributeTargets.Class)]  
  2. internal class ProductInfo: Attribute  
  3. {  
  4.   public string Product { getprivate set; }  
  5.   
  6.   public ProductInfo(string product)  
  7.   {  
  8.     Product = product;  
  9.   }  
  10. }  


Now I can decorate the handlers with a
[ProductInfo("xxx")]
attribute and extract the product name with

  1. var attributes = handlerType.GetCustomAttributes(typeof (ProductInfo), true);  
  2. if (attributes.Length == 0)  
  3.   return;  
  4.   
  5. var product = ((ProductInfo) attributes[0]).Product;  


First time I create a custom attribute. Helpful and elegant, so it won't be the last.

Sunday, July 06, 2008

More on probabilities

This is in regards to the often-repeated saying "improbable events happen all the time" (even Dembski says that... which only serves to show that even people on "my" side can be idiots).

I just read something that was so apropos of this:

Heroic Law of Probabilities: One-in-a-million chances happen 9/10

(Look for it on this page, it makes more sense in context.)

That is just too great for words, I will keep quoting that "law" every time the subject comes up. I have to read that story, even though I'm not a fan of Marvel stuff. (Plus, the main character has a Romanian name. Yay :P)

Saturday, July 05, 2008

Speculation

I believe that the mind and the brain are distinct things, while the mind and the soul are the same. However, memory is pretty clearly located inside the brain... which means that we lose our memory when we die. That would imply that the purpose of our life here is only to educate our soul... to transform us into better people. It also means that we do not know who we were or what we did - which would be good, because otherwise a lot (most? all?) of us would have to somehow come to terms with the shame of what we did. Not really compatible with heavenly bliss.

No basis in anything, really, just some idle speculation.