weird problem  
Author Message
Goop





PostPosted: Sun Mar 07 12:58:03 CST 2004 Top

Visual Studio C++ >> weird problem

Hi

I've got a weird problem

I initiate an integer with the result of a function. When I debug, I see
that the value is correctly calculated in this function, the result is 40.
But the integer gets the value 39 instead of 40.

int phase=current->GetPhase(...);

How is this possible?

thanx

Visual Studio234  
 
 
David





PostPosted: Sun Mar 07 12:58:03 CST 2004 Top

Visual Studio C++ >> weird problem >I've got a weird problem
>
>I initiate an integer with the result of a function. When I debug, I see
>that the value is correctly calculated in this function, the result is 40.
>But the integer gets the value 39 instead of 40.
>
>int phase=current->GetPhase(...);
>
>How is this possible?

What's the definition/code for GetPhase?

Is there a floating point value/calculation involved anywhere?

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
 
 
Kurt





PostPosted: Sun Mar 07 13:12:31 CST 2004 Top

Visual Studio C++ >> weird problem yes there is, that's probably the problem, but how to solve it? Just adding
1 of course isn't good enough, cause that will also affect the other results
of the function, which are most of the time correct

int CSegment::GetPhase(int pixelnr)
{
float f=((float)pixelnr)/((float)length);
return (int)((1-f)*vertices[0]->GetPhase()+f*vertices[1]->GetPhase());
}

and the call is:
int phase=current->GetPhase(pos);

So actually I interpolate the phases (which are ints) of the vertices
according to the number of the pixel

some values in my de****:

->right before the function call
pos =33

->in the GetPhase function:
pixelnr =33
f =0.351064
vertices[0]->GetPhase() =7
vertices[1]->GetPhase() =101
(1-f)*vertices[0]->GetPhase()+f*vertices[1]->GetPhase() =40.0000
(int)((1-f)*vertices[0]->GetPhase()+f*vertices[1]->GetPhase()) =40

->right after to the function call
phase =39

the correct value should be 40. This is the value I expect and I also get 40
when I calculate it on a calculator




> >I've got a weird problem
> >
> >I initiate an integer with the result of a function. When I debug, I see
> >that the value is correctly calculated in this function, the result is
40.
> >But the integer gets the value 39 instead of 40.
> >
> >int phase=current->GetPhase(...);
> >
> >How is this possible?
>
> What's the definition/code for GetPhase?
>
> Is there a floating point value/calculation involved anywhere?
>
> Dave
> --
> MVP VC++ FAQ: http://www.hide-link.com/


 
 
Micus





PostPosted: Sun Mar 07 14:33:13 CST 2004 Top

Visual Studio C++ >> weird problem


> yes there is, that's probably the problem, but how to solve it? Just
adding
> 1 of course isn't good enough, cause that will also affect the other
results
> of the function, which are most of the time correct
>
> int CSegment::GetPhase(int pixelnr)
> {
> float f=((float)pixelnr)/((float)length);
> return (int)((1-f)*vertices[0]->GetPhase()+f*vertices[1]->GetPhase());

would this or something similar work?

return (int)(((1.0-f) * float(vertices[0]->GetPhase()) +
f * float(vertices[1]->GetPhase())) +
.5);
HTH,
M

> }
>
> and the call is:
> int phase=current->GetPhase(pos);
>
> So actually I interpolate the phases (which are ints) of the vertices
> according to the number of the pixel
>
> some values in my de****:
>
> ->right before the function call
> pos =33
>
> ->in the GetPhase function:
> pixelnr =33
> f =0.351064
> vertices[0]->GetPhase() =7
> vertices[1]->GetPhase() =101
> (1-f)*vertices[0]->GetPhase()+f*vertices[1]->GetPhase() =40.0000
> (int)((1-f)*vertices[0]->GetPhase()+f*vertices[1]->GetPhase()) =40
>
> ->right after to the function call
> phase =39
>
> the correct value should be 40. This is the value I expect and I also get
40
> when I calculate it on a calculator
>
>


> > >I've got a weird problem
> > >
> > >I initiate an integer with the result of a function. When I debug, I
see
> > >that the value is correctly calculated in this function, the result is
> 40.
> > >But the integer gets the value 39 instead of 40.
> > >
> > >int phase=current->GetPhase(...);
> > >
> > >How is this possible?
> >
> > What's the definition/code for GetPhase?
> >
> > Is there a floating point value/calculation involved anywhere?
> >
> > Dave
> > --
> > MVP VC++ FAQ: http://www.hide-link.com/
>
>


 
 
Kurt





PostPosted: Sun Mar 07 15:10:16 CST 2004 Top

Visual Studio C++ >> weird problem


>


> > yes there is, that's probably the problem, but how to solve it? Just
> adding
> > 1 of course isn't good enough, cause that will also affect the other
> results
> > of the function, which are most of the time correct
> >
> > int CSegment::GetPhase(int pixelnr)
> > {
> > float f=((float)pixelnr)/((float)length);
> > return
(int)((1-f)*vertices[0]->GetPhase()+f*vertices[1]->GetPhase());
>
> would this or something similar work?
>
> return (int)(((1.0-f) * float(vertices[0]->GetPhase()) +
> f * float(vertices[1]->GetPhase())) +
> .5);
> HTH,
> M
>
> > }
> >
> > and the call is:
> > int phase=current->GetPhase(pos);
> >
> > So actually I interpolate the phases (which are ints) of the vertices
> > according to the number of the pixel
> >
> > some values in my de****:
> >
> > ->right before the function call
> > pos =33
> >
> > ->in the GetPhase function:
> > pixelnr =33
> > f =0.351064
> > vertices[0]->GetPhase() =7
> > vertices[1]->GetPhase() =101
> > (1-f)*vertices[0]->GetPhase()+f*vertices[1]->GetPhase() =40.0000
> > (int)((1-f)*vertices[0]->GetPhase()+f*vertices[1]->GetPhase()) =40
> >
> > ->right after to the function call
> > phase =39
> >
> > the correct value should be 40. This is the value I expect and I also
get
> 40
> > when I calculate it on a calculator
> >
> >


> > > >I've got a weird problem
> > > >
> > > >I initiate an integer with the result of a function. When I debug, I
> see
> > > >that the value is correctly calculated in this function, the result
is
> > 40.
> > > >But the integer gets the value 39 instead of 40.
> > > >
> > > >int phase=current->GetPhase(...);
> > > >
> > > >How is this possible?
> > >
> > > What's the definition/code for GetPhase?
> > >
> > > Is there a floating point value/calculation involved anywhere?
> > >
> > > Dave
> > > --
> > > MVP VC++ FAQ: http://www.hide-link.com/
> >
> >
>
>

that didn't help but i solved the problem. Appearantly the value was rounded
off to 39.9995 or something, so the de**** displayed 40 but in reality it
was rounded off to 39 when casted to int. Solved this by adding a small
value. Thx for your reply


 
 
David





PostPosted: Sun Mar 07 15:46:05 CST 2004 Top

Visual Studio C++ >> weird problem >yes there is, that's probably the problem, but how to solve it?

The solution really depends on your application. If you can do without
floating point values, then do so. Stick with integers, scale them
appropriately if you need to maximise accuracy.

If you can't avoid floating point values, and you want an integer
value, then at some point you have to round the value. Depending on
your requirement, you may need to round down (which is the default
behaviour and what's happened in your current situation), or round to
the nearest integer. Have a look at the _controlfp function, or
implement the rounding yourself as suggested by the other poster.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq