| 423 | } |
| 424 | |
| 425 | bool isRealSimilar(long double number_1, long double number_2) |
| 426 | { |
| 427 | // Note: The original version of the stuff below was copied from |
| 428 | // FuzzyStringComparator and then heavily modified for ClassTest. |
| 429 | // But still the case distinctions should be similar. |
| 430 | |
| 431 | absdiff = 0.; |
| 432 | ratio = 0.; |
| 433 | fuzzy_message.clear(); |
| 434 | |
| 435 | if (std::isnan(number_1)) |
| 436 | { |
| 437 | fuzzy_message = "number_1 is nan"; |
| 438 | return false; |
| 439 | } |
| 440 | if (std::isnan(number_2)) |
| 441 | { |
| 442 | fuzzy_message = "number_2 is nan"; |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | // check if absolute difference is small |
| 447 | absdiff = number_1 - number_2; |
| 448 | if (absdiff < 0) |
| 449 | { |
| 450 | absdiff = -absdiff; |
| 451 | } |
| 452 | if (absdiff > absdiff_max) |
| 453 | { |
| 454 | absdiff_max = absdiff; |
| 455 | } |
| 456 | // If absolute difference is small, large relative errors will be |
| 457 | // tolerated in the cases below. But a large absolute difference is |
| 458 | // not an error, if relative error is small. We do not jump out of |
| 459 | // the case distinction here because we want to record the relative |
| 460 | // error even in case of a successful comparison. |
| 461 | bool is_absdiff_small = (absdiff <= absdiff_max_allowed); |
| 462 | |
| 463 | if (!number_1) // number_1 is zero |
| 464 | { |
| 465 | if (!number_2) // both numbers are zero |
| 466 | { |
| 467 | fuzzy_message = "both numbers are zero"; |
| 468 | return true; |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | if (!is_absdiff_small) |
| 473 | { |
| 474 | fuzzy_message = "number_1 is zero, but number_2 is not small"; |
| 475 | return false; |
| 476 | } |
| 477 | else |
| 478 | { |
| 479 | fuzzy_message = "number_1 is zero, number_2 is small"; |
| 480 | return true; |
| 481 | } |
| 482 | } |
no test coverage detected