开发者

warning errors problem

开发者 https://www.devze.com 2023-03-07 16:22 出处:网络
Program: void DibLaplacian8Direct(CDib sourceImg) { register int i,j; int w = sourceImg.GetWidth(); int h = sourceImg.GetHeight();

Program:

void DibLaplacian8Direct(CDib sourceImg)
{   
    register int i,j;
    int w = sourceImg.GetWidth();
    int h = sourceImg.GetHeight();
    CDib cpyImage = sourceImg;
    BYTE** pSourceImg = sourceImg.GetPtr();
    BYTE开发者_如何学JAVA** pCpyImage = cpyImage.GetPtr();
    float G;
    for(j =1;j<h-1;j++)
    {
        for(i =1;i<w-1;i++)
        {
            G = -1*pCpyImage[j-1][i-1] + -1*pCpyImage[j-1][i] + (-1)*pCpyImage[j-1][i+1]+
                (-1)*pCpyImage[j][i-1] + 8*pCpyImage[j][i]    + (-1)*pCpyImage[j][i+1]+
                -1*pCpyImage[j+1][i-1]  + (-1)*pCpyImage[j+1][i] + -1*pCpyImage[j+1][i+1];
            pSourceImg[j][i] = (BYTE)G;
        }
    }
}

warning error:

warning.. Can't coonvert from int to float..

Warning 1 warning C4819: The file contains a character that cannot be represented in the current code page (1257). Save the file in Unicode format to prevent data loss D:\2nd\imagetool\dibfilter.cpp 1 1 ImageTool

I do't understand that why its making me warning of int to float. and for warning 1, I am using VS 2010.. i do't know that i am getting warning in StdAfx.h include file . Amy one can help me with this .


The first warning is due to the fact that a float has only six significant figures whereas an int can have more. If it does, then accuracy is lost.

In general, you cannot convert an integer to floating point without possible losing data. Also, you cannot convert from floating point back to integer without losing the deceimal places, so you get a warning again.

A simple minimalistic code example of the above case:

#include<iostream>
using namespace std;

int main()
{
    int a=10;
    int b=3;

    float c;
    c=a/b;

    cout << c << endl;
    return 0;
}

If you are sure of the data being in the range and there wont be any loss of accuracy you can use typecasting to get rid of the warning.

G = (float) (.....)

Check this for the second warning.

To get rid of the second warning you need to save the file in Unicode format.

Go to file->advanced save options and under that select the new encoding you want to save it as. UTF-8 or UNICODE codepage 1200 are the settings you want.


It is important to understand what the compiler is telling you with warning 20. The issue is that floating point numbers have only 23 bits of precision, while ints have 31. If your numbers is larger than 2^23, you will lose the low bits by storing in a float.

Now your number can never be larger than 2^23, so you are fine. Still, it is important to know what is going on here. There is a reason for that warning, and simply putting in the cast without understanding what is going on may mess you up some day.

In your specific case, I am not at all clear on why you are using a float. You are adding nine integers, none of which can be greater than 2^11. You have plenty of precision in an int to do that. Using a float is just going to slow your program down. (Probably quite a bit.)

Finally, that last cast to BYTE is worrisome. What happens if your value is out of range? Probably not what you want. For example if BYTE is unsigned, and your float ends up -3.0, you will be storing 253 as the result.

0

精彩评论

暂无评论...
验证码 换一张
取 消