I need to do simple math calculations in Python 2.7 with sums, subtractions, divisions, multiplications, sums over lists of numbers, etc.
I want to write elegant, bulletproof and efficient code, but I must admit that I was confused by several things, for example:
- If I have
1/(N-1)*x in my equation, I should just encode 1/(N-1)*x or possibly 1.0/(N-1)*x , 1.0/(N-1.0)*x or any other combination of them? - for division, should I use
// or / with from __future__ import division ? - What methods, such as "using
math.fsum() to concatenate a list of floats", are there? - Should I assume that the input numbers are float or do the conversion just in case (perhaps a risk of performance degradation in many
float(x) operations)?
So what are the best code writing methods for simple math calculations in Python which
- Elegant / Pythonic,
- effective
- bulletproof problems like uncertainty about exact input types (float vs integer)?
source share