数字的小数部分问题

发布于 2024-07-20 21:13:31 字数 194 浏览 14 评论 0原文

什么是一个好的算法来确定添加/减去数字所需的必要分数,以便将其四舍五入到最接近的整数而不使用内置的上限或下限函数?

编辑:寻找数学数字技巧来找出将数字四舍五入到最接近的整数所需的部分。 数学运算越原始越好。 请避免使用其他程序。 无论哪种方法都可以采用 0.5,只要适合您的方法即可。 这不是我的家庭作业问题,我也不打算在任何地方使用它。

What is a good algorithm to determine the necessary fraction needed to add/sub to the number in order to round it to the nearest integer without using the inbuilt ceiling or floor funcitons?

Edit: Looking for a mathematical number trick to figure out the part needed to round the number to the nearest integer. The more primitive the math operations the better. Please avoid using other's procedures. 0.5 can be taken eitherway, whatever suits your method. This is NOT my homework question, nor am I going to use this anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…红帽 2024-07-27 21:13:31

将数字除以 1 以获得小数部分,如果大于 0.5,则向上舍入,否则向下舍入;

或者

将数字除以 0.5,如果为奇数,则向上舍入,否则向下舍入

Mod the number with one to get the decimal part, if its >0.5, round up, else round down

OR

Divide the number by 0.5, if its odd, round up, else round down

漆黑的白昼 2024-07-27 21:13:31

一旦你得到了数字的小数部分,问题就基本上解决了。 获得小数部分的一种方法是重复从您的数字中减去 2 的幂(假设它已变为正数,如果它一开始是负数)。

下面的函数 getWholeMaker 返回您想要的内容(必须添加以对数字进行四舍五入的“事物”)。 它的运行时间为O(log(n)),并且仅使用基本运算。

/* Returns the factional part of x */
double getFrac(double x) {
    if(x < 0) x = -x;
    if(x < 1) return x;
    else if(x < 2) return x-1;

    /* x >= 0 */
    double t = 2;
    while(t+t <= x) t += t;
    /* t is now the largest power of 2 less than or equal to x */
    while(t >= 1) {
        if(t <= x) x -= t;
        t /= 2;
    }

    return x;
}

double getWholeMaker(double x) {
    double frac = getFrac(x);
    double sign = x >= 0 ? +1 : -1;
    return sign * (frac <= 0.5 ? -frac : 1-frac);
}

Once you have the fractional part of the number, the problem is pretty much solved. One way to get the fractional part is to repeatedly subtract powers-of-2 from you number (assuming it has been made positive, if it were negative to begin with).

The function below, getWholeMaker, returns what you want (the "thing" that must be added to round the number). It's running time is O(log(n)), and uses only elementary operations.

/* Returns the factional part of x */
double getFrac(double x) {
    if(x < 0) x = -x;
    if(x < 1) return x;
    else if(x < 2) return x-1;

    /* x >= 0 */
    double t = 2;
    while(t+t <= x) t += t;
    /* t is now the largest power of 2 less than or equal to x */
    while(t >= 1) {
        if(t <= x) x -= t;
        t /= 2;
    }

    return x;
}

double getWholeMaker(double x) {
    double frac = getFrac(x);
    double sign = x >= 0 ? +1 : -1;
    return sign * (frac <= 0.5 ? -frac : 1-frac);
}
柠北森屋 2024-07-27 21:13:31

如果您不能使用 mod (因为它可能只为您的语言中的整数定义,您也许可以执行类似的操作(在 C 伪代码中):

// make the input positive:
boolean inputPositive = true;
if (input < 0) {
  input = 0 - input;
  inputPositive = false;
}

// subtract 1 until you just have the decimal portion:
int integerPart = 0;
while (input > 1) {
  input = input - 1;
  integerPart++;
}

int ret;
if (input >= 0.5) { // round up
  ret = integerPart + 1;
} else {
  ret = integerPart;
}

if (inputPositive) {
  return ret;
} else {
  return 0 - ret;
}

此解决方案不使用 mod 或任何外部函数。当然,我无法想象你为什么会在现实生活中想要这个,但想想很有趣。

If you can't use mod (because it might only be defined for integers in your language, you might be able to do something like this (in C-ish pseudocode):

// make the input positive:
boolean inputPositive = true;
if (input < 0) {
  input = 0 - input;
  inputPositive = false;
}

// subtract 1 until you just have the decimal portion:
int integerPart = 0;
while (input > 1) {
  input = input - 1;
  integerPart++;
}

int ret;
if (input >= 0.5) { // round up
  ret = integerPart + 1;
} else {
  ret = integerPart;
}

if (inputPositive) {
  return ret;
} else {
  return 0 - ret;
}

This solution doesn't use mod or any outside functions. Of course, I can't imagine why you would want this in real life. It is interesting to think about, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文