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

Simply_Red
Guest
Posts: n/a

 02-14-2007
On 14 fév, 15:42, "Victor Bazarov" <(E-Mail Removed)> wrote:
> 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
> --

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);

Simply_Red
Guest
Posts: n/a

 02-14-2007
On 14 fév, 15:43, Rolf Magnus <(E-Mail Removed)> wrote:

> 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

Victor Bazarov
Guest
Posts: n/a

 02-14-2007
Simply_Red wrote:
> On 14 fév, 15:42, "Victor Bazarov" <(E-Mail Removed)> wrote:
>> 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
>> --

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

Victor Bazarov
Guest
Posts: n/a

 02-14-2007
Simply_Red wrote:
> On 14 fév, 15:43, Rolf Magnus <(E-Mail Removed)> wrote:
>
>> 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
--

Default User
Guest
Posts: n/a

 02-14-2007
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

Rolf Magnus
Guest
Posts: n/a

 02-14-2007
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".

Default User
Guest
Posts: n/a

 02-15-2007
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

Simply_Red
Guest
Posts: n/a

 02-15-2007
On 14 fév, 16:09, "Victor Bazarov" <(E-Mail Removed)> wrote:

> 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);
}

Simply_Red
Guest
Posts: n/a

 02-15-2007
On 14 fév, 17:13, "Default User" <(E-Mail Removed)> 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.
>
> Brian

Sorry, this abreviation is used in french.

Victor Bazarov
Guest
Posts: n/a

 02-15-2007
Simply_Red wrote:
> On 14 fév, 17:13, "Default User" <(E-Mail Removed)> 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.
>>
>> Brian

>
> Sorry, this abreviation is used in french.

Good to know. c.l.c++ is an English-speaking newsgroup, though.