Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. var result = 1.0 + 2.0; // result === 3.0 returns true
  2.  
  3. var result = 0.1 + 0.2; // result === 0.3 returns false
  4.  
  5. function strip(number) {
  6. return (parseFloat(number.toPrecision(12)));
  7. }
  8.  
  9. public static string BinaryRepresentation(double value)
  10. {
  11. long valueInLongType = BitConverter.DoubleToInt64Bits(value);
  12. string bits = Convert.ToString(valueInLongType, 2);
  13. string leadingZeros = new string('0', 64 - bits.Length);
  14. string binaryRepresentation = leadingZeros + bits;
  15.  
  16. string sign = binaryRepresentation[0].ToString();
  17. string exponent = binaryRepresentation.Substring(1, 11);
  18. string mantissa = binaryRepresentation.Substring(12);
  19.  
  20. return string.Format("{0}:{1}:{2}", sign, exponent, mantissa);
  21. }
  22.  
  23. 0.1 => 0:01111111011:1001100110011001100110011001100110011001100110011010
  24. 0.2 => 0:01111111100:1001100110011001100110011001100110011001100110011010
  25.  
  26. 0.1 = 2^-4 * [1].1001100110011001100110011001100110011001100110011010
  27. 0.2 = 2^-3 * [1].1001100110011001100110011001100110011001100110011010
  28.  
  29. 0.1 = 2^-3 * 0.1100110011001100110011001100110011001100110011001101(0)
  30. 0.2 = 2^-3 * 1.1001100110011001100110011001100110011001100110011010
  31. sum = 2^-3 * 10.0110011001100110011001100110011001100110011001100111
  32.  
  33. sum = 2^-2 * 1.0011001100110011001100110011001100110011001100110011(1)
  34.  
  35. a = 2^-2 * 1.0011001100110011001100110011001100110011001100110011
  36. x = 2^-2 * 1.0011001100110011001100110011001100110011001100110011(1)
  37. b = 2^-2 * 1.0011001100110011001100110011001100110011001100110100
  38.  
  39. sum = 2^-2 * 1.0011001100110011001100110011001100110011001100110100
  40.  
  41. 0.1 + 0.2 => 0:01111111101:0011001100110011001100110011001100110011001100110[100]
  42. 0.3 => 0:01111111101:0011001100110011001100110011001100110011001100110[011]
  43.  
  44. 0.1 + 0.2 => 0.300000000000000044408920985006...
  45. 0.3 => 0.299999999999999988897769753748...
  46.  
  47. function add(a, b, precision) {
  48. var x = Math.pow(10, precision || 2);
  49. return (Math.round(a * x) + Math.round(b * x)) / x;
  50. }
  51.  
  52. function getFloat(int) {
  53. var num = new Number(int);
  54. return parseFloat(num.toPrecision(2));
  55. }
  56.  
  57. if( (n * 0.1) < 100.0 ) { return n * 0.1 - 0.000000000000001 ;}
  58. else { return n * 0.1 + 0.000000000000001 ;}
  59.  
  60. 0.1 + 0.2 = 0.30000000000000004
  61. 0.1 + 0.7 = 0.7999999999999999
  62. ...
  63. 1.7 + 1.9 = 3.5999999999999996
  64. 1.7 + 2.2 = 3.9000000000000004
  65. ...
  66. 3.2 + 3.6 = 6.800000000000001
  67. 3.2 + 4.4 = 7.6000000000000005
  68.  
  69. 0.6 - 0.2 = 0.39999999999999997
  70. 0.5 - 0.4 = 0.09999999999999998
  71. ...
  72. 2.1 - 0.2 = 1.9000000000000001
  73. 2.0 - 1.9 = 0.10000000000000009
  74. ...
  75. 100 - 99.9 = 0.09999999999999432
  76. 100 - 99.8 = 0.20000000000000284
  77.  
  78. >>> 0.1 + 0.2 == 0.3
  79. False
  80. >>> Decimal('0.1') + Decimal('0.2') == Decimal('0.3')
  81. True
  82.  
  83. SIGN EXPONENT FRACTION
  84.  
  85. #include <stdio.h>
  86. #include <limits.h>
  87.  
  88. void
  89. xx(float *x)
  90. {
  91. unsigned char i = sizeof(*x)*CHAR_BIT-1;
  92. do {
  93. switch (i) {
  94. case 31:
  95. printf("sign:");
  96. break;
  97. case 30:
  98. printf("exponent:");
  99. break;
  100. case 23:
  101. printf("fraction:");
  102. break;
  103.  
  104. }
  105. char b=(*(unsigned long long*)x&((unsigned long long)1<<i))!=0;
  106. printf("%d ", b);
  107. } while (i--);
  108. printf("n");
  109. }
  110.  
  111. void
  112. yy(float a)
  113. {
  114. int sign=!(*(unsigned long long*)&a&((unsigned long long)1<<31));
  115. int fraction = ((1<<23)-1)&(*(int*)&a);
  116. int exponent = (255&((*(int*)&a)>>23))-127;
  117.  
  118. printf(sign?"positive" " ( 1+":"negative" " ( 1+");
  119. unsigned int i = 1<<22;
  120. unsigned int j = 1;
  121. do {
  122. char b=(fraction&i)!=0;
  123. b&&(printf("1/(%d) %c", 1<<j, (fraction&(i-1))?'+':')' ), 0);
  124. } while (j++, i>>=1);
  125.  
  126. printf("*2^%d", exponent);
  127. printf("n");
  128. }
  129.  
  130. void
  131. main()
  132. {
  133. float x=-3.14;
  134. float y=999999999;
  135. printf("%lun", sizeof(x));
  136. xx(&x);
  137. xx(&y);
  138. yy(x);
  139. yy(y);
  140. }
  141.  
  142. -- .../terra1/stub
  143. @ qemacs f.c
  144. -- .../terra1/stub
  145. @ gcc f.c
  146. -- .../terra1/stub
  147. @ ./a.out
  148. 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
  149. 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
  150. 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
  151. 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
  152. -- .../terra1/stub
  153. @ bc
  154. scale=15
  155. ( 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
  156. 999999999.999999446351872
  157.  
  158. 999999999.999999446351872
  159.  
  160. <?php
  161. $a = 1.23456789;
  162. $b = 1.23456780;
  163. $epsilon = 0.00001;
  164.  
  165. if(abs($a-$b) < $epsilon) {
  166. echo "true";
  167. }
  168.  
  169. var foo = 0.1;
  170. var bar = 0.2;
  171. function add(foo, bar, precision){
  172. return parseFloat((foo + bar).toFixed(precision));
  173. }
  174.  
  175. var result = myBinaryFunction (); // The result contains **0.06999999999999999**
  176. myBinaryFunction (){
  177. var x = 0.01 + 0.06;
  178. return x;
  179. }
  180.  
  181. var result = myParsedFunction() // The result contains 0.07
  182. function myParsedFunction() {
  183. var x = parseFloat(0.01) + parseFloat(0.06);
  184. return x.toFixed(2);
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement