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

# Help please, strange behavior

Howard
Guest
Posts: n/a

 02-15-2007

"Simply_Red" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

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

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

Simply_Red
Guest
Posts: n/a

 02-15-2007
On 15 fév, 16:33, "Howard" <(E-Mail Removed)> wrote:
> "Simply_Red" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) oups.com...
>
>
>
> >> >> > 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.....

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

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.

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post dakupoto@gmail.com C++ 1 04-04-2012 08:38 PM Steve Ferg Python 2 08-25-2010 01:56 PM Erland Javascript 6 09-06-2007 08:40 AM John ASP General 10 08-23-2006 05:22 PM Barry ASP .Net 2 11-04-2003 11:05 PM

Advertisments