Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
representability of an unsigned number as a float
bool is_representable_as_float(unsigned v)
{
return v == (unsigned)(float)v;
}
Float contains 23 bits of the mantissa and an implicit one. The float order is outrageous, and even qword won't hit it.
This means that anything further than 23 bits from the upper one must be zero.
bool isPreciseFloat(unsigned long long n)
{
// Простейшая проверка: стираем нижние 24 бита
// Если в них вписываемся — ДА.
unsigned long long n1 = n & ~((1ULL << 24) - 1);
if (n1 == 0)
return true;
// Получаем верхнюю единицу
// (можно также двоичным поиском, но я этого с листа не напишу)
while (true) {
unsigned long long n2 = n1 & (n1 - 1);
if (n2 == 0)
break;
n1 = n2;
}
// Получаем маску всего, что ниже 23 бит от верхнего бита.
n1 >>= 23;
--n1;
// Проверяем по маске
return (n & n1) == 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question