Skip to content

Instantly share code, notes, and snippets.

@jfalcou
Created November 6, 2019 10:09
Show Gist options
  • Save jfalcou/bc0d824a7274b466a814bda3c7287acd to your computer and use it in GitHub Desktop.
Save jfalcou/bc0d824a7274b466a814bda3c7287acd to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
bool ok_to_sum(float a, float b)
{
int exp_a, exp_b;
std::frexp(a, &exp_a);
std::frexp(b, &exp_b);
return std::abs(exp_a - exp_b) <= 23;
}
float r = 12.543f;
float u0 = 12.543f + 0.1f;
float u1 = 12.543f + 0.01f;
float u2 = 12.543f + 0.001f;
float u3 = 12.543f + 0.0001f;
float u4 = 12.543f + 0.00001f;
float u5 = 12.543f + 0.000001f;
float u6 = 12.543f + 0.0000001f;
int main()
{
std::cout << std::boolalpha << ok_to_sum(12.543f,0.1f) << "\n";
std::cout << std::boolalpha << ok_to_sum(12.543f,0.01f) << "\n";
std::cout << std::boolalpha << ok_to_sum(12.543f,0.001f) << "\n";
std::cout << std::boolalpha << ok_to_sum(12.543f,0.0001f) << "\n";
std::cout << std::boolalpha << ok_to_sum(12.543f,0.00001f) << "\n";
std::cout << std::boolalpha << ok_to_sum(12.543f,0.000001f) << "\n";
std::cout << std::boolalpha << ok_to_sum(12.543f,0.0000001f) << "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment