Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7012839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:18:29+00:00 2026-05-27T22:18:29+00:00

I need to perform simple mathematical calculations in Python 2.7 with sums, subtractions, divisions,

  • 0

I need to perform simple mathematical calculations in Python 2.7 with sums, subtractions, divisions, multiplications, sums over lists of numbers etc.

I want to write elegant, bullet-proof, and efficient code but I must admit I got confused by several things, for example:

  • if I have 1/(N-1)*x in my equation should I just code 1/(N-1)*x or maybe 1.0/(N-1)*x, 1.0/(N-1.0)*x or any other combination of these?
  • for division, should I use // or / with from __future__ import division?
  • what practices such as “using math.fsum() for concatenating a list of floats” are out there?
  • should I assume that input numbers are float or do the conversion just in case (maybe risking drop of efficiency on many float(x) operations)?

So what are the best practices for writing a code for simple mathematical calculations in Python that is

  1. elegant/Pythonic,
  2. efficient,
  3. bullet-proof to issues like uncertainty in exact number type of input data (float vs integer) ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:18:29+00:00Added an answer on May 27, 2026 at 10:18 pm
    1. If you use Python 2.7, ALWAYS use from __future__ import division. It removes a hell of a lot confusion and bugs.

      With this you should never have to worry if a division is a float or not, / will always be a float and // will always be an int.

    2. You should convert your input with float(). You will do it only once, and it won’t be much of a performance hit.

    3. I would get the sum of a list of floats like this: sum(li, 0.0), but if precision is required, use math.fsum which is specifically created for this.

    4. And finally, your final statement was confusing. Did you mean 1/((N-1)*x) or (1/(N-1))*x? In the first case I would write it as 1 / (x * (N-1)) and in the second case x / (N-1). Both assume 3.x style division.

    Also, look into numpy if you want some real performance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple task I need to perform in Python, which is to
I need to perform a range of simple calculations on an Invoice model object,
I have a pretty simple SQL I need to perform. I have a ProcessUser
When I write math in LaTeX I often need to perform simple arithmetic on
In VIM, I need to perform a simple task - highlight ( and ).
I need a gem to perform simple string encryption and decryption in Ruby for
I need to perform a very simple operation on the database - put some
I'm new to audio analysis, but need to perform a (seemingly) simple task. I
Interlocked.Increment seems like among the most standard/simple of operations one would need to perform
Sometimes we need to perform small administrative tasks in SharePoint. A simple PowerShell script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.