Marcus Kwok wrote:[color=blue]
>
> Jay Nabonne <jay_nabonne@sonicnospam.com> wrote:[color=green]
> > On Mon, 10 Oct 2005 20:02:10 +0000, Marcus Kwok wrote:
> >[color=darkred]
> >> double probs[401][2];[/color]
> >
> > struct Prob
> > {
> > double z;
> > double phi;
> > };
> >
> > Prob probs[401];
> >[color=darkred]
> >>
> >> exact values are not there, as follows:
> >>
> >>
> >> double z, phi, tmp, ratio;
> >> int indexL, indexH;
> >>
> >> /* z is calculated here */
> >>
> >> if (z <= -4.0)
> >> phi = probs[0][1];[/color]
> >
> > phi = probs[0].phi;
> >[color=darkred]
> >> else if (z >= 4.0)
> >> phi = probs[400][1];[/color]
> >
> > phi = probs[400].phi;
> >[color=darkred]
> >> else {
> >> tmp = (z + 4.0) / 0.02;
> >> indexL = floor(tmp);
> >> indexH = ceil(tmp);
> >> if (indexL == indexH)
> >> phi = probs[indexL][1];[/color]
> >
> > phi = probs[indexL].phi;
> >[color=darkred]
> >> else {
> >> ratio = (z - probs[indexL][0]) / 0.02;[/color]
> >
> > ratio = (z - probs[indexL].z) / 0.02;
> >[color=darkred]
> >> phi = probs[indexL][1] + ratio * (probs[indexH][1] - probs[indexL][1]);[/color]
> >
> > phi = probs[indexL].phi + ratio * (probs[indexH].phi - probs[indexL].phi);
> >[color=darkred]
> >> }
> >> }
> >> }
> >> }[/color][/color]
>
> Thanks. However, I was trying to avoid basic arrays and instead use a
> std::vector<>, though maybe it will be clearer to create a struct (as
> you did) instead of using a std::pair<>.
>
> The main thing I am trying to do is clean up the calculation of the
> index, and seeing if the exact value is in the table.
>[/color]
I don't think there is much you can do. It already is short, is easy to understand.
I really don't see a need to clean it up.
The only thing I would do is: I would check if it ever happens that IndexL equals
indexH. If it happens, how often does it happen? Due to round-off errors I don't think
it is likely that the calculation of ( z + 4.0 ) / 0.02 equals a whole number when
using double arithmetic. So there is no point in having an 'if' that is almost never
taken. But I may be wrong, only a test could show that.
--
Karl Heinz Buchegger
kbuchegg@gascad.at