# Convert a decimal number to a fraction function.

## Recommended Posts

This DecToFraction() function converts a decimal number to an equivalent fraction.

Here is the results of this example script using the DecToFraction() function.

The last three results all use Pi, but use increasing accuracy to establish the fraction.

Results

```Decimal = -1.2____________ Fraction =   -6/5________ = -1 1/5
Decimal = 1.2 _____________Fraction =   6/5 _________= 1 1/5
Decimal = 0.1313__________ Fraction =   13/99 _______= 13/99
Decimal = -2.8571_________ Fraction =   -20/7_______ = -2 6/7
Decimal = 0.06667_________ Fraction =   1/15________ = 1/15
Decimal = 3.14 ___________ Fraction =   22/7_________ = 3 1/7
Decimal = 3.14159265358979 Fraction =   333/106_____ = 3 15/106
Decimal = 3.14159265358979 Fraction =   103993/33102 = 3 4687/33102```

This appears to work well.

Testing will tell.

```#include <Misc.au3>

; Modified method from http://mathforum.org/library/drmath/view/51886.html

Local \$aDec = [[-1.2, 2],[1.2, 9],[Round(13 / 99, 4), 4],[Round(-20 / 7, 4), 4],[0.06667, 5], _
[Round(4 * ATan(1), 2), 2],[4 * ATan(1), 3],[4 * ATan(1), 9]]
Local \$sRes

For \$i = 0 To UBound(\$aDec) - 1

\$sRes &= "Decimal = " & \$aDec[\$i] & @TAB & _
" Fraction = " & @TAB & \$FracArray * (\$FracArray * \$FracArray + \$FracArray) & "/" & \$FracArray & @TAB & _
" = " & _Iif(\$FracArray <> 0, \$FracArray * \$FracArray, "") & " " & \$FracArray & "/" & \$FracArray & @CRLF
Next

MsgBox(0, "Decimals to Fractions", \$sRes)
;ConsoleWrite(\$sRes & @CRLF)

;\$iAcc - Accuracy - an integer - The number of decimal places for an equality check to establish the fraction.
;Return an array
;   array = Indicates if number is plus or minus, -1 for negative, or, 1 for positive
;   array = Integer part of decimal number.
;   array = Numerator of fraction
;   array = Denominatot of fraction.
;
Func DecToFraction(\$frDec, \$iAcc = 2)
Local \$aAns
If \$frDec < 0 Then ; Sign +/-
\$aAns = -1
Else
\$aAns = 1
EndIf

If StringInStr(\$frDec, ".") > 0 Then
Local \$aDec = StringSplit(Abs(\$frDec), ".", 2)
\$aAns = \$aDec ; Integer part of decimal number
Local \$a = Floor(\$fr), \$b = Ceiling(\$fr), \$c = \$b, \$d = \$b, \$x, \$low, \$high, \$a1, \$b1, \$c1, \$d1
Do
\$x = Round((\$b * \$fr - \$a) / (\$c - \$d * \$fr), 12)
\$low = Floor(\$x)
\$high = Ceiling(\$x)
\$a1 = (\$a + \$low * \$c)
\$b1 = (\$b + \$low * \$d)
\$c1 = (\$a + \$high * \$c)
\$d1 = (\$b + \$high * \$d)
\$a = \$a1
\$b = \$b1
\$c = \$c1
\$d = \$d1
If Abs(\$a / \$b - \$fr) < 10.0 ^ (-\$iAcc) Then ; 10.0 ^ (-\$iAcc)
\$aAns = \$a
\$aAns = \$b
ExitLoop ;
EndIf
If Abs(\$c / \$d - \$fr) < 10.0 ^ (-\$iAcc) Then ;10.0 ^ (-\$iAcc)
\$aAns = \$c
\$aAns = \$d
ExitLoop ;
EndIf
Until 0
Else
\$aAns = Abs(\$frDec)
EndIf
Return \$aAns
EndFunc ;==>DecToFraction```
##### Share on other sites

Any how... A few changes. No longer uses string functions, and is slightly shorter (no idea about speed, although it shouldn't change too much)

Also, now returns 0/0 if an integer is returned, rather than just blank.

```Func DecToFraction(\$frDec, \$iAcc= 2)
If \$frDec < 0 Then \$aAns = -1

If Mod(\$frDec, 1) Then
\$fr = Abs (\$frDec - \$aDec)
Local \$a = Floor(\$fr), \$b = Ceiling(\$fr), \$c = \$b, \$d = \$b, \$x, \$low, \$high, \$a1, \$b1, \$c1, \$d1
Do
\$x = Round((\$b * \$fr - \$a) / (\$c - \$d * \$fr), 12)
\$low = Floor(\$x)
\$high = Ceiling(\$x)
\$a1 = (\$a + \$low * \$c)
\$b1 = (\$b + \$low * \$d)
\$c1 = (\$a + \$high * \$c)
\$d1 = (\$b + \$high * \$d)
\$a = \$a1
\$b = \$b1
\$c = \$c1
\$d = \$d1
If Abs(\$a / \$b - \$fr) < 10.0 ^ (-\$iAcc) Then ; 10.0 ^ (-\$iAcc)
\$aAns = \$a
\$aAns = \$b
ExitLoop
EndIf
If Abs(\$c / \$d - \$fr) < 10.0 ^ (-\$iAcc) Then ;10.0 ^ (-\$iAcc)
\$aAns = \$c
\$aAns = \$d
ExitLoop
EndIf
Until 0
EndIf

Return \$aAns
EndFunc ;==>DecToFraction```

I did some work on the integer and fraction parts of a number... This explains those two functions (as well as a few other rounding functions too)

http://homepage.smc.edu/kennedy_john/GALLERYROUND.PDF

Mat

##### Share on other sites

Nice work! I love your math udf's.

[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

##### Share on other sites

Which reminds me. Here is a list of everything maths related on the forums that I could find. I don't think its everything, since its restricted to only a few authors...

Pm me if you want something added

Mat

Edit: I completely missed out on Primes.au3 Edited by Mat
##### Share on other sites

Any how... A few changes. No longer uses string functions, and is slightly shorter (no idea about speed, although it shouldn't change too much)

Also, now returns 0/0 if an integer is returned, rather than just blank.

```Func DecToFraction(\$frDec, \$iAcc= 2)
If \$frDec < 0 Then \$aAns = -1

If Mod(\$frDec, 1) Then
\$fr = Abs (\$frDec - \$aDec)
Local \$a = Floor(\$fr), \$b = Ceiling(\$fr), \$c = \$b, \$d = \$b, \$x, \$low, \$high, \$a1, \$b1, \$c1, \$d1
Do
\$x = Round((\$b * \$fr - \$a) / (\$c - \$d * \$fr), 12)
\$low = Floor(\$x)
\$high = Ceiling(\$x)
\$a1 = (\$a + \$low * \$c)
\$b1 = (\$b + \$low * \$d)
\$c1 = (\$a + \$high * \$c)
\$d1 = (\$b + \$high * \$d)
\$a = \$a1
\$b = \$b1
\$c = \$c1
\$d = \$d1
If Abs(\$a / \$b - \$fr) < 10.0 ^ (-\$iAcc) Then ; 10.0 ^ (-\$iAcc)
\$aAns = \$a
\$aAns = \$b
ExitLoop
EndIf
If Abs(\$c / \$d - \$fr) < 10.0 ^ (-\$iAcc) Then ;10.0 ^ (-\$iAcc)
\$aAns = \$c
\$aAns = \$d
ExitLoop
EndIf
Until 0
EndIf

Return \$aAns
EndFunc ;==>DecToFraction```

I did some work on the integer and fraction parts of a number... This explains those two functions (as well as a few other rounding functions too)

Mat

I found out why your script does not work with negative values.

`\$fr = Abs (\$frDec - \$aDec)`

Change to,

`\$fr = Abs (\$frDec - int(\$frDec))`

Apart from the mistake in logic, I like your alterations/improvements in principle. I don't know if there is an improvement in performance.

##### Share on other sites

I found out why your script does not work with negative values.

Apart from the mistake in logic, I like your alterations/improvements in principle. I don't know if there is an improvement in performance.

Ah right... Never noticed that! I don't know if there is an improvement to performance, but I think that its always nice to have *pure* maths solutions.

Mat

## Create an account

Register a new account

• ### Recently Browsing   0 members

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...