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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:40:32+00:00 2026-06-08T13:40:32+00:00

Say I have two gmpy2.mpc objects x and y precise to p bits. When

  • 0

Say I have two gmpy2.mpc objects x and y precise to p bits. When I compute x+y it may be that some parts of x and y cancel so I’m left with less precision.

For example

from gmpy2 import *

x = mpc('-0.55555')
y = mpc('0.5555500000001')
print(x+y)

The result is precise to only 4 significant figures even though x and y were precise to ~15.

I think I need to work out how many bits of cancellation occur when I do addition and subtraction and then take this away from the minimum of x or y‘s precision. For multiplication and division I think I will only lose 1 bit of precision at most.

So the question is quite general: how can I keep track of the precision of mpc objects, particularly when adding and subtracting them?

  • 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-06-08T13:40:33+00:00Added an answer on June 8, 2026 at 1:40 pm

    The following function will return the numbers of matching bits of two mpfr objects.

    import gmpy2
    
    def matching_bits(x, y):
        '''Returns the number of bits that match between x and y. The
        sign of x and y are ignored. x and y must be of type mpfr.'''
    
        # Force both values to be positive, and x >= y.
        x = abs(x)
        y = abs(y)
        if x < y:
            x, y = y, x
    
        if not isinstance(x, type(gmpy2.mpfr(0))) or not isinstance(y, type(gmpy2.mpfr(0))):
            raise TypeError("Arguments must be of type 'mpfr'.")
    
        x_bits, x_exp, x_prec = x.digits(2)
        y_bits, y_exp, y_prec = y.digits(2)
    
        # (x_exp - y_exp) is the number of zeros that must be prepended
        # to x to align the mantissas. If that is greater than the precision
        # y, then no bits in common.
        if (x_exp - y_exp) > x_prec:
            return 0
    
        x_bits = "0" * (x_exp - y_exp) + x_bits
    
        count = 0
        while count < min(x_prec, y_prec) and x_bits[count] == y_bits[count]:
            count += 1
        return count
    

    I haven’t extensively tested this function but it should give you a start. You will need to check the real and imaginary components separately. You’ll probably want to modify it to check for sign and whether you are performing addition vs. subtraction.

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

Sidebar

Related Questions

Say I have two Java apps that I wrote: Ping.jar and Pong.jar and they
Say I have two classes and have a requirement that the primary key property
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
Say I have two types of objects, Apples and Chainsaws . A user is
Say I have two static libraries that were not built by me and I
Say I have two type classes defined as follows that are identical in function
Say I have two containers storing pointers to the same objects: std::list<Foo*> fooList; std::vector<Foo*>
Say I have two tables, user and comment . They have table definitions that
Say I have two domain objects and a mapper interface. class Person { int
say I have two files: file1 and file2 is there a single command that

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.