View Full Version : Divide function
ljuwaidah
11-15-2008, 05:55 AM
Hi,
I started a challenge like this one on some other forum but many people found it too hard so it didn't go so well, let's hope it goes well this time :)
It's pretty simple: Create a divide function and optimize it!
Have fun,
I'll post my code soon (I have it already).
[Edit:] You have to do this without using the divide operator /.
ljuwaidah
11-16-2008, 06:10 AM
:|
private function init():void {
number.text = divide(12, 7).toString();
}
private function divide(num1:Number, num2:Number):Number
{
var places:int = 0;
var tempRes:Number;
var ltCounter:int = 0;
var temp:Number;
var point:Boolean = false;
var container:String = "";
var counter:int;
var result:Number;
while(num1 % int(num1))
{
num1 *= 10;
places++;
}
while(num2 % int(num2))
{
num2 *= 10;
places++;
}
tempRes = num1;
while(tempRes != 0 && ltCounter <= 17)
{
temp = tempRes;
if(temp < num2)
{
temp *= 10;
if(!point)
{
if(container == "")
{
container += "0";
}
container += ".";
point = true;
}
}
counter = 0;
while(temp >= num2)
{
temp -= num2;
counter++;
}
container += counter.toString();
tempRes = temp;
ltCounter++;
}
result = parseFloat(container);
result *= Math.pow(10, places);
return result;
}
pj-co
11-16-2008, 09:26 AM
you gotta give people more that a saturday night to do this... not nearly enough people visit this part of the forum on normal days much less weekends...
ljuwaidah
11-16-2008, 09:43 AM
I forgot it's the weekend in the normal world :| Our weekend is on Thu and Fri, sigh.
pj-co
11-16-2008, 10:31 AM
dont get too discouraged, it was a very cool challenge. just give it more time next time :)
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.