2

I am looking for inverse double values

if( temp1.p[j] == -temp2.p[j] ) {
    cout << "Inverse Value \n";
}

Problem I have is that when I multiple the following value they are not always completely exact for instance the last 3 digits.

temp2.p[j] =   0.91217251125729792
-temp2.p[j] = -0.91217251124879162 

Now I suspect it has something to do with the precision of doubles and the operation of sign bit but my question is what is the quickest solution?

I thought it would be easy to choose a precision of a double but this seems to only be the case when printing. So do I need to convert the values to strings in order to compare or is there a quicker method?

codem
  • 283
  • 1
  • 2
  • 9
  • 2
    Floating-point numbers are inaccurate. To get around that, you need a different solution, depending on your exact needs. For example, checking if |x1-x2| – Ophir Gvirtzer Jun 10 '15 at 10:29
  • 1
    Sorry, but I just don't believe the `temp2.p[j]` and `-temp2.p[j]` values you list could be output for a `double`s unless it was modified between the generation of those outputs; your values vary after only 10 significant digits, and double's normally good for at least 15 (and in this case where only the sign bit need be touched I'd expect most implementations to be precise). Please show us your actual code demonstrating a mismatch and high precision output for the values (e.g. with `std::fixed << std::setprecision(17)`). – Tony Delroy Jun 10 '15 at 10:36
  • great thanks for that, also sorry for duplication I did try a search and I could find what I was looking for. – codem Jun 10 '15 at 10:39
  • 1
    @OphirGvirtzer `|x1-x2| – Tony Delroy Jun 10 '15 at 10:45
  • @Tony D Your improvement of the standard hack can be useful sometimes, but not always. think of (1.0/1e-8)*1e08 and (1.0/1e-9)*1e09. The standard hack is useful in very restricted sections of code where the delta can be tailored to the expected values. – Ophir Gvirtzer Jun 10 '15 at 11:07
  • @OphirGvirtzer: quite so - I'm not saying "my" suggestion is "useful" or necessary *all* the time; my aim was just to warn readers that `|x1-x2| – Tony Delroy Jun 10 '15 at 11:14

0 Answers0