How to resolve the algorithm Convert decimal number to rational step by step in the VBA programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Convert decimal number to rational step by step in the VBA programming language
Table of Contents
Problem Statement
The task is to write a program to transform a decimal number into a fraction in lowest terms. It is not always possible to do this exactly. For instance, while rational numbers can be converted to decimal representation, some of them need an infinite number of digits to be represented exactly in decimal form. Namely, repeating decimals such as 1/3 = 0.333... Because of this, the following fractions cannot be obtained (reliably) unless the language has some way of representing repeating decimals: Acceptable output: Finite decimals are of course no problem:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Convert decimal number to rational step by step in the VBA programming language
Source code in the vba programming language
Function Real2Rational(r As Double, bound As Long) As String
If r = 0 Then
Real2Rational = "0/1"
ElseIf r < 0 Then
Result = Real2Rational(-r, bound)
Real2Rational = "-" & Result
Else
best = 1
bestError = 1E+99
For i = 1 To bound + 1
currentError = Abs(i * r - Round(i * r))
If currentError < bestError Then
best = i
bestError = currentError
If bestError < 1 / bound Then GoTo SkipLoop
End If
Next i
SkipLoop:
Real2Rational = Round(best * r) & "/" & best
End If
End Function
Sub TestReal2Rational()
Debug.Print "0.75" & ":";
For i = 0 To 5
Order = CDbl(10) ^ CDbl(i)
Debug.Print " " & Real2Rational(0.75, CLng(Order));
Next i
Debug.Print
Debug.Print "0.518518" & ":";
For i = 0 To 5
Order = CDbl(10) ^ CDbl(i)
Debug.Print " " & Real2Rational(0.518518, CLng(Order));
Next i
Debug.Print
Debug.Print "0.9054054" & ":";
For i = 0 To 5
Order = CDbl(10) ^ CDbl(i)
Debug.Print " " & Real2Rational(0.9054054, CLng(Order));
Next i
Debug.Print
Debug.Print "0.142857143" & ":";
For i = 0 To 5
Order = CDbl(10) ^ CDbl(i)
Debug.Print " " & Real2Rational(0.142857143, CLng(Order));
Next i
Debug.Print
Debug.Print "3.141592654" & ":";
For i = 0 To 5
Order = CDbl(10) ^ CDbl(i)
Debug.Print " " & Real2Rational(3.141592654, CLng(Order));
Next i
Debug.Print
Debug.Print "2.718281828" & ":";
For i = 0 To 5
Order = CDbl(10) ^ CDbl(i)
Debug.Print " " & Real2Rational(2.718281828, CLng(Order));
Next i
Debug.Print
Debug.Print "-0.423310825" & ":";
For i = 0 To 5
Order = CDbl(10) ^ CDbl(i)
Debug.Print " " & Real2Rational(-0.423310825, CLng(Order));
Next i
Debug.Print
Debug.Print "31.415926536" & ":";
For i = 0 To 5
Order = CDbl(10) ^ CDbl(i)
Debug.Print " " & Real2Rational(31.415926536, CLng(Order));
Next i
End Sub
You may also check:How to resolve the algorithm Write language name in 3D ASCII step by step in the Plain English programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the OCaml programming language
You may also check:How to resolve the algorithm Dining philosophers step by step in the Erlang programming language
You may also check:How to resolve the algorithm HTTP step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Singleton step by step in the Factor programming language