Pergunta

I am doing some c++ right now and stumbled on a problem I can't get to wrap my head around.

I am doing a floating point comparison like this:

if(dists.at<float>(i,0) <= 0.80 * dists.at<float>(i,1)) {
  matched = true;
  matches++;
} else {
  printf((dists.at<float>(i,0) <= 0.80 * dists.at<float>(i,1)) ? "true\n" : "false");
  printf("threw one away because of dist: %f/%f\n", dists.at<float>(i,0),dists.at<float>(i,1));
}

at the first line, the comparison threw a false what means: dists[0] > dists[1] When we print the values, the results are this:

falsethrew one away because of dist: 0.000000/0.000000
falsethrew one away because of dist: 0.000000/0.000000

I think it has something to do with the results not being a float or something, but I'm no pro at C++ so I could use some help to find out what these values are.

Foi útil?

Solução

Looks like it was due to the number being too small.

In response to @NirMH, I am posting my comment as an answer.

%g instead of %f can print very small floating point numbers.

There are other options such as %e, as @ilent2 mentioned in the comment.

For example:

double num = 1.0e-23;
printf("%e, %f, %g", num, num, num);

prints out the following result:

1.000000e-023, 0.000000, 1e-023
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top