473,508 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C overflow/underflow problem

I have a problem which I think is due to overflow/underflow.

I want to segment an Image using SOM network. Code follows:
================================================== ============================================
struct MyImage
{
int width, height;
long int num_pixels;
short int *tag;
unsigned char *R, *B, *G;
};

struct _SOMgrid {
int width,height;
int N;
int *R,*B,*G;
};

The functions with which I update the SOM's weights is:

void UpdateWeightsSmall(struct _SOMgrid *SOMgrid,struct MyImage
*Image,int min_index,int epoch,long int current) {
int j;
float n,s,h,dist;
int cR=0,cB=0,cG=0;
int current_x,current_y,loop_x,loop_y,mindimension,rad ius;

current_y=min_index/SOMgrid->width;
current_x=min_index-current_y*SOMgrid->width;

//Find learning rate
n=0.9*(1-(epoch*1.0/(SOM_EPOCHS*2)));

//Set radius as portion of image size
mindimension=(SOMgrid->width<SOMgrid->height)?SOMgrid->width:SOMgrid->height;
radius=(int)rint(mindimension*0.2);
//Find s
s=radius*(1-epoch*1.0/SOM_EPOCHS)+1;

for(j=0;j<SOMgrid->N;j++) {
loop_y=j/SOMgrid->width;
loop_x=j-loop_y*SOMgrid->width;

dist=sqrtf((loop_x-current_x)*(loop_x-current_x) +
(loop_y-current_y)*(loop_y-current_y));
h=expf(-powf(dist,2)/(2*powf(s,2)));

cR=(int)lrintf(n*h*((float)SOMgrid->R[min_index]-SOMgrid->R[j]));
cG=(int)lrintf(n*h*((float)SOMgrid->G[min_index]-SOMgrid->G[j]));
cB=(int)lrintf(n*h*((float)SOMgrid->B[min_index]-SOMgrid->B[j]));
SOMgrid->R[j]+=cR;
SOMgrid->B[j]+=cB;
SOMgrid->G[j]+=cG;

if(SOMgrid->R[j]>255 || SOMgrid->R[j]<0 || SOMgrid->G[j]>255 ||
SOMgrid->G[j]<0 || SOMgrid->B[j]>255 || SOMgrid->B[j]<0 ) {
printf("changes:: (%d),input=(%d,%d), loop=(%d,%d), radius=%d, n=%f,
s=%f, dist=%f, h=%f, input=(%u,%u,%u), SOM=(%u,%u,%u), change=(%u,%u,%u)
%u,%f,%f\n",
epoch,current_x,current_y,loop_x,loop_y,
radius,n,s,dist,h,Image->R[current],Image->G[current],Image->B[current],SOMgrid->R[j],SOMgrid->G[j],SOMgrid->B[j],
cR,cG,cB,(unsigned int)tmpR,tmpG,tmpB);
exit(1);
}
}
}
================================================== =====================================

The inputs to the following function are checked and are ok. Outside the
function in my main loop where I feed the inputs I have:
printf("%ld %d,%d,%d\n",i,SOMgrid->R[i],SOMgrid->G[i],SOMgrid->B[i]);
where i is the curent input --a pixel of my image.

At some time this prints something like (different numbers on every run):

305 -1633772131,2071692934,2088531831
306 -1633771874,2038003318,-2021162623
307 -1616928865,-2105377154,-1835889267
308 -1616928608,-1903261560,-1633773673
309 -1684300387,-1785359726,-1381128538
As you can see In my update function I do an if() check whether weights
exceed 0 or 255 and it passes without any errors. Why does printf gives
me this? The programm ends "correctly" but the output is messy.

Can anybody help me? I'm stuck in C with those stupid number
conversions. Thanks a lot.
Dec 20 '05 #1
1 1782
Harris Kosmidis wrote:
I have a problem which I think is due to overflow/underflow.
At some time this prints something like (different numbers on every run):

305 -1633772131,2071692934,2088531831
306 -1633771874,2038003318,-2021162623
307 -1616928865,-2105377154,-1835889267
308 -1616928608,-1903261560,-1633773673
309 -1684300387,-1785359726,-1381128538

As you can see In my update function I do an if() check whether weights
exceed 0 or 255 and it passes without any errors. Why does printf gives
me this? The programm ends "correctly" but the output is messy.


Sounds like a memory corruption problem. Try turning the
compiler warning level up to maximum, and check all of your
memory allocations and frees (which you didn't post).

The best thing you can do is to post a complete program that
we can compile and demonstrate the problem.

Dec 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
8479
by: Oplec | last post by:
Hello, How can underflow and overflow of the basic arithmetic types be tested for using standard C++? For instance, if two long doubles are multiplied together, test whether or not the result...
5
8141
by: Codemonkey | last post by:
Hi, When I first installed Visual Studio 2003, I noticed I was getting the follow error when showing a form: A first chance exception of type 'System.ArithmeticException' occurred in...
4
3543
by: aling | last post by:
Given: signed a, b; How to judge overflow of the sum of these two operands? Just use one *if* statement. Is this statement right? if ((a>0 && b>0 && sum<=0) || (a<0 && b<0 && sum>=0)) //...
2
6051
by: Paul Emmons | last post by:
Can anyone suggest example code, or how to proceed, in adding or subtracting two long long integers (I'm working in gcc with Linux, where a long long is 64 bits) and determining whether an overflow...
5
9191
by: Ian Pilcher | last post by:
I'm trying to figure out if an increment to a variable of an integer type, followed by a decrement, (or vice versa) is guaranteed to restore the variable to its initial value, even if the first...
4
4124
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in...
4
9929
by: Raymond | last post by:
Source: http://moryton.blogspot.com/2007/08/detecting-overflowunderflow-when.html Example from source: char unsigned augend (255); char unsigned const addend (255); char unsigned const sum...
2
3065
by: jou00jou | last post by:
Hi, I have trouble using sscanf and fgets to check for overflow. I will post the assignment specification so I could help whoever would kindly like to offer his/her help. ...
11
3171
by: pereges | last post by:
Hi, I have written a small function that can (I think) deal with overflow/underflow while adding integers and divide by zero problem. Can some one please tell me if I have done it correctly ? Would...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7385
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1558
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.