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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:46:02+00:00 2026-06-08T17:46:02+00:00

i have two double arrays, let’s say A and B. i want to compare

  • 0

i have two double arrays, let’s say A and B. i want to compare their results to 7 significant digits. will the following be correct to make the comparison?

k = pow(10,7);
for(...)
{
 if(((int)A[i]*k)!=((int)B[i]*k))
 {
  ...
 }
}
  • 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-08T17:46:04+00:00Added an answer on June 8, 2026 at 5:46 pm

    No, this will not work.

    The type cast operator has higher precedence than the multiplication operator. This means that A[i] and B[i] will be cast to integers (and be truncated) before being multiplied by 1e7. 2.25 and 2.5 will end up being equal to your code. You can fix that by putting the multiplication in parentheses: (int)(A[i]*k)

    Also, since you’re relying on truncation instead of rounding, you may end up with incorrect results (depending on what you’re expecting). 1.0e-7 and 1.9e-7 will be equal (1 == 1), while 1.9e-7 and 2.1e-7 will not (1 != 2). I suggest finding a function that will round properly with the behavior you desire.

    Also, your comparison does not deal with significant digits, it simply changes the value of the exponent. In the above examples, there are only 2 significant digits, however your code would only compare one of those digits because the value of the exponent is -7.

    Here is some code that does what you want:

    //create integer value that contains 7 significant digits of input number
    int adjust_num(double num) {
        double low_bound = 1e7;
        double high_bound = low_bound*10;
        double adjusted = num;
        int is_negative = (num < 0);
        if(num == 0) {
            return 0;
        }
        if(is_negative) {
            adjusted *= -1;
        }
        while(adjusted < low_bound) {
            adjusted *= 10;
        }
        while(adjusted >= high_bound) {
            adjusted /= 10;
        }
        if(is_negative) {
            adjusted *= -1;
        }
        //define int round(double) to be a function which rounds
        //correctly for your domain application.
        return round(adjusted);
    }
    
    ...
    
    if(adjust_num(A[i]) == adjust_num(B[i])) {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let say we have two arrays: DateTime[] wDates = new DateTime[20000]; double[] wValues =
Let's say I have two maps: typedef int Id; std::map<Id, std::string> idToStringMap; std::map<Id, double>
I have the following method to swap two double arrays (double**) in c++. Profiling
Assume I have two vectors represented by two arrays of type double , each
I have two arrays values and keys both of the same length. I want
I have two Arrays that are initialized as such: public static double[] arrayweight= new
I have two arrays mat1 & Mat2. I want to have new_mat=[ma1,mat2]; I have
I have two arrays double[] pole=new double[100]; XSSFRichTextString[] pole1=new XSSFRichTextString[100]; array pole I filled
I'm looking to slice a two dimensional array in C#. I have double[2,2] prices
I have two classes set up as follows: class Point { protected: double coords[3];

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.