whoa... hold on.... Help Please.
After I posted yesterday, (or the night before??? can't remember) as I
realized how badly the code sucked, I was just messing around with this
code below to see if I could separate the halves and recompose them
properly; however,
if you pop the code below on the timeline, you will see why I am confused.
I know it's not the greatest piece of code in the world, especially
since I was just messing around mentally, but it should work. Why is
the process of converting to a Number adding .1????
function floatSumV3(roundDown:Boolean = true, ... nums):Number{
var nl:uint = nums.length;
var ls:Number=0; //left side
var pp:int; //period position
var rs:Number = 1; //right side
var rsl:int; //right side length
var ml:int = 0; //max length right side
var powerCounter:int = 1;
var powerResult:int;
var s:String;
for (var i:int = 0; i< nl; ++i) {
s = nums[i].toString();
pp = s.indexOf('.');
if(pp!=-1){
rsl = s.length - pp-1;
if(rsl>ml){
powerCounter = rsl - ml;
powerResult=1;
while(powerCounter!=0){
powerResult *= 10;
powerCounter -= 1;
}
rs *= powerResult;
rs += Number(s.substr(pp+1));
ml=rsl;
//trace('ml = '+ml+', rs = '+ rs);
}else if (rsl<ml){
powerCounter = ml-rsl;
powerResult=1;
while(powerCounter!=0){
powerResult*= 10;
powerCounter -= 1;
}
rs += Number(s.substr(pp+1)) * powerResult;
}else {
rs+=Number(s.substr(pp+1));
}
ls += Number(s.substr(0,pp));
//trace('numls = ' + s.substr(0,pp));
//trace('ls = ' + ls);
}else{
ls +=Number(s);
//trace('ls = ' + s);
//trace('ls = ' + ls);
}
}
s = rs.toString();
if(s.length>ml){
ls += Number(s.substr(0,s.length - ml))-1;
}
s=ls+"."+s.substr(s.length-ml);
trace(s, "= initial string value");
if(s.length > 16 && roundDown == true){
if(ls.toString().length==16){
s = s.substr(0,18);
}else if(ls.toString().length>16){
s = ls.toString();
}
else{
s = s.substr(0,17);
}
}
trace(s, "= string value after checks");
return Number(s);
}
trace(floatSumV3(false,999999999999990.9,.09,.009,0.0009,1,0.2,0.01,0.01,0.01,10000000.00000001));
trace(floatSumV3(true,
999999999999990.9,.09,.009,0.0009,1,0.2,0.01,0.01,0.01,10000000.00000001));
On 12/15/2010 2:31 PM, Anthony Pace wrote:
Oh yeah, I forgot to give some examples to test it with, so...
trace(floatSum(-0.1,0.9155,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1));
//vs
trace(-0.1+0.9155+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1);
//and
trace(floatProduct(2.1,0.1,1.0001,.0001));
//vs
trace(2.1*0.1*1.0001*.0001);
On 12/15/2010 2:25 PM, Anthony Pace wrote:
I wrote the functions below in literally just a few min, so, even
though they do seem to work for me, I wouldn't necessarily say they
are production ready.
Also, I would only use these if you will know that the numbers past
the mantissa are of small amounts.
Oh yeah, and please tell me if I screwed up somewhere, or if you
think you can do it better :)
function floatProduct(... nums){
var ml:int = 0; //max length past mantissa
var ln:uint = nums.length;
var rsl:int; //right side length
var p:Number = 1;
var s:String;
for (var i:uint = 0; i< ln; ++i) {
s = nums[i].toString();
rsl = s.length - s.indexOf('.');
ml += rsl;
p *= nums[i];
}
return Number(p.toFixed(ml));
}
function floatSum(... nums){
var ml:int = 0; //max length past mantissa
var ln:uint = nums.length;
var rsl:int; //right side length
var p:Number = 0;
var s:String;
for (var i:int = 0; i< ln; ++i) {
s = nums[i].toString();
rsl = s.length - s.indexOf('.');
if (rsl > ml) {
ml = rsl;
}
p += nums[i];
}
return Number(p.toFixed(ml));
}
-
On 12/14/2010 12:05 PM, Kerry Thompson wrote:
Adrian ZajÄ…c wrote:
trace (0.27 + 0.03); // output --> 0.30000000000000004
Can anyone tell me why I get this weird result in output window?
As people have said, it's a problem with decimals. It's not a
problem with
Flash--it's a problem with binary numbers.
Integers are accurate because all integers are a multiple of 1, and
binary
does just fine with combining 0's and 1's, the only possibile values
of a
digit in the binary system.
If you think of decimals as fractions, maybe it will help you
understand the
issue. In binary, a digit can only be a multiple of 2: 1/2, 1/4,
1/8/ 1/16,
1/32, and so on. You can create numbers that aren't multiples of two by
adding multiple digits. For example, 1/4 + 1/8 = 3/8, or .375.
This works pretty well, until you get numbers with a lot of decimal
places.
At some point, you will find a decimal that is impossible to make using
powers of 2.
Cordially,
Kerry Thompson
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders