Re: Stuck... Long day I think.. ;-)
| From: | Brian V Bonini | Date: | Tue, 20 May 2003 21:45:02 +0000 |
| Subject: | Re: Stuck... Long day I think.. ;-) | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-148330@lists.php.net to get a copy of this message | ||
On Fri, 2003-05-16 at 19:53, Frappy John wrote:
> > > One general rule of thumb is never to test a floating
> > > point number for equality.
Still stumped with this... Tried as everyone suggested by converting to
int by using *10 but still nada. Does PHP have a cast construct like C++
??
for($i = 1; $i <= 2.1; $i += 0.1) {
$p_val[] = $i;
}
echo "Protien (pr/kg/bodyweight):\n";
echo '<select name="protein">'."\n";
foreach($p_val as $v) {
$str = (($v * 10) != ($_POST['protein'] * 10)) ? "<option
value=\"$v\">" .
sprintf("%.1f", $v) . "g</option>" : "<option
value=\"$v\"
selected=\"selected\">" . sprintf("%.1f", $v) .
"g</option>";
echo $str ."\n";
}
echo "</select>\n";
I still can not get ((($v * 10) != ($_POST['protein'] * 10)) to evaluate
as false. I set up another loop to see what was going on
foreach($p_val as $v) echo '$v= ' . $v * 10 . ' - $_POST[\'protein\']=
"
. $_POST['protein'] * 10 . "\n";
which returns
$v= 10 - $_POST['protein']= 13
$v= 11 - $_POST['protein']= 13
$v= 12 - $_POST['protein']= 13
$v= 13 - $_POST['protein']= 13
$v= 14 - $_POST['protein']= 13
$v= 15 - $_POST['protein']= 13
$v= 16 - $_POST['protein']= 13
$v= 17 - $_POST['protein']= 13
$v= 18 - $_POST['protein']= 13
$v= 19 - $_POST['protein']= 13
$v= 20 - $_POST['protein']= 13
so I do not see why I can not check $v against $_POST['protein'] at this
point...
> >
> > Ah, I did not know that, thanks. So any suggestions for
> > how to approach this then? I guess I need to convert
> > those values to something else and test them??
>
> Right. Probably best to convert to integers (multiply by
> ten) and convert back for display. You could test the
> floating point for a range instead, but I think the
> simplest and fastest is to use integers.
>
> For more, see the box labeled:
> "Floating point precision" at:
>
> http://www.php.net/manual/en/language.types.float.php
>
> --Frappy.
>
> On Friday 16 May 2003 07:43 pm, Brian V Bonini wrote:
> > On Fri, 2003-05-16 at 19:19, Frappy John wrote:
> > > > I do the same thing here and it works fine:
> > > >
> > > > for($i = 250; $i <= 1000; $i += 250) {
> > > > $c_val[] = $i;
> > > > }
> > >
> > > . . .
> > >
> > > > I'm not seeing the difference in terms of
> > > > functionality.
> > >
> > > One big difference is your protein routine deals with
> > > decimals (floating point) and your calories routine
> > > deals with whole numbers (integers).
> > >
> > > One general rule of thumb is never to test a floating
> > > point number for equality.
> >
> > Ah, I did not know that, thanks. So any suggestions for
> > how to approach this then? I guess I need to convert
> > those values to something else and test them??
> >
> > > > I can not get ($v != $_POST['protein']) to evaluate
> > > > as true.
> > >
> > > I would think it would *always* evaluate as true (i.e.
> > > always != ). Is that what you meant to say?
> >
> > Yup, that's what I meant, like I said, "long day"... ;-)