Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help please, strange behavior

Reply
Thread Tools

Help please, strange behavior

 
 
Simply_Red
Guest
Posts: n/a
 
      02-14-2007
On 14 fév, 14:37, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Simply_Red wrote:
> > On 14 fév, 14:29, Rolf Magnus <(E-Mail Removed)> wrote:
> >> Simply_Red wrote:
> >>> On 14 fév, 14:12, "Victor Bazarov" <(E-Mail Removed)> wrote:
> >>>> Simply_Redwrote:
> >>>>> i'm sorry i posted this in other groupes, and i didn't see it,
> >>>>> and as this group is most actif, i repost it here, and sorry for
> >>>>> mutliposting:

>
> >>>>> Hi,
> >>>>> i'm using VC6, i have this declaration:

>
> >>>>> typedef struct tagTLimite {
> >>>>> double Debut;
> >>>>> double Fin;
> >>>>> }Limites;

>
> >>>>> typedef struct TagElemTab {
> >>>>> double NivY;
> >>>>> bool Existe;
> >>>>> std::vector<Limites> PtLimites;
> >>>>> }ElemTabCont;

>
> >>>>> void myfunc( )
> >>>>> {
> >>>>> .....
> >>>>> ElemTabCont * ContNonOrd;//contour non ordone
> >>>>> long nbNiveau = (yhi-ylo)/stepY +1 ;

>
> >>>> What is the type of 'yhi', 'ylo', 'stepY'? What are their values?
> >>>> What value do you get in 'nbNiveau'? What did you expect

>
> >>> yhi, ylo, stepY are doubles values: 16, 0, 0.1 respectively
> >>> I get 160 in nbNiveau, and I need 161.

>
> >> double isn't precise either. Note that 0.1 in binary is a periodic
> >> number that cannot be represented exactly. So instead of 0.1, you
> >> get the closest representable value. As a result, you might get
> >> something like 160.99999999998, which gets truncated to 160 when
> >> converted to long.

>
> > but if 160.999999998 is truncated to 160, 159.99999998 must be
> > truncated to 159.....

>
> Yes... And?
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


when i just put long nbNiveau = (yhi-ylo)/stepY. i get nbNiveau = 160
not 159

 
Reply With Quote
 
 
 
 
Simply_Red
Guest
Posts: n/a
 
      02-14-2007
On 14 fév, 14:35, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> Simply_Red wrote:
> > i'm sorry i posted this in other groupes, and i didn't see it, and as
> > this group is most actif, i repost it here, and sorry for
> > mutliposting:

>
> > Hi,
> > i'm using VC6, i have this declaration:

>
> > typedef struct tagTLimite {
> > double Debut;
> > double Fin;
> > }Limites;

>
> > typedef struct TagElemTab {
> > double NivY;
> > bool Existe;
> > std::vector<Limites> PtLimites;
> > }ElemTabCont;

>
> > void myfunc( )
> > {
> > .....
> > ElemTabCont * ContNonOrd;//contour non ordone
> > long nbNiveau = (yhi-ylo)/stepY +1 ;
> > ContNonOrd = new ElemTabCont [nbNiveau];
> > .....
> > }
> > i don't know why,nbNiveau is equal to (yhi-ylo)/stepY, it didn't
> > +1????and even if i put:

>
> How did you tell? Probably you misinterpret some output that we do not see
> because your code snippet is incomplete.
>
> > and even if i put: ContNonOrd = new ElemTabCont [nbNiveau+1]; the size of
> > ContNonOrd = nbNiveau????

>
> Again, how did you tell?
>
> Please post a minimal complete program that shows the problem. With the code
> fragments from above, there is no way to tell what the problem is
> (especially since we do not even know the types of yhi, yhl, and stepY.
>
> Best
>
> Kai-Uwe Bux


bool __declspec(dllexport) CALLBACK NewMakeContour(LPSAFEARRAY FAR
*saY, VARIANT FAR *pvArray, double yhi, double ylo,double stepX,double
stepY)
{

SAFEARRAYBOUND sabound[1]; // 1-D array
SAFEARRAY FAR* psa = NULL; // SAFEARRAY structure pointer
HRESULT hr; // Operations result
float HUGEP *vbY; // Tableau Contenant tous les points
long UboundY;
double V;
long ptTabRes,ptTabSource;

hr = SafeArrayAccessData(*saY, (void HUGEP**)&vbY);
UboundY = (*saY)->rgsabound[0].cElements;

ElemTabCont * ContNonOrd;//contour non ordone
long nbNiveau = (yhi-ylo)/stepY+1 ;// +1 ; je ne sais pas pourquoi il
ne me fait pas le + 1;
ContNonOrd = new ElemTabCont[nbNiveau];
//ContNonOrd = new ElemTabCont[161];//donc je suis obliger de le
faire ici

V = vbY[1];//le premier pt est un pt du contour
ptTabRes = 0;
ptTabSource = 0;
while (FRound(V,3) <= FRound(yhi,3))
{
FindLevelPt(vbY, ptTabSource,ContNonOrd,ptTabRes,V,UboundY,stepX);
ptTabRes++;
V+=stepY;
}
delete[] ContNonOrd;
hr = SafeArrayUnaccessData(*saY);
return true
}


void FindLevelPt(float* TabSource, long& ptTabSource,ElemTabCont*
ContNonOrd,long ptTabRes,double V, long UBoundY, float StepX) {

Limites XLimites;

ContNonOrd[ptTabRes].NivY = V;
//si on est au niv superieur a V=>pas de point au niveau V
if (FRound(TabSource[ptTabSource+1],3) == FRound(V,3))
{
ContNonOrd[ptTabRes].Existe = true;

bool nivDepasse = false;
bool terminer = false;
while (!nivDepasse && !terminer)
{
XLimites.Debut = TabSource[ptTabSource];

bool trouve = false;//true si on trouve la limite du segment
ptTabSource+=3;//le debut a ete deja trouver
while(!trouve && ptTabSource <= UBoundY-3) //-1 z, -2 y -3 x
{
nivDepasse = (FRound(TabSource[ptTabSource+1],3) > FRound(V,3));
trouve = (FRound(TabSource[ptTabSource]-TabSource[ptTabSource-3],
3)>FRound(StepX,3)) || nivDepasse ;
ptTabSource+=3;
}
if (trouve) {
XLimites.Fin = TabSource[ptTabSource-6];
ptTabSource-=3;//pour pointer au prochain debut.
ContNonOrd[ptTabRes].PtLimites.push_back(XLimites);
}
else {//le dernier pt ou la derniere ligne
terminer = true;
XLimites.Fin = TabSource[ptTabSource-3];
ContNonOrd[ptTabRes].PtLimites.push_back(XLimites);
}
}
}
else ContNonOrd[ptTabRes].Existe = false;
}

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      02-14-2007
Simply_Red wrote:

>> >> That last statement is beyond me. What do you mean by "the size of
>> >> ContNotOrd"? How the hell can you know the size? How can it be
>> >> different from the size you requested?

>>
>> > i just verify in watch window ContNotOrd[160].

>>
>> I don't know how you do that, but tell me, if you do
>>
>> int main() {
>> long nbNiveau = 160;
>> char *ContNotOrd = new char[nbNiveau + 1];
>> }
>>
>> and "verify in watch window", do you get 160 or 161 _elements_? Do
>> not tell me the last index (it is less by 1 than the size, right?)

>
> when i give a value to ContNotOrd[161] i have a crash,


That's not surprising. If nbNiveau is 160, then you allocate an array with
161 elements, numbered 0 to 160. So if you try to access the element with
index 161, you are going past the end of your array.

> and 160 no.....for my example i check the vector of the 160 elem, when
> there is no pb it contain _First = 0 _Last = 0 _End = 0.


 
Reply With Quote
 
Simply_Red
Guest
Posts: n/a
 
      02-14-2007
> when i just put long nbNiveau = (yhi-ylo)/stepY. i get nbNiveau = 160
> not 159


i was wrong it's 159...........

and if i use float, is there pbs with the precision???

 
Reply With Quote
 
Simply_Red
Guest
Posts: n/a
 
      02-14-2007
On 14 fév, 15:25, Rolf Magnus <(E-Mail Removed)> wrote:

>
> That's not surprising. If nbNiveau is 160, then you allocate an array with
> 161 elements, numbered 0 to 160. So if you try to access the element with
> index 161, you are going past the end of your array.
>


i know this, but my pb is that i need that nbNiveau = 161, and i get
160, i was sure that when the value is betwwen 160.51 and
160.99999999, in long the compilator will replace it with 160


 
Reply With Quote
 
Simply_Red
Guest
Posts: n/a
 
      02-14-2007
On 14 fév, 15:33, "Simply_Red" <(E-Mail Removed)> wrote:
> On 14 fév, 15:25, Rolf Magnus <(E-Mail Removed)> wrote:
>
>
>
> > That's not surprising. If nbNiveau is 160, then you allocate an array with
> > 161 elements, numbered 0 to 160. So if you try to access the element with
> > index 161, you are going past the end of your array.

>
> i know this, but my pb is that i need that nbNiveau = 161, and i get
> 160, i was sure that when the value is betwwen 160.51 and
> 160.99999999, in long the compilator will replace it with 160


i wanted to say: i was sure that when the value is betwwen 160.51 and
> 160.99999999, in long the compilator will replace it with 161


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-14-2007
Simply_Red wrote:
> [..]
> i wanted to say: i was sure that when the value is betwwen 160.51 and
>> 160.99999999, in long the compilator will replace it with 161


What in the world made you so *sure* of that? Just curious...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-14-2007
Simply_Red wrote:
>> when i just put long nbNiveau = (yhi-ylo)/stepY. i get nbNiveau =
>> 160 not 159

>
> i was wrong it's 159...........
>
> and if i use float, is there pbs with the precision???


Of course. Just like if you use 'double', only worse.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      02-14-2007
Simply_Red wrote:

> On 14 fév, 15:33, "Simply_Red" <(E-Mail Removed)> wrote:
>> On 14 fév, 15:25, Rolf Magnus <(E-Mail Removed)> wrote:
>>
>>
>>
>> > That's not surprising. If nbNiveau is 160, then you allocate an array
>> > with 161 elements, numbered 0 to 160. So if you try to access the
>> > element with index 161, you are going past the end of your array.

>>
>> i know this, but my pb is that i need that nbNiveau = 161, and i get
>> 160, i was sure that when the value is betwwen 160.51 and
>> 160.99999999, in long the compilator will replace it with 160

>
> i wanted to say: i was sure that when the value is betwwen 160.51 and
>> 160.99999999, in long the compilator will replace it with 161


Well, that's not the case. In C++, when converting a floating point value
into an integer, the fractional part is always truncated.

 
Reply With Quote
 
Simply_Red
Guest
Posts: n/a
 
      02-14-2007
On 14 fév, 15:41, "Victor Bazarov" <(E-Mail Removed)> wrote:
> Simply_Red wrote:
> > [..]
> > i wanted to say: i was sure that when the value is betwwen 160.51 and
> >> 160.99999999, in long the compilator will replace it with 161

>
> What in the world made you so *sure* of that? Just curious...
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask



in vb it's like that, i know that doesn't meen that C++ must do the
same

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange STL vector behavior -- request help dakupoto@gmail.com C++ 1 04-04-2012 08:38 PM
Help please! strange Tkinter behavior has me totally baffled. Steve Ferg Python 2 08-25-2010 01:56 PM
onclick on <a> causing strange behavior, please help? Erland Javascript 6 09-06-2007 08:40 AM
Very strange behavior of "If Not rs.EOF", Need help... John ASP General 10 08-23-2006 05:22 PM
Very strange behavior. Please help. Barry ASP .Net 2 11-04-2003 11:05 PM



Advertisments