Saturday, December 30, 2017

Fan-fiction recommendations

I posted this to HN and I realized it might be useful for other people. If at least one person who wasn't aware of fan-fiction discovers it as a result, this has served its purpose.

For Buffy fans, TTH has a huge number of stories, both in-universe and crossovers with other worlds. Speakertocustomers, Becuzitswrong, Cordyfan, DianeCastle and Hotpoint are a few of my favorite authors. Hotpoint's crossover between X-COM and SG-1 is absolutely amazing.

For other worlds, FF, SB, SV and AO3 are the most well-known sites for fanfiction. One of my favorite stories is Taylor Varga.

What I normally do is I download the stories in the .mobi format - TTH and AO3 have that feature in the site, and for the others you can use the FanFicFare plugin for Calibre - and then upload them to my Kindle. This allows me to read in bed, which is bad for my sleep but I like it :)

Friday, March 31, 2017

Don't compare floats

Floating-point numbers are tricky; one of the first things a programmer needs to remember when working with them is to never check floats for equality. (I believe ReSharper warns you if you do that; I don't know if plain Visual Studio does because I never use it without ReSharper.)

If precise representations of decimal numbers is needed, like when manipulating currencies, use decimals instead (if using C#); if a similar primitive type does not exist in your language, write a separate package / module / library to "fake it" by using integer values and scale them down by two or four digits, depending on your needs. (For example, the number 123456 can represent either 1,234.56 or 12.3456, depending on your application.)

Here's a simple code example to show the difference between floats and decimals in C#:

float f1 = 0.1f;
float f2 = f1 * 10.0f;

float f3 = 0.0f;
for (var i = 1; i <= 10; i++)
    f3 += f1;

Console.WriteLine(f2 == f3);

decimal d1 = 0.1m;
decimal d2 = d1 * 10.0m;

decimal d3 = 0.0m;
for (var i = 1; i <= 10; i++)
    d3 += d1;

Console.WriteLine(d2 == d3);

Running this in LinqPad prints out:


(A peculiarity of the decimal type in C# is that it keeps track of the number of decimal places that have been used, hence the 1.00 vs 1.0 in the second part of the results.)

Wednesday, March 15, 2017

Evaluating an expression

Leaving a note to myself - a simple algorithm for evaluating expressions, no unary operators or parentheses.
phase 1 (tokenizer)

'+' => op1, add
'-' => op1, sub
'*' => op2, mul
'/' => op2, div
\d+ => number, value

phase 2 (evaluator)

tuple = first (number, op2, number)
while tuple:
  replace tuple with result of op2

tuple = first (number, op1, number)
while tuple:
  replace tuple with result of op1

there should be a single item left, a number; return its value