Wonky conditional statement

Hey everyone,

I'm conducting research and need to export a file containing data from one program, rearrange and possibly interpolate, for upload to another program. The data contains values at x1 and y1 points, I have other x2 and y2 points where I need values. My program looks if the x's are equal and y's are equal and stores the data, if not it interpolates. Currently the input file requires no interpolation and contains all the x2 and y2 points required, but only some are stored. When y2 (or r as called in the program) is = 0.3 it is just ignored, along with some other numbers. I've used cout to compare all the values before they are stored, and when they are printed out they show that they are both equal and formatted the same. So the value should be stored, but it isn't.

I thought it was because some zero's where stored as 3e-20 or some pseudo zero, I fixed that however. I've tested it by manually putting in values that are equal right before the if statement that stores, it works then. The rN and zN values give the exact nodes I need (I've checked by printing out), the file I've read in contains all these values, however some just won't trigger the if statement (for example r=0.3).

Using ubuntu, notepad++,and g++ to compile.

Thank you very much for your help, it's been days.

Regards,
Nick

Code: https://1drv.ms/t/s!ArSAc7sTeAlag1yqCHVHPkRCvJsH?e=qR7Abv

This is the data file, it is named "comMag.txt", it is a sample / simple one with no interpolating required. First two columns are garbage, then they are z,r, bz, br, bphi. The part I need help with is when zN == z and rN ==r the if statement isn't entered.

datafile: https://1drv.ms/t/s!ArSAc7sTeAlag1vHVZIHoFGLkWop?e=esQPHZ

Last edited on
I see you are directly comparing floating point values (doubles) for equality.

This is, as a general rule of thumb, fundamentally flawed. Here's a site that you can read to learn all about it , https://floating-point-gui.de/ , with particular reference to https://floating-point-gui.de/errors/comparison/

Let's just say that this:

1
2
3
4
5
6
7
8
9
10
11
float a = 0.1;
float b = 0.2;

if (a+b == 0.3)
{
  cout << "Same";
}
else
{
  cout << "Not same";
}

might not do what you think it should - http://cpp.sh/5j2wi


There are a few quick kludges you can do, such as checking not for equality but that the values are "very close", whatever counts as "close enough" for you.

You can also read something like this - https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ - which goes into detail.
Last edited on
Thank you very much for the information Repeater, I'm glad I can stop trying to find a mistake in the logic or syntax and work on a new solution.
Instead of comparing to see if the input number equals one of the points, with all the comparison problems that involves, just interpolate always. This code reads two points and an x value. It interpolates the corresponding y value. The code assumes that the two points have different x values. If the x values can be the same or very close, then you could compute ratio using the x or y values, depending on whether the difference in x is larger or the difference in y.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

struct Point {
    double x,y;
};

int main()
{
    Point k1, k2;		// known points
    Point u;			// point with unknown y value

    while (cin >> k1.x >> k1.y >> k2.x >> k2.y >> u.x) {
	// interpolate the y value of u
	double ratio = (u.x-k1.x) / (k2.x-k1.x);
	u.y = k1.y + ratio * (k2.y - k1.y);

	cout << u.x << '\t' << u.y << '\n';
    }
}


Topic archived. No new replies allowed.