C ++,我的double或int值始终为0 [英] C++, my double or int value is always 0

查看:36
本文介绍了C ++,我的double或int值始终为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我正在为某些图像编辑软件编写的百分比增加方法遇到一些奇怪的行为.

I'm fairly new to C++ and I'm experiencing some strange behaviour from a percentage increase method I am writing for some image editing software.

我想做的是给当前像素的R G或B值,然后将其除以某个修饰符,然后将其乘以新值以返回百分比增加,这很简单.

What I want to do is give the R G or B value of the current pixel and divide it by some modifier, then multiply it by the new value to return the percentage increase, fairly easy concept.

但是,每当我运行调试器时,返回值始终为0,我认为这可能是因为我试图进行给整数赋予负数的操作(或者可能会被零除)吗,所以我尝试使用双精度来存储计算的输出,但是我没有运气.

However, whenever I run my debugger, the return value is always 0, I thought this may be because I was trying to do operations which give negative numbers on an integer (or maybe a divide by zero could occur?), so I tried to use a double to store the output of the computation, however I've had no luck.

我正在苦苦挣扎的代码如下:

The code I'm struggling with is below:

int Sliders::getPercentageIncrease(int currPixel, int newValue, int modifier)
{
    // calculate return value
    double returnVal = (currPixel / modifier) * newValue;

    // Check we are returning a positive integer
    if(returnVal >= 0)
        return (int)returnVal;

    // Return a negative integer value
    return (int)(0 - returnVal);
}

我在做什么错了?

注意:我已经检查了调试器中输入的值,并且得到如下信息:currPixel = 30newValue = 119修饰符= 200

NOTE: I have checked values, of inputs in my debugger and I get stuff like: currPixel = 30 newValue = 119 modifier = 200

由此,我期望输出为18(我不关心返回的十进制数字)

From this I would expect an output of 18 (I am not concerned with returning decimal figures)

推荐答案

您当前的计算仅涉及整数,因此会受到整数除法的影响(将结果截断为最接近的整数值).

Your current calculation only involves integers and so will be affected by integer division (which truncates the result to the nearest integer value).

(currPixel / modifier) * newValue
     |           |
      ---------------integer division e.g. 10/3 = 3, not 3.333

然后将结果强制转换为两倍,但在此之前精度会丢失.

The result is then cast to double, but the accuracy is lost before this point.

请考虑以下内容:

#include <iostream>
using namespace std;

int main() {
    int val1 = 10;
    int val2 = 7;
    int val3 = 9;

    double outval1 = (val1 / val2) * val3;
    double outval2 = ((double)val1 / val2) * val3;
    cout << "without cast: " << outval1 << "\nwith    cast: "<< outval2 << std::endl;

    return 0;
}

此输出为:

without cast: 9
with    cast: 12.8571

在此处查看

请注意,强制转换必须在正确的位置进行

Note that the cast has to be applied in the right place:

(double)(val1 / val2) * val3 == 9.0      //casts result of (val1/val2) after integer division
(val1 / val2) * (double)val3 == 9.0      //promotes result of (val1/val2) after integer division
((double)val1 / val2) * val3 == 12.8571  //promotes val2 before division
(val1 / (double)val2) * val3 == 12.8571  //promotes val1 before division

由于其他操作数的提升,如果有疑问,您可以将所有内容都进行强制转换,结果代码将相同:

Due to promotion of the other operands, if in doubt you can just cast everything and the resulting code will be the same:

((double)val1 / (double)val2) * (double)val3 == 12.8571  

虽然有点冗长.

这篇关于C ++,我的double或int值始终为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆