of     1   

1waffle1
#60116387Saturday, December 24, 2011 5:04 AM GMT

I've figured out and tested this, only using 18 lines.     function Fraction(a)         local A = 10^(#tostring(a)-1)         if tostring(a):find("%.") then A=A/10 end         local B = a*A         local gcf = A         local last = A         repeat             last = gcf             gcf = math.max(gcf, B)%math.min(gcf, B)         until gcf == 0         local mult = tostring(B/last):find("%.")         if mult then             mult = 10^(#tostring(B/last):sub(mult)-1)         else             mult = 1         end         return B/last*mult.."/"..A/last*mult     end
[rfa#hidefromsearch]
#60116672Saturday, December 24, 2011 5:10 AM GMT

[rfa#hidefromsearch]
greatswordpwner
#60117636Saturday, December 24, 2011 5:28 AM GMT

May you tell me this script's importance?
Aaaboy97
#60117778Saturday, December 24, 2011 5:31 AM GMT

print(Fraction(17/6)) >30997150997151/10940170940171
LocalDimensions
#60118519Saturday, December 24, 2011 5:45 AM GMT

Or Varp's: local luaPrecision function toBinary(dec) local sig,exp = math.frexp(dec) local initexp = exp local t = {} exp = -1 while(sig > 0)do for i = exp,-2 do t[#t+1] = 0 --print(0) end t[#t+1] = 1 --print(1) sig,exp = math.frexp(sig-0.5) end return initexp,t end function testRepetition(table,len) local last = #table-len-2 --Ignore last digit for i = last-len,0,-len do for j = len-1,0,-1 do if(i+j < 0)then return 1 end if(table[last+j] ~= table[i+j])then return i+j+1 end end end return 1 end function toNumber(bin,start,last) local n = 0 for i = start or 1,last or #bin do n = n*2 + bin[i] end return n end luaPrecision = #select(2,toBinary(1/3)) function toFraction(dec) local exp,bin = toBinary(dec) if(#bin < luaPrecision/2)then --The decimal terminates quickly. local minExp = exp - #bin - 1 if(minExp < 0)then return toNumber(bin),math.ldexp(.5,-minExp) else return toNumber(bin)*math.ldexp(.5,minExp+2),1 end end local bestVal = math.huge local repNum local repStart local potential = 3 local reps = 2 while(potential < bestVal and reps < #bin/2)do local pos = testRepetition(bin,reps) if(pos < #bin-reps-reps-2)then local val = potential * math.ldexp(.5,pos) if(val < bestVal)then bestVal = val repNum = reps repStart = pos end end reps = reps + 1 potential = potential * 2 + 1 end if(bestVal == math.huge)then --No repetition found. Just write as binary. local minExp = exp - #bin - 1 if(minExp < 0)then return toNumber(bin),math.ldexp(.5,-minExp) else return toNumber(bin)*math.ldexp(.5,minExp+2),1 end end local num = toNumber(bin,repStart,repStart+repNum-1) local den = math.ldexp(.5,repNum+1)-1 local rawden = den local repExp = exp - repStart + 2 if(repExp > 0)then num = num * math.ldexp(.5,repExp) else den = den * math.ldexp(.5,2-repExp) end if(repStart > 1)then local binpart = toNumber(bin,1,repStart-1) num = num + binpart * rawden end local gcf = den --Euclidean algorithm follows. local tempb = num while(tempb ~= 0)do local t = tempb tempb = gcf % t gcf = t end return num/gcf,den/gcf end print(toFraction(7/9)) strike witches sanya
Aaaboy97
#60118628Saturday, December 24, 2011 5:47 AM GMT

@Varps print(toFraction(13/7)) >2    1
MicroUser
#60118977Saturday, December 24, 2011 5:54 AM GMT

Aaaboy97
#60119170Saturday, December 24, 2011 5:58 AM GMT

@MicroUser, how can that convert decimals to fractions? All I see is a way to use fractions
MicroUser
#60119824Saturday, December 24, 2011 6:11 AM GMT

fraction_class.create_fract(10.456778345, 1) MMMMMMM you just add a 1 below it? =)
1waffle1
#60121884Saturday, December 24, 2011 7:02 AM GMT

@Aboy It doesn't like repeating decimals. 30997150997151/10940170940171 = 2.83333333 17/6 = 2.8333...
NXTBoy
#60134645Saturday, December 24, 2011 3:15 PM GMT

Can I claim the prize? http://wiki.roblox.com/index.php/User:NXTBoy/Scripts/Fraction     print(Fraction.fromFloat(1/3) + Fraction.fromFloat(1/2)) --5/6     print(Fraction.new(1, 6) + Fraction.new(1, 3)) --1/2          --Best representation of pi in the form p / q where q < 100     print(Fraction.fromFloat(math.pi, 100)) --22/7
1waffle1
#60147461Saturday, December 24, 2011 6:27 PM GMT

No, because it's huge.
Merlin11188
#60148138Saturday, December 24, 2011 6:37 PM GMT

WOAH! I found an accurate converter online that walks you through it step-by-step. That's interesting. I think I'm going to make an actual decimal to fraction converter for the userdata fraction class I'll make shortly afterward.
TheCapacitor
#60148168Saturday, December 24, 2011 6:38 PM GMT

Or mine: print(numerator .. "/" .. denominator)
TheCapacitor
#60148397Saturday, December 24, 2011 6:42 PM GMT

Wait, replace denominator to "1" print(decimal .. "/1")
NXTBoy
#60149661Saturday, December 24, 2011 7:03 PM GMT

@1waffle1: Most of it is just operator overloading, which isn't actually used. The important bit, _as labelled in the code_, is:     --Important bit     Fraction.fromFloat = function(input, maxDenominator)         maxDenominator = maxDenominator or 1e99              local f0 = Fraction.new(1, 0)         local f1 = Fraction.new(math.floor(input), 1)         local f2              local r = input % 1         local next_cf              while math.abs(r) ​>= 0.0001 do             r = 1 / r             next_cf = math.floor(r)             f2 = Fraction.new(                 next_cf * f1.p + f0.p,                 next_cf * f1.q + f0.q             )                  -- Limit the denominator             if f2.denominator ​> maxDenominator then break end                  -- remember the last two fractions             f0 = f1             f1 = f2                  r = r - next_cf         end         return f1     end
pwnedu46
#60152664Saturday, December 24, 2011 7:53 PM GMT

@NXT:     maxDenominator = maxDenominator or 1e99 If wouldn't 1e99 cause an overflow if you didn't supply the maxDenominator argument? ----------     ~ pwnedu46, the unicorn ~
aboy5643a
#60166552Saturday, December 24, 2011 11:21 PM GMT

"@Aboy" Thats me you n0b. Namestealer is aaaboy

    of     1