Saturday, May 31, 2008

Degrees and Radians

When you were learning trigonometry, you probably measured angles in degrees. But in calculus, angles are measured in radians (search for "degrees vs. radians" on Google for the reasons). Being able to convert between these two units might be handy. PHP provides two functions for this purpose, but no such luck in ASP. As usual, we're going to write our own.


Radians is heavily based on my favorite number, pi. We're going to need this number in our calculations, so make sure to define a constant for it.


Const M_PI = 3.14159265358979323846


And now the functions themselves...


  1. function deg2rad(x)
  2.     deg2rad = x * M_PI / 180
  3. end function
  4. function rad2deg(x)
  5.     rad2deg = x * 180 / M_PI
  6. end function

View this code on Snipplr

Saturday, May 24, 2008

Euclid's Algorithm

Euclid's algorithm is one of the oldest, known by ancient Greeks like Aristotle. You might remember it from elementary school when you had to find things like greatest common factor/divisor and least common multiple. Greatest common factor/divisor allowed you to reduce fractions like 4/12 to 1/3. Least common multiple allowed you to add and subtract fractions that had different denominators.


There is more than one way to implement this algorithm. The original involved iterative subtraction. This was later improved upon with iterative modulo division. Another method, the one I'll be using here, is recursion.


ASP

  1. function gcd(byVal a, byVal b)
  2.     a = abs(a)
  3.     b = abs(b)
  4.     if a = 0 then
  5.         gcd = b
  6.     elseif b = 0 then
  7.         gcd = a
  8.     elseif a > b then
  9.         gcd = gcd(b, a mod b)
  10.     else
  11.         gcd = gcd(a, b mod a)
  12.     end if
  13. end function
  14. function lcm(byVal a, byVal b)
  15.     a = abs(a)
  16.     b = abs(b)
  17.     if a > b then
  18.         lcm = (b / gcd(a, b)) * a
  19.     else
  20.         lcm = (a / gcd(a, b)) * b
  21.     end if
  22. end function

PHP

  1. function gcd($a, $b)
  2. {
  3.     $a = abs($a);
  4.     $b = abs($b);
  5.     if ($a == 0)
  6.     {
  7.         return $b;
  8.     }
  9.     elseif ($b == 0)
  10.     {
  11.         return $a;
  12.     }
  13.     elseif ($a > $b)
  14.     {
  15.         return gcd($b, $a % $b);
  16.     }
  17.     else
  18.     {
  19.         return gcd($a, $b % $a);
  20.     }
  21. }
  22. function lcm($a, $b)
  23. {
  24.     $a = abs($a);
  25.     $b = abs($b);
  26.     if ($a > $b)
  27.     {
  28.         return ($b / gcd($a, $b)) * $a;
  29.     }
  30.     else
  31.     {
  32.         return ($a / gcd($a, $b)) * $b;
  33.     }
  34. }

Euclid's algorithm is not always the fastest, but it is the simplest. A better algorithm was devised by 20th century mathematician Dick Lehmer. Another 20th century mathematician, Josef Stein, devised a specialized algorithm for computers which uses bit shifting. Note, however, that the performance improvement of these more modern algorithms varies depending on the CPU and size of the numbers involved. In some cases, Euclid is actually faster.


Monday, May 19, 2008

Update

I've updated my Leap Years article with more elegant PHP solutions thanks to Jim Mayes.

I discovered his feedback via a web site called Snipplr, where I've been uploading all the code examples from this blog. All the previous code examples are still available here on the blog, but I'm considering posting future code examples on Snipplr only and linking to them from the blog, because it takes a ridiculous amount of time to format the code examples to display nicely on the blog. I think it will depend on the length of the example; I don't mind reformatting short examples, but I've got some longer ones that I'd rather not.

Anyways, you can view all my code snippets from my Snipplr profile. There are also some bonus things in there which are outside the scope of this blog (eg. Windows registry tweaks). I've also added this link to the sidebar so that it will be easy to find in the future when this post disappears off the front page.

Saturday, May 17, 2008

Ceiling and Floor

In mathematics, there are two elementary special functions called ceiling() and floor() which allow us to round up or down, respectively, to the nearest whole number. These functions exist natively in PHP, but not in ASP. They are handy in situations where you want to force a number like 15.8 to round down to 15, but the round() function rounds it up to 16 according to the standard rules for rounding.


ASP

  1. function floor(x)
  2.     dim temp
  3.     temp = round(x)
  4.     if temp > x then
  5.         temp = temp - 1
  6.     end if
  7.     floor = temp
  8. end function
  9. function ceil(x)
  10.     dim temp
  11.     temp = round(x)
  12.     if temp < x then
  13.         temp = temp + 1
  14.     end if
  15.     ceil = temp
  16. end function

Saturday, May 10, 2008

Tractive Effort

This week is another short, fun snippet of code. We're going to calculate the tractive effort of a steam locomotive. Useless to most people, but I've got a large project where I'll be using it myself.


ASP

  1. function TractiveEffort(cylinderDiameter, stroke, pressure, wheelDiameter)
  2.     TractiveEffort = cylinderDiameter ^ 2 * stroke * pressure * 0.85 / wheelDiameter
  3. end function

PHP

  1. function $tractiveEffort($cylinderDiameter, $stroke, $pressure, $wheelDiameter)
  2. {
  3.     return $cylinderDiameter ^ 2 * $stroke * $pressure * 0.85 / $wheelDiameter;
  4. }

It should be noted that the formula only applies to non-compound steam locomotives. Locomotives with multiple cylinders are more complex.

Saturday, May 3, 2008

Base Conversion

Most of the modern world uses a base-10 number system. It's the easiest to work with. However, when it comes to computer systems, we use a few different number systems too. Base-2, or binary, is the basis of data storage and transmission; a bit is either on or off. We also use Base-8, or octal; each byte contains 8 bits. Last but not least is Base-16, or hexadecimal.


Under some circumstances, we might need to convert between these different number systems. ASP provides a pathetic two functions for this purpose: hex(), which converts from base-10 (decimal) to base-16 (hexadecimal), and oct() which converts from decimal to octal. PHP provides equivalent functions dechex() and decoct(), as well as additional functions decbin() for converting from decimal to binary, bindec() for converting from binary to decimal, hexdec() for converting from hexadecimal to decimal, and octdec() for converting from octal to decimal.


It would be nice to extend ASP to support these four additional functions. When I started doing that, I noticed a pattern in the formulae. All the functions that converted from decimal were fairly similar, and all the functions that converted to decimal were also fairly similar. So I abstracted the calculations out into their own functions and simplified my code.


ASP

  1. ' Convert from binary to decimal.
  2. function bindec(bin)
  3.     bindec = toDecimal(bin, 2)
  4. end function
  5. ' Convert from decimal to binary
  6. function decbin(dec)
  7.     decbin = fromDecimal(dec, 2)
  8. end function
  9. ' Convert from decimal to hexadecimal.
  10. function dechex(dec)
  11.     ' Assume that built-in hex() function is faster.
  12.     dechex = hex(dec)
  13. end function
  14. ' Convert from decimal to octal.
  15. function decoct(dec)
  16.     ' Assume that built-in oct() function is faster.
  17.     decoct = oct(dec)
  18. end function
  19. ' Convert from hexadecimal to decimal.
  20. function hexdec(hex)
  21.     hexdec = toDecimal(hex, 16)
  22. end function
  23. ' Convert from octal to decimal.
  24. function octdec(oct)
  25.     octdec = toDecimal(oct, 8)
  26. end function

Before we get into the underlying fromDecimal() and toDecimal() functions, some things need to be said. The lowest possible base is 2, so if the user tries to specify a base smaller than 2, we will change it to 2. Likewise, there is a practical upper limit of base 36. Beyond that, things get more complicated, so any base higher than 36 will be changed to 36 to prevent the function from going out of bounds and returning bad data.


ASP

  1. function toDecimal(value, radix)
  2.     dim result
  3.     dim digit
  4.     result = 0
  5.     ' Prevent radix from going out of bounds.
  6.     if radix < 2 then
  7.         radix = 2
  8.     elseif radix > 36 then
  9.         radix = 36
  10.     end if
  11.     for i = 1 to Len(value)
  12.         digit = Mid(value, i, 1)
  13.         ' Convert letters to numbers.
  14.         if not isNumeric(digit) then
  15.             ' The letter A in any base is equal to 10 in decimal.
  16.             ' The ASCII value of A is 65, so subtract 55 from the ASCII value to obtain the decimal value.
  17.             digit = Asc(UCase(digit)) - 55
  18.         else
  19.             digit = CInt(digit)
  20.         end if
  21.         ' Return zero if any digit is out of bounds for the radix.
  22.         if digit >= radix then
  23.             result = 0
  24.             exit for
  25.         end if
  26.         result = result + (digit * radix ^ (Len(value) - i))
  27.     next
  28.     toDecimal = result
  29. end function
  30. function fromDecimal(value, radix)
  31.     dim result
  32.     dim digit
  33.     result = 0
  34.     ' Prevent radix from going out of bounds.
  35.     if radix < 2 then
  36.         radix = 2
  37.     elseif radix > 36 then
  38.         radix = 36
  39.     end if
  40.     ' Check for invalid input.
  41.     if isNumeric(value) then
  42.         ' Inputted value appears to be base 10. OK to proceed.
  43.         do until value = 0
  44.             digit = value Mod radix
  45.             if digit > 9 then
  46.                 digit = Chr(digit + 55)
  47.             end if
  48.             result = CStr(digit) & result
  49.             value = value \ radix
  50.         loop
  51.     else
  52.         ' Inputted value was NOT base 10.
  53.         result = 0
  54.     end if
  55.     fromDecimal = result
  56. end function

It would be even better if we could easily convert between two bases where neither one is decimal. Also, it would be nice to have a unified interface. We can achieve both with one function, base_convert(), which is also present in PHP.


ASP

  1. function base_convert(value, sourceRadix, targetRadix)
  2.     if sourceRadix = targetRadix then
  3.         ' If source radix and target radix are equal, don't waste time converting.
  4.         baseConv = value
  5.     elseif sourceRadix = 10 then
  6.         ' If source radix is decimal, skip converting to decimal.
  7.         baseConv = fromDecimal(value, targetRadix)
  8.     elseif targetRadix = 10 then
  9.         ' If target radix is decimal, skip converting from decimal.
  10.         baseConv = toDecimal(value, sourceRadix)
  11.     else
  12.         ' Convert to decimal, and then from decimal.
  13.         baseConv = fromDecimal(toDecimal(value, sourceRadix), targetRadix)
  14.     end if
  15. end function

I leave you with the following potential exercises:

  • From base 37 to 62, the lowercase letters of the Latin alphabet are used, but special handling would need to be added for them. The ASCII value for Z is 90. The ASCII value for a is 97. There are six other characters in between.
  • Base64 encoding begins with the uppercase letters A-Z, followed by a-z, followed by 0-9, followed by + and /. If you want to add Base64 support, separate functions would be wise, but could still be tied into the base_convert() wrapper.