0

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** 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 .

3
  • 1
    Since your edit, some of the answers bear no relation to the question as stated. This makes this page hard to follow. Please do not do this. If you have a new question, post it as a new question. Commented May 21, 2011 at 6:27
  • 1
    hmm @johnsyweb.. hmm ok thanks .. i will care for it .. Commented May 21, 2011 at 6:33
  • @johnsyweb: i corrected the question .. Commented May 21, 2011 at 6:48

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@Miss: Updated the answer. Please just don't use the typecasting if you are not sure of the accuracy. You are better of using G as float in that case.
yes i used flaot.. can you give me any reference from where i can study the accuracy of these int and flaot.. etc
i did as you said and its removed but i have one more error same like that . @als : error : 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\dib.h 1 1 ImageTool
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.