Created
November 15, 2014 23:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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