English 中文(简体)
如何在不超支的情况下(如果可能的话)用固定点合理的≥1来划分非负面变量的分类?
原标题:How to divide a nonnegative variable integer by a constant fixed-point rational ≥1 without overflow (at all, if possible)?

如何计算⌊? ? 投入吗? ∈ [0, INT_import] 储存在int的变量中? 是否给出的字面是“精干”的字面,其形式是:? 此处为数学司。 我们的榜样是⌊?÷65781.76⌋。 如何在C中适当做到这一点?

http://www.h-schmidt.net/FloatConverter/IEEE754.html http://www.h-schmidt.net/FloatConverter/IEEE754.html,65781.76,作为最常数,至少不能在我的机器上代表,而100*?/6578176L 25*? 1644544L 过多的风险。 无论如何,根据我所知,添加suffix L或L(例如,25L*?1644544L)是毫无意义的,因为<代码>长期longint没有保证有不同的宽限期。 任何其他选择? 我对涉及纯算术和(最重要的是)适当解释的解决办法感兴趣。

PS。 理想的情况是,我们想要找到一个准确和可行的解决办法;如果可能的话,请在C89-C90周围的某个地方没有外部图书馆。

PPS。 我从问题中删除了<条码>。

问题回答

这里,一种由您的原始分类方法:

            x
--------------------------
              7227
      65536 * ----
               100
      ------------
           72
           72x
--------------------------
              7227
      65536 * ----
               100
           7200x
--------------------------
        473628672
if(x <= INT_MAX / 7200)                 // no overflow will happen
    res = 7200 * x / 473628672;
else                                    // overflow would happen
    res = x / (473628672 / 7200);

当然,如果将<条码>x与7200条相乘,那么你可以考虑在这些特殊情况下不通过<条码>473628672 / 7200(<条码>65781)加以区别,但你实际上通过<条码><473628672.0 / 7200.0(<条码>65781.76)加以区别。

在正常的、非流出的个案中,这种情况将是先兆。

采用64个轨道分类,将给您<代码>[0, 2562047788015215],如果您的<代码>int为32 bit(最有可能是)的话,它将涵盖这一全方位,而不采用备份解决办法:

unsigned long long calc(unsigned long long x) {
    if(x <= ULLONG_MAX / 7200ull)
        return 7200ull * x / 473628672ull;
    else // backup
        return (long double)x / (473628672.0L / 7200.0L);
}

How to divide a nonnegative variable integer by a positive constant float without risking too much of an overflow?

你重新考虑了这一问题。 工作方式是利用菲律宾武装部队的普通司。 你对此表示反对的理由是,你的分裂者在双轨浮动点并不确切地代表,但精准火车一经四舍五入到65536 * 7227/72至7名重要精度数,就离开了该站。

确实,如果您使用<代码>杜布尔,而不是使用<代码>float。 然后,你可以就理想的65536*72.27/72红利获得more的准确结果,然后,你可以采取从6578176位数中位数开始的任何办法。 在你可能会见的任何机器上,每台<条码>杜布莱有53条门槛,所有大小,以及<条码>,最多有32条精确度线,只有少数几条重要字面(无重大意)。 此外,鉴于您的红利之大,您的引人最多将拥有16个重要领域。

Just do the equivalent of this:

const double scale_factor = 65536 * 72.27 / 72;

int compute result(int x) {
    return x / scale_factor;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签