Float arithmetics

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdu adil
    New Member
    • Nov 2011
    • 2

    Float arithmetics

    Im writing a program which uses simple neural network perception algorithm, I have problem with doing negative float arithmetic, when I do this

    summ = x1*w1+x2*w2-t

    summ gets the value of -2.77555756156e-17

    this happens only when x1 = 1.0 w1 = 0.2, x2 = 0.0 w2 = 0.1 t=0.2. If i do this in the python console everything is fine but in the program it gives me that strange value.

    here is the full code http://tinypaste.com/1b8f9dd6 the calculation is done at the top, in the first function.

    Btw Im using python 2.7, Im confused as hell
  • abdu adil
    New Member
    • Nov 2011
    • 2

    #2
    I found whats wrong, basically this is normal for float, what should I have done is to round the summ to precision 1, so round(summ,1) it will give me 0.0 in sted of that 'small' zero value

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      This phenomenon is explained in the Python documentation on Floating Point Arithmetic.

      Comment

      Working...