1

:

0.3-0.2-0.1 = -2.775558e-17 # in R3.4 / Python 3.5

While

0.4-0.2-0.1-0.1 = 0.

I am very confused now. I am aware of different storage types in both language. I came on this issue because, I needed to determine if a numeric variable (as a result of different arithmetic operations) is positive or negative.

I need to figure out a reliable way to check the sign of numeric variable.

M--
  • 25,431
  • 8
  • 61
  • 93
rcodo
  • 11
  • 1
  • Here your values both are neither positive nor negative, what do you want to return in such cases? – cglacet Mar 21 '19 at 20:24
  • Try using the **Decimal** class: >>> from decimal import Decimal >>> float(Decimal("0.3") - Decimal("0.2") - Decimal("0.1")) 0.0 – Sash Sinha Mar 21 '19 at 20:34
  • If you need to have an answer about this "I need to figure out a reliable way to check the sign of numeric variable." then you should probably ask a question about this specific point. – cglacet Mar 21 '19 at 20:44
  • I usually define a very small constant `epsilon = 0.000000001` and then say that if `x - epsilon < 0` then x is negative, and if `x + epsilon > 0` then x is positive. – MatrixManAtYrService Jan 03 '20 at 17:07

0 Answers0