Your Ad Here

Monday, February 18, 2008

Decimal Value to Roman Numeral - Algorithm 2

If you haven't seen the earlier algorithm to change Decimal Values to Roman Numerals you can read that blog here: Decimal Values to Roman Numeral Algorithm 1.


The following code represents a shorter algorithm that should be easy enough to understand. Algorithm 2 does away with the cumbersome IF statements from the previous algorithm and wraps up all the conversions in one WHILE loop. This function the same principle as that used in the previous algorithm, but instead of hard coding all the values of the Roman Numeral characters and pairs into separate IF and WHILE clauses, the values and characters are stored in a couple of arrays and referenced in a single WHILE loop. I have not compared the speeds of the two algorithms demonstrated here and in Decimal Values to Roman Numeral Algorithm 1. The speed tests will be covered in a later blog, but for now, here's the code written in VB .NET:



DECIMAL to ROMAN Algorithm 2


Private Sub cmdConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvert.Click

' Declare and initialize all variables. Roman Numerals and their standard combinations
' are stored in strRoman. The corresponding values are stored in intDecimal.

Dim strRoman() As String = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
Dim intDecimal() As Integer = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
Dim intInput As Integer = Nothing ' Numeric representation of the input field.
Dim intTemp As Integer = Nothing ' Used to store the magnitude of each decimal digit.
Dim intPointer As Short = 0 ' Pointer to the Roman and Decimal arrays, starts at the first cell.
Dim intCountI As Short = Nothing ' Temporary variable used for loops.

intInput = CInt(txtInput.Text) ' Convert the string in the input field to an integer.
txtOutput.Clear() ' Clear the contents of the output field.

While intInput > 0 ' Check to see if intInput still has any value left in it.
intTemp = intInput \ intDecimal(intPointer) ' See how many of the currently selected value can fit in the remaining input.

For intCountI = 1 To intTemp
txtOutput.Text += strRoman(intPointer) ' Append a number of Roman Characters depending on the value of intTemp.
Next

intInput -= intTemp * intDecimal(intPointer) ' Subtract the value of the characters that were appended to output from the input.
intPointer += 1 ' Move the pointer to the next cell of the arrays.
End While

End Sub


You can download a ZIP file with all the VB .NET project files and an executable using this algorithm here. You can also see several other algorithm implementations at the Computer Science 101 website.

Saturday, February 16, 2008

Convert Floating Point Decimal Values to Floating Point Binary Representation


A visitor to this site send me an email asking if it were possible to use the Numeric Base Conversion algorithm to convert fractions between bases.  I thought it would be pretty straight forward to implement the change to the general algorithm, but found it was easier just to make a single purpose algorithm to convert from decimal to binary, since that was specifically what he asked for.  As time allows, I'll update the algorithm to make it general purpose to convert fractions between any two bases.


This algorithm borrows from the general Numeric Base Conversion algorithm, but it has been simplified to only convert from base 10 to base 2, and an additional portion that converts the fractional part of the input number to a binary fraction has been added.


Binary fractions are a bit tricky to work with.  Some values convert easily, such as 0.5, 0.375, and 0.75, which are 0.1, 0.011, and 0.11, respectively, in binary form.  Other fractional values, which are easily displayed in base 10, become long strings of seemingly random bits.  For instance, 1/10 = 0.1 (base 10) = 0.000110011001100110... (base 2), and 1/3 = 0.33333... (base 10) = 0.010011001100110011... (base 2), and 1/100 = 0.01 (base 10) = 0.000000101000111101... (base 2).


If you are already familiar with binary notation, you recognize that each binary digit represents a power of 2.  Starting at the least significant bit and moving towards the most significant bit, the bits represent 1, 2, 4, 8, 16, 32, 64, 128, etc.  Just as decimal fraction digits represent the inverses of the powers of 10: 1/10, 1/100, 1/1000, etc, binary fractional bits represent the inverses of the powers of 2:  1/2, 1/4, 1/8, 1/16, etc., which are 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.0078125, etc.  As you can see, we have to take a binary fraction out to 7 places before we get less than 0.01 (base 10).


The Visual Basic code below will only output 20 bits, including the decimal point.  You can modify this yourself by changing the value assigned to intNumBits near the top of the listing.  The embedded comments in the code should be fairly explanatory, but if you have questions or comments, feel free to leave a comment to this post.



Private Sub Dec2Bin_Click()

' Subroutine to convert floating point decimal values to floating point binary values.
'
' Written by: Erik Oosterwal
' Started on: October 26, 2005
' Completed: October 27, 2005

Dim txtNumericBaseData, txtOutputValue As String
Dim intX, intNumBits As Integer
Dim dblDecimalValue, dblFractionValue, dblBinaryFraction As Double


intNumBits = 20 ' The number of bits in the output (including the decimal point)
txtNumericBaseData = "01" ' Output digits can be either "0" or "1"
dblBinaryFraction = 0.5 ' Binary fraction bits are powers of 0.5
txtOutput.Text = "" ' Clear the display
txtOutputValue = "" ' Clear the working output variable


dblDecimalValue = Int(CDbl(txtInput.Text)) ' Get the integer portion of the input
dblFractionValue = CDbl(txtInput.Text) - dblDecimalValue ' Get the fractional portion of the input


' Figure out the integer portion of the input.
While dblDecimalValue > 0
intX = Int(((dblDecimalValue / 2) - Int(dblDecimalValue / 2)) * 2 + 1.5)
txtOutputValue = Mid(txtNumericBaseData, intX, 1) + txtOutputValue
dblDecimalValue = Int(dblDecimalValue / 2)
Wend


If txtOutputValue = "" Then txtOutputValue = "0" ' If there was no whole number, set it to "0"
txtOutputValue = txtOutputValue + "." ' then add a decimal point


' Figure out the fractional portion of the input.
While Len(txtOutputValue) < intNumBits And dblFractionValue > 0 ' As long as we're not exceeding the
' allowed number of bits in the output
' and there's still part of the input
' value to be converted...
If dblFractionValue >= dblBinaryFraction Then ' If the input number is larger than the fraction bit,
txtOutputValue = txtOutputValue + "1" ' add a "1" to the output and
dblFractionValue = dblFractionValue - dblBinaryFraction ' reduce the input number by the fraction bit's value.
Else ' Otherwise...
txtOutputValue = txtOutputValue + "0" ' just tag on a "0" to the end of the output.
End If
dblBinaryFraction = dblBinaryFraction / 2# ' The fraction bit must be cut in half.
Wend

txtOutput.Text = txtOutputValue ' Send the output value to the display.

End Sub




For a more generalized routine that can convert integer values between any two numeric bases, see the Numeric Base Conversion article from the main Computer Science 101 page.


You can also download a zip file that contains the listing given above as well as an executable file that demonstrates this routine.

Convert Decimal Values to Roman Numerals - Algorithm 1


Roman numerals follow a fairly strict rule in sequence in order to reduce ambiguity in numbers. To create Roman numerals we must first define which letters represent which values, and what exceptions there are to the basic rule.

First, the following letters represent the associated values:



I = 1

V = 5

X = 10

L = 50

C = 100

D = 500

M = 1000



The basic rule states that numerals are read from left to right, larger valued letters precede smaller valued letters, and multiple letters in a row represent multiples of that value. For instance:


I = 1
II = 2
III = 3
VIII = 8
CVIII = 108
CCVIII = 208
DCCVIII = 708
MDCCVIII = 1708



The single exception to the basic rule is that if a larger value letter is preceded by a single smaller value letter, then the combination represents the value of the larger value letter minus the value of the smaller value letter. This exception only holds true for the values 4, 9, 40, 90, 400, and 900, which are written as IV, IX, XL, XC, CD, and CM respectively.

A seldom used convention of Roman numerals is that you can create values larger values by overscoring letters to represent the letter's normal value multiplied by 1000:


_
V = 5000
_
X = 10,000
_
L = 50,000
_
C = 100,000
_
D = 500,000
_
M = 1,000,000



We won't bother with the larger values in this routine and limit our numbers to values between 1 and 3999.



DECIMAL to ROMAN Algorithm 1


The following pseudo code should provide enough information to implement the algorithm in your language of choice:


function roman(InputValue) as string

dim InputValue as integer ' Declare Variables
dim RomanValue as string

if (InputValue > 3999) OR (InputValue < 1)
roman = "N/A"
return
endif

RomanValue = ""

While InputValue > 999 (
RomanValue = RomanValue + "M" ' Concatenate the letters to the right side
InputValue = InputValue - 1000 ' Reduce the amount left in InputValue
)

If InputValue > 899 (
RomanValue = RomanValue + "CM" ' Concatenate letters to the right side
InputValue = InputValue - 900
)

If InputValue > 499 (
RomanValue = RomanValue + "D"
InputValue = InputValue - 500
)

If InputValue > 399 (
RomanValue = RomanValue + "CD"
InputValue = InputValue - 400
)

While InputValue > 99 (
RomanValue = RomanValue + "C"
InputValue = InputValue - 100
)

If InputValue > 89 (
RomanValue = RomanValue + "XC"
InputValue = InputValue - 90
)

If InputValue > 49 (
RomanValue = RomanValue + "L"
InputValue = InputValue - 50
)

If InputValue > 39 (
RomanValue = RomanValue + "XL"
InputValue = InputValue - 40
)

While InputValue > 9 (
RomanValue = RomanValue + "X"
InputValue = InputValue - 10
)

If InputValue > 8 (
RomanValue = RomanValue + "IX"
InputValue = InputValue - 9
)

If InputValue > 4 (
RomanValue = RomanValue + "V"
InputValue = InputValue - 5
)

If InputValue > 3 (
RomanValue = RomanValue + "IV"
InputValue = InputValue - 4
)

While InputValue > 0 (
RomanValue = RomanValue + "I"
InputValue = InputValue - 1
)

roman = RomanValue

return

This is not a particularly efficient algorithm. In later articles additional algorithms will be discussed that show better ways to convert from Decimal Values to Roman Numerals and from Roman Numerals back to Decimal Values.