469,601 Members | 2,329 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,601 developers. It's quick & easy.

Help please, strange behavior

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<LimitesPtLimites;
}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:
ContNonOrd = new ElemTabCont [nbNiveau+1]; the size of ContNonOrd =
nbNiveau????

Feb 14 '07 #1
31 1408
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<LimitesPtLimites;
}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?

It is possible that you're encountering rounding (truncation in
case of integer division) and adding 1 doesn't do what you need
it to do.
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:
ContNonOrd = new ElemTabCont [nbNiveau+1]; the size of ContNonOrd =
nbNiveau????
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?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 14 '07 #2
On 14 fév, 14:12, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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<LimitesPtLimites;
}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.
?
>
It is possible that you're encountering rounding (truncation in
case of integer division) and adding 1 doesn't do what you need
it to do.
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:
ContNonOrd = new ElemTabCont [nbNiveau+1]; the size of ContNonOrd =
nbNiveau????
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].


Feb 14 '07 #3
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<LimitesPtLimites;
}ElemTabCont;
[redacted]
In addition to what Victor said to you, the definitions above are C-ish
and not really needed. They should be:

struct Limites {
double Debut;
double Fin;
};

struct ElemTabCont {
double NivY;
bool Existe;
std::vector<LimitesPtLimites;
};

No typedefs or "tag" names are necessary.
Feb 14 '07 #4
Simply_Red wrote:
On 14 fév, 14:12, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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<LimitesPtLimites;
}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.

Feb 14 '07 #5
Simply_Red wrote:
On 14 fév, 14:12, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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<LimitesPtLimites;
}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.
You have stumbled upon a well-known (now to you as well) situation
when 16/0.1 is not 160 (exactly) but rather 159.999999999999. Add
1 to it and you get 160.999999999999 and assign it to a 'long', you
get 160. It's called "imprecise representation of a decimal value"
and essentially all computers with binary FP units suffer from it.

To calculate those things "correctly", add 1.0000001 instead of 1:

long nbNiveau = (yhi-ylo)/stepY + 1.0000001;

It's not pretty, but it will work better. The number of zeros after
the decimal point depends on your 'yhi-ylo' scale.
?
>>
It is possible that you're encountering rounding (truncation in
case of integer division) and adding 1 doesn't do what you need
it to do.
>>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:
ContNonOrd = new ElemTabCont [nbNiveau+1]; the size of ContNonOrd =
nbNiveau????
>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?)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 14 '07 #6
On 14 fév, 14:29, Rolf Magnus <ramag...@t-online.dewrote:
Simply_Red wrote:
On 14 fév, 14:12, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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<LimitesPtLimites;
}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.....

Feb 14 '07 #7
Simply_Red wrote:
On 14 fév, 14:29, Rolf Magnus <ramag...@t-online.dewrote:
>Simply_Red wrote:
>>On 14 fév, 14:12, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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<LimitesPtLimites;
}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
Feb 14 '07 #8
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<LimitesPtLimites;
}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
Feb 14 '07 #9
On 14 fév, 14:35, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Simply_Red wrote:
On 14 fév, 14:12, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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<LimitesPtLimites;
}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.

You have stumbled upon a well-known (now to you as well) situation
when 16/0.1 is not 160 (exactly) but rather 159.999999999999. Add
1 to it and you get 160.999999999999 and assign it to a 'long', you
get 160. It's called "imprecise representation of a decimal value"
and essentially all computers with binary FP units suffer from it.

To calculate those things "correctly", add 1.0000001 instead of 1:

long nbNiveau = (yhi-ylo)/stepY + 1.0000001;

It's not pretty, but it will work better. The number of zeros after
the decimal point depends on your 'yhi-ylo' scale.
?
It is possible that you're encountering rounding (truncation in
case of integer division) and adding 1 doesn't do what you need
it to do.
>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:
ContNonOrd = new ElemTabCont [nbNiveau+1]; the size of ContNonOrd =
nbNiveau????
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?)

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 give a value to ContNotOrd[161] i have a crash, 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.

Feb 14 '07 #10
On 14 fév, 14:37, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Simply_Red wrote:
On 14 fév, 14:29, Rolf Magnus <ramag...@t-online.dewrote:
Simply_Red wrote:
On 14 fév, 14:12, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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<LimitesPtLimites;
}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

Feb 14 '07 #11
On 14 fév, 14:35, Kai-Uwe Bux <jkherci...@gmx.netwrote:
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<LimitesPtLimites;
}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;
}

Feb 14 '07 #12
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.
Feb 14 '07 #13
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???

Feb 14 '07 #14
On 14 fév, 15:25, Rolf Magnus <ramag...@t-online.dewrote:
>
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
Feb 14 '07 #15
On 14 fév, 15:33, "Simply_Red" <Simply.Re...@gmail.comwrote:
On 14 fév, 15:25, Rolf Magnus <ramag...@t-online.dewrote:
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
Feb 14 '07 #16
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
Feb 14 '07 #17
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
Feb 14 '07 #18
Simply_Red wrote:
On 14 fév, 15:33, "Simply_Red" <Simply.Re...@gmail.comwrote:
>On 14 fév, 15:25, Rolf Magnus <ramag...@t-online.dewrote:
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.

Feb 14 '07 #19
On 14 fév, 15:41, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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 :-(

Feb 14 '07 #20
On 14 fév, 15:42, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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
So i got to use:

float FRound(double f, BYTE b)
{
char sz[20];
char szFmt[5] = { '%','.','0'+b,'f', 0 };
sprintf(sz, szFmt, f);
return atof(sz);
}

and put :
long nbNiveau = FRound ((y1-y2)/y3+1,0);

Feb 14 '07 #21
On 14 fév, 15:43, Rolf Magnus <ramag...@t-online.dewrote:
Well, that's not the case. In C++, when converting a floating point value
into an integer, the fractional part is always truncated.
now i know it, but what i found strange is that, if i put the same
instruction in watch window(debug), i get the result that i need. (yhi-
ylo)/stepy+1 became 161, in watch window and nbNiveau = 160

Feb 14 '07 #22
Simply_Red wrote:
On 14 fév, 15:42, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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

So i got to use:

float FRound(double f, BYTE b)
{
char sz[20];
char szFmt[5] = { '%','.','0'+b,'f', 0 };
sprintf(sz, szFmt, f);
return atof(sz);
}

and put :
long nbNiveau = FRound ((y1-y2)/y3+1,0);
Ugh!... Please don't do that. For any 'f' greater than 1e10
it will overrun the buffer ('sz'), which is how security holes
get created/exploited.

What is your goal here? Is (y1-y2) very close to N * y3 or is
it arbitrary? If it's close, you can always try

long nbNiveau = (y1-y2)/y3 + 1; // your formula
if ((nbNiveau - 1) * y3 < (y1-y2)) // verify!
++nbNiveau; // bump it up!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 14 '07 #23
Simply_Red wrote:
On 14 fév, 15:43, Rolf Magnus <ramag...@t-online.dewrote:
>Well, that's not the case. In C++, when converting a floating point
value into an integer, the fractional part is always truncated.
now i know it, but what i found strange is that, if i put the same
instruction in watch window(debug), i get the result that i need.
(yhi- ylo)/stepy+1 became 161, in watch window and nbNiveau = 160
Apparently your "watch window" rounds it before outputting. Try

printf("%f", (yhi-ylo)/stepy+1)

and you're likely to see 161 as well. Conversion to text rounds
the value (and lies to you).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 14 '07 #24
Simply_Red wrote:

i know this, but my pb is that i need
It's best to post in straight English with strange abreviations like
"pb". I assume you mean "problem", but that's not a standard
abreviation in English, except as the chemical name of lead.


Brian
Feb 14 '07 #25
Default User wrote:
Simply_Red wrote:

>i know this, but my pb is that i need

It's best to post in straight English with strange abreviations like
"pb". I assume you mean "problem", but that's not a standard
abreviation in English, except as the chemical name of lead.
V.E.R.A. offes "Pipeline Burst".
Feb 15 '07 #26
Rolf Magnus wrote:
Default User wrote:
Simply_Red wrote:

i know this, but my pb is that i need
It's best to post in straight English with strange abreviations like
"pb". I assume you mean "problem", but that's not a standard
abreviation in English, except as the chemical name of lead.

V.E.R.A. offes "Pipeline Burst".
And we all know how painful that can be.


Brian
Feb 15 '07 #27
On 14 fév, 16:09, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Ugh!... Please don't do that. For any 'f' greater than 1e10
it will overrun the buffer ('sz'), which is how security holes
get created/exploited.
I don't understand what is le10???
>
What is your goal here? Is (y1-y2) very close to N * y3 or is
it arbitrary? If it's close, you can always try
long nbNiveau = (y1-y2)/y3 + 1; // your formula
if ((nbNiveau - 1) * y3 < (y1-y2)) // verify!
++nbNiveau; // bump it up!
Y1,Y2 and Y3 aren't arbitrary and i know that (y1-y2)/y3 = N
(integer).
and if i use:

long LRound(double f)
{
char sz[20];
char szFmt[5] = { '%','.','0','f', 0 };
sprintf(sz, szFmt, f);
return atol(sz);
}

Feb 15 '07 #28
On 14 fév, 17:13, "Default User" <defaultuse...@yahoo.comwrote:
Simply_Red wrote:
i know this, but my pb is that i need

It's best to post in straight English with strange abreviations like
"pb". I assume you mean "problem", but that's not a standard
abreviation in English, except as the chemical name of lead.

Brian
Sorry, this abreviation is used in french.

Feb 15 '07 #29
Simply_Red wrote:
On 14 fév, 17:13, "Default User" <defaultuse...@yahoo.comwrote:
>Simply_Red wrote:
>>i know this, but my pb is that i need

It's best to post in straight English with strange abreviations like
"pb". I assume you mean "problem", but that's not a standard
abreviation in English, except as the chemical name of lead.

Brian

Sorry, this abreviation is used in french.
Good to know. c.l.c++ is an English-speaking newsgroup, though.
Feb 15 '07 #30

"Simply_Red" <Si**********@gmail.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...
typedef struct tagTLimite {
double Debut;
double Fin;
}Limites;
typedef struct TagElemTab {
double NivY;
bool Existe;
std::vector<LimitesPtLimites;
}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.....
True, but where did you get 159.99999998?

The problem is that, sometimes, the result of (yhi-ylo)/stepY will not be an
exact integer, because stepY may not be exactly representable in binary (as
a double or float). So sometimes your array size will be too small.

The solution for handling this depends on your goal. How are you later
using the array elements?

In your example, stepY is 0.1, or 1/10, which means you're expecting to get
(yhi-ylo)*10 = 160, right? Well, where does stepY come from? Can you
compute it from something like 1/numElementsPerUnit, where
numElementsPerUnit is an integer? If so, your code could change to:

nbNiveau = (yhi-ylo)*numElementsPerUnit + 1;
stepY = 1 / numElementsPerUnit;

That would avoid the issue entirely.

If stepY is not representable that way, and if yhi-ylo isn't too big, you
could just add 1.1 instead of 1 before truncating to an integer, but that's
not the prefered solution.

Victor's idea of "bumping up" the value might work for you, as well, if all
you care about is having "enough" elements in the array.

One thing to note: If you ever make calculations like (i-ylo)/stepY, or
simply i*stepY, then for large values of i the result will get further and
further from what you expect. But that's probably not be a problem for you,
since your values are small.

By the way, one thing I like to do when investigating problems involving
calculations like this is to store intermediate results to another variable,
like this:

double temp = (yhi-ylo)/stepY +1;
long nbNiveau = temp;

This lets you examine the values yourself when debugging, instead of trying
to guess what's going on inside the program.

Hope this helps...

-Howard
Feb 15 '07 #31
On 15 fév, 16:33, "Howard" <m...@here.comwrote:
"Simply_Red" <Simply.Re...@gmail.comwrote in message

news:11**********************@s48g2000cws.googlegr oups.com...
typedef struct tagTLimite {
double Debut;
double Fin;
}Limites;
typedef struct TagElemTab {
double NivY;
bool Existe;
std::vector<LimitesPtLimites;
}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.....

True, but where did you get 159.99999998?

The problem is that, sometimes, the result of (yhi-ylo)/stepY will not bean
exact integer, because stepY may not be exactly representable in binary (as
a double or float). So sometimes your array size will be too small.

The solution for handling this depends on your goal. How are you later
using the array elements?

In your example, stepY is 0.1, or 1/10, which means you're expecting to get
(yhi-ylo)*10 = 160, right? Well, where does stepY come from? Can you
compute it from something like 1/numElementsPerUnit, where

I take the value of stepY from a database, i can't compute it.

numElementsPerUnit is an integer? If so, your code could change to:

nbNiveau = (yhi-ylo)*numElementsPerUnit + 1;
stepY = 1 / numElementsPerUnit;

That would avoid the issue entirely.

If stepY is not representable that way, and if yhi-ylo isn't too big, you
could just add 1.1 instead of 1 before truncating to an integer, but that's
not the prefered solution.

Victor's idea of "bumping up" the value might work for you, as well, if all
you care about is having "enough" elements in the array.

One thing to note: If you ever make calculations like (i-ylo)/stepY, or
simply i*stepY, then for large values of i the result will get further and
further from what you expect. But that's probably not be a problem for you,
since your values are small.

By the way, one thing I like to do when investigating problems involving
calculations like this is to store intermediate results to another variable,
like this:

double temp = (yhi-ylo)/stepY +1;
long nbNiveau = temp;

This lets you examine the values yourself when debugging, instead of trying
to guess what's going on inside the program.

Hope this helps...

-Howard

thank you for your help.

Feb 15 '07 #32

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Steven | last post: by
3 posts views Thread by John | last post: by
87 posts views Thread by expertware | last post: by
3 posts views Thread by Mitchell Thomas | last post: by
5 posts views Thread by cpptutor2000 | last post: by
10 posts views Thread by weichaoliu | last post: by
3 posts views Thread by Chuck Renner | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.