Skip to content

Instantly share code, notes, and snippets.

@shimondoodkin
Created November 15, 2014 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shimondoodkin/f2960afc402938ef55a7 to your computer and use it in GitHub Desktop.
Save shimondoodkin/f2960afc402938ef55a7 to your computer and use it in GitHub Desktop.
try take advantage of inaccurate large exponent of double floating point to compare decimal numbers
//try take advantage of inacurate large exponent of double floating point to compare decimal numbers.
var large_detable_float=function(point){ if(point){this.maxfloat=parseFloat("1"+Array((Number.MAX_SAFE_INTEGER*point).toFixed(0).length+1).join("0"));}this.a=0; this.b=0;};
large_detable_float.prototype={
maxfloat:100000000,
add:function(a){ if(a<0) return this.sub(-a);
if(this.a+a<this.maxfloat)this.a+=a;
else {this.a-=this.maxfloat;this.b+=this.maxfloat;this.a+=a;}
},
sub:function(a){ if(a<0) return this.add(-a);
if(this.a-a>-this.maxfloat)this.a-=a;
else {this.a+=this.maxfloat;this.b-=this.maxfloat;this.a-=a;}
},
delta:function(other){
return (this.b-other.b)+(this.a-other.a);
}
}
/*
var delta=
{
a:new large_detable_float(0.00000001 ),
b:new large_detable_float(0.00000001 )
}
delta.a.add(1)
delta.b.add(1)
delta.a.delta(delta.b);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment