Fraction
Author Message
Fraction

Are there any good routines for converting fractions to decimal numbers and
decimal numbers to fractions

Sat, 17 Nov 2001 03:00:00 GMT
Fraction
Fractions to decimals is easy. Decimals to fractions is a bit harder (and
many decimal values will not reduce much) - but if you consider (say) 0.875
to be 875/1000 then all you need to do is reduce this fraction. Some months
ago Bart Lateur posted some code to do exactly this - and I am sure that he
will not mind me reproducing it here:

Subject: Re: Working with fractions
Date: 29 October 1998 22:20

If we represent a fraction by two (long) integers, T and B (for Top and
Bottom; I find the official names a bit too difficult); then you can
reduce this fraction "T/B" by dividing T and B by their GCD (greatest
common divisor).

For example: "14/21", the GCD of 14 and 21 is 7, so we do T/7 and B/7
resulting in "2/3".

The GCD can easiliy be calculated using Euclid's method (see
<http://www.cut-the-knot.com/blue/binary.html> for a mathemaical proof):

To calculate the GCD of two positive numbers M and N:
* divide the large number by the smaller; we only need the remainder;
* if the remainder is zero, the smaller number is the GCD;
* if it's not, replace the larger number by the smaller, and the
smaller by the remainder. Repeat until the remainder IS zero.

Example: 14 and 21. 21 mod 14 is 7; 14 mod 7 is 0. Therefore, the GCD is
7.

Special care must be taken if either Top or Bottom are negative.

Here's some code.

Function gcd (ByVal m&, ByVal n&) As Long
'm and n must be > 0
Dim temp&
If m < n Then temp = m: m = n: n = temp
Do
temp = m Mod n
If temp = 0 Then Exit Do
m = n
n = temp
Loop
gcd = n
End Function

Function fraction (ByVal t&, ByVal b&) As String
Dim c&, sign%
If b < 0 Then t = -t: b = -b
c = gcd(Abs(t), Abs(b))
If c = b Then
fraction = CStr(t / b)
Else
fraction = CStr(t / c) & "/" & CStr(b / c)
End If
End Function

You could improve on the representation, for T > B, by splitting of the
integer part. Hint: you need Cstr(int(T/B)) and
Cstr(T mod B) & "/" & Cstr(B)

Mike

Quote:

>Are there any good routines for converting fractions to decimal numbers and
>decimal numbers to fractions

Sat, 17 Nov 2001 03:00:00 GMT
Fraction
As stated by Mike, fractions to decimals is easy. If you are looking for a
nice function to convert a decimal to a fraction then check out Entisoft
Tools. Entisoft Tools has a built in function called FloatToFraction. This
function takes a decimal number and the devisor you give it and converts it
to a string representation of the fraction. You can check out Entisoft Tools
at http://www.entisoft.com

Good Luck

Quote:
> Are there any good routines for converting fractions to decimal numbers
and
> decimal numbers to fractions

Sat, 17 Nov 2001 03:00:00 GMT
Fraction
How does it handle decimal values which do not convert exactly in terms of
the devisor which you specify (which would in practice be most decimal
values). Does it give you the enumerator rounded to the nearest whole
number. For example, how would it handle 0.20625 (13.2 divided by 64) if you
specified the devisor 64?

Mike

Quote:

>As stated by Mike, fractions to decimals is easy. If you are looking for a
>nice function to convert a decimal to a fraction then check out Entisoft
>Tools. Entisoft Tools has a built in function called FloatToFraction. This
>function takes a decimal number and the devisor you give it and converts it
>to a string representation of the fraction. You can check out Entisoft
Tools
>at http://www.entisoft.com

>Good Luck

>> Are there any good routines for converting fractions to decimal numbers
>and
>> decimal numbers to fractions

Sun, 18 Nov 2001 03:00:00 GMT
Fraction
The Float 0.20625 is rounded and displayed as 13/64. If you give it a
divisor of 128 it returns the same thing. Given a devisor of 32 it returns
7/32. I hope this can help.

Quote:
> How does it handle decimal values which do not convert exactly in terms of
> the devisor which you specify (which would in practice be most decimal
> values). Does it give you the enumerator rounded to the nearest whole
> number. For example, how would it handle 0.20625 (13.2 divided by 64) if
you
> specified the devisor 64?

> Mike

> >As stated by Mike, fractions to decimals is easy. If you are looking for
a
> >nice function to convert a decimal to a fraction then check out Entisoft
> >Tools. Entisoft Tools has a built in function called FloatToFraction.
This
> >function takes a decimal number and the devisor you give it and converts
it
> >to a string representation of the fraction. You can check out Entisoft
> Tools
> >at http://www.entisoft.com

> >Good Luck

> >> Are there any good routines for converting fractions to decimal numbers
> >and
> >> decimal numbers to fractions

Sun, 18 Nov 2001 03:00:00 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages