Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var result = 1.0 + 2.0; // result === 3.0 returns true
- var result = 0.1 + 0.2; // result === 0.3 returns false
- function strip(number) {
- return (parseFloat(number.toPrecision(12)));
- }
- public static string BinaryRepresentation(double value)
- {
- long valueInLongType = BitConverter.DoubleToInt64Bits(value);
- string bits = Convert.ToString(valueInLongType, 2);
- string leadingZeros = new string('0', 64 - bits.Length);
- string binaryRepresentation = leadingZeros + bits;
- string sign = binaryRepresentation[0].ToString();
- string exponent = binaryRepresentation.Substring(1, 11);
- string mantissa = binaryRepresentation.Substring(12);
- return string.Format("{0}:{1}:{2}", sign, exponent, mantissa);
- }
- 0.1 => 0:01111111011:1001100110011001100110011001100110011001100110011010
- 0.2 => 0:01111111100:1001100110011001100110011001100110011001100110011010
- 0.1 = 2^-4 * [1].1001100110011001100110011001100110011001100110011010
- 0.2 = 2^-3 * [1].1001100110011001100110011001100110011001100110011010
- 0.1 = 2^-3 * 0.1100110011001100110011001100110011001100110011001101(0)
- 0.2 = 2^-3 * 1.1001100110011001100110011001100110011001100110011010
- sum = 2^-3 * 10.0110011001100110011001100110011001100110011001100111
- sum = 2^-2 * 1.0011001100110011001100110011001100110011001100110011(1)
- a = 2^-2 * 1.0011001100110011001100110011001100110011001100110011
- x = 2^-2 * 1.0011001100110011001100110011001100110011001100110011(1)
- b = 2^-2 * 1.0011001100110011001100110011001100110011001100110100
- sum = 2^-2 * 1.0011001100110011001100110011001100110011001100110100
- 0.1 + 0.2 => 0:01111111101:0011001100110011001100110011001100110011001100110[100]
- 0.3 => 0:01111111101:0011001100110011001100110011001100110011001100110[011]
- 0.1 + 0.2 => 0.300000000000000044408920985006...
- 0.3 => 0.299999999999999988897769753748...
- function add(a, b, precision) {
- var x = Math.pow(10, precision || 2);
- return (Math.round(a * x) + Math.round(b * x)) / x;
- }
- function getFloat(int) {
- var num = new Number(int);
- return parseFloat(num.toPrecision(2));
- }
- if( (n * 0.1) < 100.0 ) { return n * 0.1 - 0.000000000000001 ;}
- else { return n * 0.1 + 0.000000000000001 ;}
- 0.1 + 0.2 = 0.30000000000000004
- 0.1 + 0.7 = 0.7999999999999999
- ...
- 1.7 + 1.9 = 3.5999999999999996
- 1.7 + 2.2 = 3.9000000000000004
- ...
- 3.2 + 3.6 = 6.800000000000001
- 3.2 + 4.4 = 7.6000000000000005
- 0.6 - 0.2 = 0.39999999999999997
- 0.5 - 0.4 = 0.09999999999999998
- ...
- 2.1 - 0.2 = 1.9000000000000001
- 2.0 - 1.9 = 0.10000000000000009
- ...
- 100 - 99.9 = 0.09999999999999432
- 100 - 99.8 = 0.20000000000000284
- >>> 0.1 + 0.2 == 0.3
- False
- >>> Decimal('0.1') + Decimal('0.2') == Decimal('0.3')
- True
- SIGN EXPONENT FRACTION
- #include <stdio.h>
- #include <limits.h>
- void
- xx(float *x)
- {
- unsigned char i = sizeof(*x)*CHAR_BIT-1;
- do {
- switch (i) {
- case 31:
- printf("sign:");
- break;
- case 30:
- printf("exponent:");
- break;
- case 23:
- printf("fraction:");
- break;
- }
- char b=(*(unsigned long long*)x&((unsigned long long)1<<i))!=0;
- printf("%d ", b);
- } while (i--);
- printf("n");
- }
- void
- yy(float a)
- {
- int sign=!(*(unsigned long long*)&a&((unsigned long long)1<<31));
- int fraction = ((1<<23)-1)&(*(int*)&a);
- int exponent = (255&((*(int*)&a)>>23))-127;
- printf(sign?"positive" " ( 1+":"negative" " ( 1+");
- unsigned int i = 1<<22;
- unsigned int j = 1;
- do {
- char b=(fraction&i)!=0;
- b&&(printf("1/(%d) %c", 1<<j, (fraction&(i-1))?'+':')' ), 0);
- } while (j++, i>>=1);
- printf("*2^%d", exponent);
- printf("n");
- }
- void
- main()
- {
- float x=-3.14;
- float y=999999999;
- printf("%lun", sizeof(x));
- xx(&x);
- xx(&y);
- yy(x);
- yy(y);
- }
- -- .../terra1/stub
- @ qemacs f.c
- -- .../terra1/stub
- @ gcc f.c
- -- .../terra1/stub
- @ ./a.out
- sign:1 exponent:1 0 0 0 0 0 0 fraction:0 1 0 0 1 0 0 0 1 1 1 1 0 1 0 1 1 1 0 0 0 0 1 1
- sign:0 exponent:1 0 0 1 1 1 0 fraction:0 1 1 0 1 1 1 0 0 1 1 0 1 0 1 1 0 0 1 0 1 0 0 0
- negative ( 1+1/(2) +1/(16) +1/(256) +1/(512) +1/(1024) +1/(2048) +1/(8192) +1/(32768) +1/(65536) +1/(131072) +1/(4194304) +1/(8388608) )*2^1
- positive ( 1+1/(2) +1/(4) +1/(16) +1/(32) +1/(64) +1/(512) +1/(1024) +1/(4096) +1/(16384) +1/(32768) +1/(262144) +1/(1048576) )*2^29
- -- .../terra1/stub
- @ bc
- scale=15
- ( 1+1/(2) +1/(4) +1/(16) +1/(32) +1/(64) +1/(512) +1/(1024) +1/(4096) +1/(16384) +1/(32768) +1/(262144) +1/(1048576) )*2^29
- 999999999.999999446351872
- 999999999.999999446351872
- <?php
- $a = 1.23456789;
- $b = 1.23456780;
- $epsilon = 0.00001;
- if(abs($a-$b) < $epsilon) {
- echo "true";
- }
- var foo = 0.1;
- var bar = 0.2;
- function add(foo, bar, precision){
- return parseFloat((foo + bar).toFixed(precision));
- }
- var result = myBinaryFunction (); // The result contains **0.06999999999999999**
- myBinaryFunction (){
- var x = 0.01 + 0.06;
- return x;
- }
- var result = myParsedFunction() // The result contains 0.07
- function myParsedFunction() {
- var x = parseFloat(0.01) + parseFloat(0.06);
- return x.toFixed(2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement