简体   繁体   English

计算 integer 边界以包括缩放的浮点值

[英]Compute integer bounds to include scaled floating point values

I am trying to compute integer array bounds that will include floating point limits divided by a scale.我正在尝试计算 integer 数组边界,其中包括浮点限制除以比例。 For example, if my origin is 0, my floating point maximum is 10 then my integer array bounds need to be 2. The obvious formula is to divide my bounds by the scale, giving the incorrect result of 1.例如,如果我的原点是 0,我的浮点最大值是 10,那么我的 integer 数组边界需要为 2。显而易见的公式是将我的边界除以比例,给出错误的结果 1。

I need to divide the inclusive maximum values by the scale and add one if the division is an exact multiple.我需要将包含的最大值除以比例,如果除法是精确的倍数,则添加一个。

I am running into a mismatch between the normal way to define and use integer array indexes and my desired way to use real value coordinates.我在定义和使用 integer 数组索引的正常方式与使用实值坐标的所需方式之间遇到了不匹配。 I am trying to map inclusive real value coordinates into integer array indexes, using a scaling term.我正在尝试使用缩放项将 map 包含实值坐标转换为 integer 数组索引。

(I am actually working with two dimensional maps, but the problem can be expressed more simply in one dimension.) (我实际上是在使用二维地图,但问题可以更简单地用一维来表达。)

This is wrong:这是错误的:

    int get_array_size(double, scale, double maximum)
    {
       return std::ceil(maximum / scale); // Fails on exact multiples
    }

This is wasteful:这是浪费:

    int get_array_size(double, scale, double maximum)
    {
       return 1 + std::ceil(maximum / scale); // Allocates extra array memory
    }

This is ugly and I am not sure if it is correct:这很丑陋,我不确定它是否正确:

    int get_array_size(double, scale, double maximum)
    {
       if (maximum % scale == 0) // I am not sure if this is correct
          return 1 + std::ceil(maximum / scale);
       else
          return std::ceil(maximum / scale); // Maybe I can eliminate the call to std::ceil?
    }

I am trying to get the value maximum / scale on every open ended interval ending at multiples of scale and 1 + maximum / scale on every interval from >= multiple of scale ending at < multiple of scale + 1. I am not sure how to correctly express this in mathematical terms or how to implement it in c++.我试图在每个以刻度倍数结束的开放式间隔上获得最大值/刻度值,并且每个间隔上的 1 + 最大值/刻度从 >= 刻度倍数以 < 刻度倍数 + 1 结束。我不知道如何用数学术语正确表达这一点或如何在 c++ 中实现它。 I would be grateful if someone can clarify my understand and point me in the right direction.如果有人能澄清我的理解并指出正确的方向,我将不胜感激。

Mathematically I think I am trying to define f(x, s) = y st if s * n <= x and x < s * (n + 1) then y = n + 1. I want to implement this efficiently and respect the difference between <= and < comparison.从数学上讲,我想我正在尝试定义 f(x, s) = y st if s * n <= x and x < s * (n + 1) then y = n + 1。我想有效地实现这一点并尊重<= 和 < 比较之间的区别。

The way I interpret this question, I think maximum and scale don't actually matter - what you are really asking about is how to correctly map from floats to ints with specific boundary conditions.我解释这个问题的方式,我认为maximumscale实际上并不重要 - 你真正要问的是如何正确地将 map 从浮点数转换为具有特定边界条件的整数。 For example [0.0, 1.0) to 0, [1.0, 2.0) to 1, etc. So the question becomes a bit simpler if we just consider maximum / scale to be a single quantity;例如 [0.0, 1.0) 到 0,[1.0, 2.0) 到 1 等。所以如果我们只将maximum / scale视为单个数量,问题就会变得更简单一些; I'll call it t .我称之为t

I believe you actually want to use std::floor instead of std::ceil :我相信您实际上想使用std::floor而不是std::ceil

int scaled_coord_to_index(float t) {
    return std::floor(t);
}

And the size of your array should always be the maximum scaled coordinate + 1 (with negative values normalized to start at 0).并且数组的大小应始终为最大缩放坐标 + 1(负值标准化为从 0 开始)。

int array_size(float min_t, float max_t) {
    // NOTE: This will "anchor" your coords based on the most negative value.
    //       e.g. if that value is 1.6, then your bins will be [1.6, 2.6), [2.6, 3.6), etc.
    //       To change that behavior you could use std::floor(min_t) instead.
    return scaled_coord_to_index(max_t - min_t) + 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM