Posts

Showing posts from March, 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 float s and decimal s 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); Console.WriteLine(f3); Consol...

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