Re: PHP Brainteasers 2011

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



what's the scope?

I have some brain-teasing functions i've been working on, but they are far
from bulletproof, but here is an example

function float_int($significand) {
    $sign = ($significand<0) ? true  : false;
    $significand = abs($significand);
    $drep = (decbin( (int) $significand));
    $frep = "";

    for($i = 0; $i <= 22; $i++) {
        $significand = ($significand - (int) $significand)*2;
        if($significand == 0) break;
        $frep .= ($significand >= 1) ? "1" : "0";
    }

    $significand = preg_replace("/^0*1/", "",
$drep.$frep);

    $significand = (strlen($significand) < 23) ? str_pad($significand, 23,
"0") : substr($significand, 0,
23);


    if($drep) {
        $exponent = decbin(126+strlen($drep));
    } else {
        $tmp = preg_split('/1/', $drep.$frep, 2, PREG_SPLIT_OFFSET_CAPTURE);
        $exponent = decbin((-$tmp[1][1])+127);
    }
    $f = bindec($exponent.$significand);
    return (!$sign) ? $f : $f^0xFFFFFFFF;
}

I guess the function name and variable names will offer some assistance in
determining what this does, but i think its an interesting brain-bender, not
very useful, because its pretty slow, but still :)

-- Alex --

--
The trouble with programmers is that you can never tell what a programmer is
doing until it’s too late.  ~Seymour Cray



On Fri, May 20, 2011 at 12:49 PM, Steve Staples <sstaples@xxxxxxxx> wrote:

> Just wondering if anyone has done anything for this?   I personally
> haven't had any "ideas" come to mind yet...
>
> Looking forward to seeing them!!!  (once they come)
>
>
> Steve
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux