Home > database >  Adjust floats to satisfy the condition: abs(float) <= 0.5?
Adjust floats to satisfy the condition: abs(float) <= 0.5?

Time:05-16

I have got a vector of float of an arbitrary size. I would like to adjust the floats so that they satisfy the condition abs(float) <= 0.5. The fractional part should be preserved although it can differ from the original value, thus setting "x = 0.5" is incorrect. If the float is close to integer, discard it (the input float must not be an integer or close to one within an arbitrary precision).

This is what I wrote, but the code looks somewhat branchy. I wonder if there are ways to make it more elegant / efficient or there's something cruical I might have missed.

Input: float

Output: adjusted float x, so that fabs(x) <= 0.5

Examples with integer part

  1. -5.3 -> -0.3 (take the fractional part, if abs(fractional) <= 0.5)
  2. -5.8 -> 0.2 (add the closest integer, rounded upwards, relative to the absolute value).
  3. 5.3 -> 0.3
  4. 5.8 -> -0.2 (substract, but round down)
  5. -5.5 -> -0.5 (sign is preserved here)
  6. 5.5 -> 0.5 (sign is preserved here)

Examples with only fractional part

  1. 0.3 -> 0.3

  2. 0.8 -> -0.2 (substract 1)

  3. -0.3 -> -0.3

  4. -0.8 -> 0.2 (add the 1)

Corner cases

  1. any integer -> 0
  2. 0.5 -> 0.5
  3. -0.5 -> -0.5
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <random>
#include <vector>

float scale(float x)
{
  const auto integerPart = std::abs(static_cast<std::int32_t>(x));
  const auto fractionalPart = std::fabs(x - static_cast<std::int32_t>(x));
  if (x < 0) {
    if (integerPart == 0) {
      if (fractionalPart > 0.5) {
       x  = 1;
      }
    }
    else {
     x  = integerPart   (fractionalPart > 0.5 ? 1 : 0);
    }
  }
  else {
    if (integerPart == 0) {
      if (fractionalPart > 0.5) {
        x -= 1;
      }
    } else {
      x -= integerPart   (fractionalPart > 0.5 ? 1 : 0);
    }
  }
  return x;
}

int main() {
  std::vector<float> floats(10000);

  static std::default_random_engine e;
  static std::uniform_real_distribution<float> distribution(-5, 5);

  std::generate(floats.begin(), 
                floats.end(), 
                [&]() { return distribution(e); });

  for (std::size_t i = 0; i < floats.size();   i) {
    floats[i] = scale(floats[i]);
  }

  std::cout << std::boolalpha 
            << std::all_of(floats.cbegin(),
                           floats.cend(),
                           [&](const float x){ return std::fabs(x) <= 0.5; }) << "\n";

}

Sign preservation is relevant here, if fractional part exceeds 0.5, the sign of resulting value is inverted.

Thank you.

CodePudding user response:

You can use the floor function to reduce the amount of branches:

#include <iostream>
#include <cmath>

float scale(float x) {
    bool neg = std::signbit(x);
    x -= std::floor(x   0.5);
    if (!neg && x == -0.5) {
         return 0.5;
    } else {
         return x;
    }
}

int main() {
    std::cout << "-1.23 " << scale(-1.23) << std::endl;
    std::cout << "-0.5 " << scale(-0.5) << std::endl;
    std::cout << "0.123 " << scale(0.123) << std::endl;
    std::cout << "0.5 " << scale(0.5) << std::endl;
    std::cout << "1.23 " << scale(1.23) << std::endl;
}

-1.23 -0.23
-0.5 -0.5
0.123 0.123
0.5 0.5
1.23 0.23
  • Related