Re: Unsigned Problems Revisited
| From: | Jason T. Greene | Date: | Tue, 22 Oct 2002 22:35:09 +0000 |
| Subject: | Re: Unsigned Problems Revisited | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-89940@lists.php.net to get a copy of this message | ||
On Tue, 2002-10-22 at 15:52, Andi Gutmans wrote:
> Jason knows that my stand on this one is that if we have >>> we really
> should also have <<< which will clash with here-docs. Suggestions for other
> operators such as his <<<< are a possibility.
>
I am open to alternatives as well.
> >3. Implement Unsigned Data types
> >---------------------------------
> >
> >It is possible to implement a uint data type, which could be made
> >available by either using settype(), or (uint), and by certain scenarios
> >decided by the engine. Ambiguities are solved by the following rule: All
> >mathematical operations performed on an unsigned type will result in an
> >unsigned type, unless overflow occurs. Floating point numbers, if
> >positive, could be converted to an unsigned type before right shift
> >occurs. The following code demonstrates this options:
> >
> ><?php
> >
> >$b = (uint) 1;
> >
> >//Set b = 1
> >$b = (1 << 31) >> 31;
> >
> >?>
>
> A strong -1. It would be completely ambiguous with signed integers on just
> about all language operations. Implementing a whole new type is too much IMO.
I took a look at what it would take to rule out the ambiguities, and the
implementation for this would be very similar to how doubles are
handled, an additional type check would need to be added to alot of the
core math op codes. Functions would still have to be aware of the
unsigned type to take advantage of it.
To demonstrate a possible confusing aspect of this option to a user
would be:
<?php
settype($blah, "uint");
$blah = php_function_that_returns_a_standard_long();
// From this point on blah is no longer unsigned
?>
>
> >4. Add a function which performs an unsigned right shift
> >--------------------------------------------------------
> >
> >This functionality could be moved to a function called
> >unsigned_right_shift(), or true_right_shift. I dislike this the most
> >because it is inconsistent, you lose the assign-op ability, and it
> >requires a lot more typing.
> >
> >ex
> ><?php
> >// Set b = 1
> >$b = true_right_shift(1 << 31, 31);
> >
> >// Set b = -1
> >$b = (1 << 31) >> 31;
> >?>
>
> I think this is a good solution for the few people who really need an
> unsigned shift.
The unsigned problems affects more then just those who use the unsigned
shift operator. If you use any function that tries to accept an unsigned
value, and that value being passed is a string, it will be truncated to
LONG_MAX upon conversion (Take a look at how ip2long() works now). This
can be solved by either creating a new function like
convert_to_unsigned_long(), or by modifying convert_to_long to perform a
strtoul instead of a strtol.
-Jason
> Andi
>