Reply to Re: & behaves differently

Your name:

Reply:


Posted by fritz-bayer@web.de on 03/06/06 10:46

ColdShine wrote:
> > //unsigned shift right
> > function fillWithZeroes($a, $b)
> > {
> > $z = hexdec(80000000);
> > if ($z & $a)
> > {
> > $a = ($a>>1);
> > $a &= (~$z);
> > $a |= 0x40000000;
> > $a = ($a>>($b-1));
> > }
> > else
> > {
> > $a = ($a>>$b);
> > }
> > return $a;
> > }
>
> This should handle an arbitrary number of bits for $a, allowing $b to be
> greater than the number of machine word bits.
>
> // Assuming 32-bit (1 sign) integer length.
> function fillWithZeroes($a, $b)
> {
> // In PHP you could use is_float($a), but I don't know
> // the equivalent Perl function.
> if ($a > 0x7FFFFFFF)
> {
> // It is some FP number, must emulate shift with division.
> $d = exp(2, $b);
> $a = round($a / $d);
> // Return int if possible.
> if ($a <= 0x7FFFFFFF)
> $a = (int)$a;
> }
> else
> $a >>= $b;
> return $a;
> }
>
> This yelds correct results as long as FP numbers have enugh precision. This
> is obviously NOT a safe assumption; yet if FP numbers have 53 + 1
> significand bits (64-bits IEEE FP), this fillWithZeroes should be able to
> handle numbers from 0 to 2^54 - 1.
>
> --
> ColdShine
>
> "Experience is a hard teacher: she gives the test first, the lesson
> afterwards." - Vernon Sanders law

Hi Coldhine, thanks for the workaround. I had to slightly change the
code, since perl does not have a rounding function and have now:

sub zeroFill2()
{
my ($a, $b) = @_;
# In PHP you could use is_float($a), but I don't know
# the equivalent Perl function.
if ($a > 0x7FFFFFFF)
{
# It is some FP number, must emulate shift with division.
$d = 2 ** $b;
$a = round($a / $d);

# Return int if possible.
if ($a <= 0x7FFFFFFF)
{
return int($a);
}
}
else
{
$a = ($a >> $b);
}
return $a;

}

sub round()
{
my ($number) = @_;
return int($number + .5 * ($number <=> 0));
}

Problem still remains so. This function returns 679118 instead of the
needed 154830. What should I change, so that it will return the right
numbers?

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация