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 7089099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:52:52+00:00 2026-05-28T07:52:52+00:00

First off, I’m not a math guy, so large number precision rarely filters into

  • 0

First off, I’m not a math guy, so large number precision rarely filters into my daily work. Please be gentle. 😉

Using NumPy to generate a matrix with values equally divided from 1:

>>> m = numpy.matrix([(1.0 / 1000) for x in xrange(1000)]).T
>>> m
matrix[[ 0.001 ],
       [ 0.001 ],
       ...
       [ 0.001 ]])

On 64-bit Windows with Python 2.6, summing rarely works out to 1.0. math.fsum() does with this matrix, it doesn’t if I change the matrix to use smaller numbers.

>>> numpy.sum(m)
1.0000000000000007
>>> math.fsum(m)
1.0
>>> sum(m)
matrix([[ 1.]])
>>> float(sum(m))
1.0000000000000007

On 32-bit Linux (Ubuntu) with Python 2.6, summing always works out to 1.0.

>>> numpy.sum(m)
1.0
>>> math.fsum(m)
1.0
>>> sum(m)
matrix([[ 1.]])
>>> float(sum(m))
1.0000000000000007

I can add an epsilon to my code when assessing if the matrix sums to 1 (e.g. -epsilon < sum(m) < +epsilon) but I want to first understand what the cause of the difference is within Python, and if there’s a better way to determine the sum correctly.

My understanding is that the sum(s) are processing the machine representation of the numbers (floats) differently than how they’re displayed, and when sum’ing, the internal repesentation is used. Howeve,r looking at the 3 methods I used to calculate the sum it’s not clear why they’re all different, or the same between the platforms.

What’s the best way to correctly calculate the sum of a matrix?

If you’re looking for a more interesting matrix, this simple change will have smaller matrix numbers:

>>> m = numpy.matrix([(1.0 / 999) for x in xrange(999)]).T

Thanks in advance for any help!

Update
I think I figured something out. If I correct the value being stored to a 32-bit float the results match the 32-bit Linux sum’ing.

>>> m = numpy.matrix([(numpy.float32(1.0) / 1000) for x in xrange(1000)]).T
>>> m
matrix[[ 0.001 ],
       [ 0.001 ],
       ...
       [ 0.001 ]])
>>> numpy.sum(m)
1.0

This will set the matrix machine numbers to represent 32-bit floats, not 64-bit on my Windows test, and will sum correctly. Why is a 0.001 float not equal as a machine number on a 32-bit and 64-bit system? I would expect them to be different if I was trying to store very small numbers with lots of decimal places.

Does anyone have any thoughts on this? Should I explicitly switch to 32-bit floats in this case, or is there a 64-bit sum’ing method? Or am I back to adding an epsilon? Sorry if I sound dumb, I’m interested in opinions. Thanks!

  • 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-28T07:52:52+00:00Added an answer on May 28, 2026 at 7:52 am

    It’s because you’re comparing 32-bit floats to 64-bit floats, as you’ve already found out.

    If you specify a 32-bit or 64-bit dtype on both machines, you’ll see the same result.

    Numpy’s default floating point dtype (the numerical type for a numpy array) is the same as the machine precision. This is why you’re seeing different results on different machines.

    E.g.
    The 32-bit version:

    m = numpy.ones(1000, dtype=numpy.float32) / 1000
    print repr(m.sum())
    

    and the 64-bit version:

    m = numpy.ones(1000, dtype=numpy.float64) / 1000
    print repr(m.sum())
    

    Will be different due to the differing precision, but you’ll see the same results on different machines. (However, the 64-bit operation will be much slower on a 32-bit machine)

    If you just specify numpy.float, this will be either a float32 or a float64 depending on the machine’s native architecture.

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

Sidebar

Related Questions

First off, I am not a Unix expert by any stretch, so please forgive
First off, let me start off that I am not a .net developer. The
First off, I'm not terribly experienced in XML. I know the very basics of
First off, please accept my apologies if this question is basic, I mainly have
First off, I'm a real beginer at C# so please be gentle. I'm trying
First off, I am cross-posting this from Server Fault because I did not receive
First off, I'm using an older version of Restlet (1.1). Secondly, I'm not sure
First off, I'm not all that familiar with cookies but I know how they
First off, the mousewheel event is not listed in Visual Studio 2008's events pane
First off, let me say that I am familiar with content_for . It's not

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.