I don't know the reason for this output("False"). Please explain.  sum=0 for i in range(10):     sum+=0.1 print (sum==1.0)
27
Consider the following code:   0.1 + 0.2 == 0.3  -&gt;  false   0.1 + 0.2         -&gt;  0.30000000000000004   Why do these inaccuracies happen?
3799
How do I access the index in a for loop like the following?  ints = [8, 23, 45, 12, 78] for i in ints:     print('item #{} = {}'.format(???, i))   I want to get this output:  item #1 = 8 item #2 = 23 item #3 = 45 item #4 = 12 item #5 = 78   When I loop through it using a for loop, how do I access the loop index, from 1 to 5 in this case?
1704
This has always confused me. It seems like this would be nicer:  my_list = ["Hello", "world"] print(my_list.join("-")) # Produce: "Hello-world"   Than this:  my_list = ["Hello", "world"] print("-".join(my_list)) # Produce: "Hello-world"   Is there a specific reason it is like this?
76762
I have written a Python 3 code, which is:  i = int(input("enter number: ")) print("enter",i)   I'm expecting an output as:  enter number: 5 enter 5   However, I'm getting this:  enter number: 5 ('enter', 5)   What could be the issue? How can I rectify this?
9476
Imagine you have a large array of floating point numbers, of all kinds of sizes. What is the most correct way to calculate the sum, with the least error? For example, when the array looks like this:  [1.0, 1e-10, 1e-10, ... 1e-10.0]   and you add up from left to right with a simple loop, like  sum = 0 numbers.each do |val|     sum += val end   whenever you add up the smaller numbers might fall below the precision threshold so the error gets bigger and bigger. As far as I know the best way is to sort the array and start adding up numbers from lowest to highest, but I am wondering if there is an even better way (faster, more precise)?  EDIT: Thanks for the answer, I now have a working code that perfectly sums up double values in Java. It is a straight port from the Python post of the winning answer. The solution passes all of my unit tests. (A longer but optimized version of this is available here )  /**  * Adds up numbers in an array with perfect precision, and in O(n).  *   * @see http://code.activestate.com/recipes/393090/  */ public class Summarizer {      /**      * Perfectly sums up numbers, without rounding errors (if at all possible).      *       * @param values      *            The values to sum up.      * @return The sum.      */     public static double msum(double... values) {         List&lt;Double&gt; partials = new ArrayList&lt;Double&gt;();         for (double x : values) {             int i = 0;             for (double y : partials) {                 if (Math.abs(x) &lt; Math.abs(y)) {                     double tmp = x;                     x = y;                     y = tmp;                 }                 double hi = x + y;                 double lo = y - (hi - x);                 if (lo != 0.0) {                     partials.set(i, lo);                     ++i;                 }                 x = hi;             }             if (i &lt; partials.size()) {                 partials.set(i, x);                 partials.subList(i + 1, partials.size()).clear();             } else {                 partials.add(x);             }         }         return sum(partials);     }      /**      * Sums up the rest of the partial numbers which cannot be summed up without      * loss of precision.      */     public static double sum(Collection&lt;Double&gt; values) {         double s = 0.0;         for (Double d : values) {             s += d;         }         return s;     } }
97319
Can you please explain why \begin{align*} \lim_{x\to 0^+}{x^{x^x}} &amp;= 0 \end{align*}
5169
In my program I have one array with 25 double values 0.04 When I try to sum these values in a loop I get following results:  0.0 + 0.04 = 0.04 0.04 + 0.04 = 0.08 0.08 + 0.04 = 0.12 0.12 + 0.04 = 0.16 0.16 + 0.04 = 0.2 0.2 + 0.04 = 0.24000000000000002 0.24000000000000002 + 0.04 = 0.28 0.28 + 0.04 = 0.32 0.32 + 0.04 = 0.36 0.36 + 0.04 = 0.39999999999999997 0.39999999999999997 + 0.04 = 0.43999999999999995 0.43999999999999995 + 0.04 = 0.4799999999999999 0.4799999999999999 + 0.04 = 0.5199999999999999 0.5199999999999999 + 0.04 = 0.5599999999999999 0.5599999999999999 + 0.04 = 0.6 0.6 + 0.04 = 0.64 0.64 + 0.04 = 0.68 0.68 + 0.04 = 0.7200000000000001 0.7200000000000001 + 0.04 = 0.7600000000000001 0.7600000000000001 + 0.04 = 0.8000000000000002 0.8000000000000002 + 0.04 = 0.8400000000000002 0.8400000000000002 + 0.04 = 0.8800000000000002 0.8800000000000002 + 0.04 = 0.9200000000000003 0.9200000000000003 + 0.04 = 0.9600000000000003   Why on earth could that happen?!
72967
When I entered the value $$\frac{1}{i}$$ in my calculator, I received the answer as $-i$ whereas I was expecting the answer as $i^{-1}$. Even google calculator shows the same answer (Click  to check it out).  Is there a fault in my calculator or $\frac{1}{i}$ really equals $-i$? If it does then how?
115153
I know you can't rely on equality between double or decimal type values normally, but I'm wondering if 0 is a special case.  While I can understand imprecisions between 0.00000000000001 and 0.00000000000002, 0 itself seems pretty hard to mess up since it's just nothing.  If you're imprecise on nothing, it's not nothing anymore.  But I don't know much about this topic so it's not for me to say.  double x = 0.0; return (x == 0.0) ? true : false;   Will that always return true?
10523
I know how to do it normally, but I could swear that you could fill out out like a[0] = {0,0,0,0}; How do you do it that way? I did try Google, but I didn't get anything helpful.
12929
Could someone please tell me what I'm doing wrong with this code? It is just printing 'count' anyway.  I just want a very simple prime generator (nothing fancy).   import math  def main():     count = 3     one = 1     while one == 1:         for x in range(2, int(math.sqrt(count) + 1)):             if count % x == 0:                  continue             if count % x != 0:                 print count          count += 1
45648
Since everyone freaked out, I made the variables are the same. $$ \sum_{x=1}^{n} 2^{x-1} $$  I've been trying to find this for a while. I tried the usually geometric equation  but I couldn't get it right (if you need me to post my work I will). Here's the outputs I need:  1, 3, 7, 15, 31, 63  If my math is correct.
4130
Python's  function returns the sum of numbers in an iterable.  sum([3,4,5]) == 3 + 4 + 5 == 12   I'm looking for the function that returns the product instead.  somelib.somefunc([3,4,5]) == 3 * 4 * 5 == 60   I'm pretty sure such a function exists, but I can't find it.
96772
This is a continued discussion from () by return count only.     Problem      Given four lists A, B, C, D of integer values, compute how many tuples   (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.      To make problem a bit easier, all A, B, C, D have same length of N   where \$0 \le N \le 500\$. All integers are in the range of \$-2^{28}\$ to \$2^{28} - 1\$   and the result is guaranteed to be at most \$2^{31} - 1\$.      Example:      Input:  A = [ 1, 2] B = [-2,-1] C = [-1, 2] D = [ 0, 2]       Output:  2       Explanation:      The two tuples are:         (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0   (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0      I'm wondering if there are any ideas to have a solution less than \$O(n^2)\$ time complexity.  Source code in Python 2.7,  from collections import defaultdict def four_sum(A, B, C, D):     sum_map = defaultdict(int)     result = 0     for i in A:         for j in B:             sum_map[i+j] += 1     for i in C:         for j in D:             if -(i+j) in sum_map:                 result += sum_map[-(i+j)]      return result  if __name__ == "__main__":     A = [1, 2]     B = [-2, -1]     C = [-1, 2]     D = [0, 2]     print four_sum(A,B,C,D)
17463
Why is ++i is l-value and i++ not?
80880
My code:  a = '2.3'   I wanted to display a as a floating point value.  Since a is a string, I tried:  float(a)   The result I got was :  2.2999999999999998   I want a solution for this problem. Please, kindly help me.  I was following .
92907
I see in many Brazilian sites that, if you get a number and subtract it by its reverse, you will have zero or a multiple of nine. For example:  22 - 22 = 0 51 - 15 = 36 (multiple of 9) 444 - 444 = 0 998 - 899 = 99 (multiple of 9) 1350 - 0531 = 819 (multiple of 9) 654321 - 123456 = 530865 (multiple of 9)   I wrote this Python code to test a range of numbers:   import math  for i in range(1, 1000001):     inverse = int(str(i)[::-1])     result = int(math.fabs(i - inverse))      if result != 0 and result % 9 != 0 :         print(result)   for that range it seems to be true. But I wasn't able to find any similiar type of "mathematical curiosity" in English language sites.  If it is true, is there explanation to that? Because the sites that spread that information, does not provide any explanation.