# [RFC] Big Integer Support

- Thread: https://externals.io/message/77863
- Original archive: https://news-web.php.net/php.internals/77863
- Started by Andrea Faulds on 2014-10-10T21:33:52+00:00
- Messages: 58
- RFC: Big Integer Support (Withdrawn) — https://externals.io/rfc/bigint.md
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/77863.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #77863 — Andrea Faulds — 2014-10-10T21:33:52+00:00

URL: https://externals.io/message/77863#77863

Good evening,

Since I don’t want this to languish as a ‘Draft’ forever, despite the patch being incomplete, I am finally putting the Big Integer Support RFC “Under Discussion”.

The RFC can be found here: https://wiki.php.net/rfc/bigint

The patch is, as I mentioned, incomplete. Additionally, there are quite a few matters left to be decided (see Open Questions). However, I think I should put this formally under discussion now.

Any help with the patch (largely just updating extensions and the mountains of tests these changes break, though later I will need to deal with opcache) would be appreciated.

Thanks!
--
Andrea Faulds
http://ajf.me/

---

## #77867 — Andrea Faulds — 2014-10-10T23:28:12+00:00

URL: https://externals.io/message/77863#77867

> Is this the right approach to implement BIGINT?
> I don't see the use of GMP to implement something as simple as native 64
> bit numbers on 64 bit platforms as the right base.

Um, we already have this since the 64-bit patch.

> All we are missing is
> correctly handling two word data on a 32bit platform. The wrapping and
> everything still applies, but only on a 32 bit platform ... no need for
> the complication of GMP. The bit I'm looking at here IS using BIGINT as
> array keys without the problems of them changing to long strings and I
> don't see how GMP fixes that?

What you want is 64-bit data handling. This is arbitrary-bit data handling. It’s not a “wrong approach”.
--
Andrea Faulds
http://ajf.me/

---

## #77869 — Andrea Faulds — 2014-10-11T00:18:31+00:00

URL: https://externals.io/message/77863#77869

> > […]
> 
> So BIGINT on 32 bit platforms will be different to BIGINT on 64 bit
> platforms? BIGINT is a fix length number not a variable one …

“Bigints” typically refer to arbitrary-size integers, that is, their size is bounded only by the amount of RAM available.

I don’t know what you think a “bigint” is, but it’s different to everyone else.

--
Andrea Faulds
http://ajf.me/

---

## #77877 — Pierre Joye — 2014-10-11T13:36:47+00:00

URL: https://externals.io/message/77863#77877

>
> 
> > […]
handling. It’s not a “wrong approach”.
> > […]
size is bounded only by the amount of RAM available.
> > […]
everyone else.
>
> PLEASE rename the page name for this and stop using BIGINT for it ...
>
> BIGINT is the SQL99-compliant 64-bit signed integer type
>
> http://www.firebirdsql.org/refdocs/langrefupd25-bigint.html
> http://www.postgresql.org/docs/9.1/static/datatype-numeric.html
> http://dev.mysql.com/doc/refman/5.5/en/integer-types.html
>
> BIGINT is very much an 8 byte data value which we have been struggling
> with on PHP for some time. Now that it's available in 64bit builds, we
> need a simple transparent way to maintain that in 32bit builds ...
>
> If you are proposing BigInteger that is something else
> http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html and
> GMP already provides that. Miking it up with the BIGINT standard which
> is something we DO need is the problem here ...

No.

The problem is on the other end of the wire. Big integer concept, in a
programming language context, or libraries (openssl, Crypto++, etc), is
pretty clear.

Can we now move to discussions about the implementations and open
questions? That will be much more useful.

---

## #77879 — Lester Caine — 2014-10-11T16:05:59+00:00

URL: https://externals.io/message/77863#77879 · In reply to: #77877

> 
> On Oct 11, 2014 4:14 PM, "Lester Caine" <lester@lsces.co.uk
> <mailto:lester@lsces.co.uk>> wrote:
> > […]
> data handling. It’s not a “wrong approach”.
> > […]
> size is bounded only by the amount of RAM available.
> > […]
> everyone else.
> > […]
> 
> No.
> 
> The problem is on the other end of the wire. Big integer concept, in a
> programming language context, or libraries (openssl, Crypto++, etc), is
> pretty clear.
> 
> Can we now move to discussions about the implementations and open
> questions? That will be much more useful.

BIGINT is a cleanly defined concept and something we have had to cope
with for some time since PHP does not support 64 bit integers cleanly.
Now that 64 bit builds support a clean 64 bit integer, the problem
arises that 32 bit builds will handle this in the old way.

I'll be quite frank,, and the idea of BigInteger was not something I was
even aware of but it does NOT use 'bigint' as a shorthand for it, so
mixing the two concepts up does not make any sense!

What needs tidying up here is the confusion being created for those of
us who use 'BIGINT' day in day out. GMP provides larger range integer
values, while the proposed rfc is ONLY describing handling 64 bit wrap
around ... which is what is needed to tidy up bigint handling on 32bit
platforms. There are two conflicting requirements which this rfc is
mixing up. Creating an unlimited integer size via GMP should correctly
fix all of the knock on effects such as shift on the whole integer
rather than just 64bits of it. While on the other hand, we need a clean
bigint alternative for 32bit platforms to mirror that on 64bit builds
... which does not involve GMP

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #77880 — johannes@schlueters.de — 2014-10-11T18:22:46+00:00

URL: https://externals.io/message/77863#77880 · In reply to: #77879

> BIGINT is a cleanly defined concept and something we have had to cope
> with for some time since PHP does not support 64 bit integers cleanly.
> Now that 64 bit builds support a clean 64 bit integer, the problem
> arises that 32 bit builds will handle this in the old way.

        Arbitrary-precision arithmetic
        From Wikipedia, the free encyclopedia
          (Redirected from Bigint)
        http://en.wikipedia.org/wiki/Bigint
        
        In computer science, arbitrary-precision arithmetic, also called
        bignum arithmetic, multiple precision arithmetic, or sometimes
        infinite-precision arithmetic, indicates that calculations are
        performed on numbers whose digits of precision are limited only
        by the available memory of the host system. This contrasts with
        the faster fixed-precision arithmetic found in most arithmetic
        logic unit (ALU) hardware, which typically offers between 8 and
        64 bits of precision
        [...]
        
But that's bikesheding, if we like we can call it also
yellow-blue-striped birds with red dots. Please discuss the contents not
the painting. Thanks.

johannes

---

## #77881 — Lester Caine — 2014-10-11T19:39:04+00:00

URL: https://externals.io/message/77863#77881 · In reply to: #77880

> But that's bikesheding, if we like we can call it also
> yellow-blue-striped birds with red dots. Please discuss the contents not
> the painting. Thanks.

I AM discussing the content ... it will not fix the problem that was
ORIGINALLY being discussed it just changes to yet another abstract
concept which can not be used as a 'bigint' index on arrays in 32bit
builds of PHP ...

It may be that we now need two rfc's one for BigInteger and one to fix
the problem of BIGINT ...

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #77887 — Rowan Collins — 2014-10-12T01:05:42+00:00

URL: https://externals.io/message/77863#77887 · In reply to: #77881

> it will not fix the problem that was
> ORIGINALLY being discussed

Your assuming that Andrea's RFC is a response to that particular 
discussion, rather than simply assuming that it's a valid discussion in 
its own right.

It's perfectly possible to have two discussions at once, even about 
slightly-related things.

-- 
Rowan Collins
[IMSoP]

---

## #77888 — Rowan Collins — 2014-10-12T01:15:46+00:00

URL: https://externals.io/message/77863#77888

> BIGINT is the SQL99-compliant 64-bit signed integer type

It's a matter of context. In C, and therefore in related discussions 
(which includes the internals of PHP), integers are referred to as 
"short" (for 16-bit), "long" (for 32-bit) and "long  long" (for 64-bit), 
but never as "big". SQL is unusual in calling a 32-bit integer "BigInt" 
rather than some variant of "long" or "32", and since we're not 
discussing databases here, it's of only marginal relevance.

I can see why, having spent more time in SQL than C, you might jump to 
the wrong meaning of "BigInt", and it might even be worth considering 
this potential confusion when writing the end-user documentation for 
this new feature, should it be implemented.

But, as others have said, that's really not an issue which should 
dominate the discussion at this stage.

-- 
Rowan Collins
[IMSoP]

---

## #77891 — Lester Caine — 2014-10-12T07:52:06+00:00

URL: https://externals.io/message/77863#77891 · In reply to: #77888

> 
> > […]
> 
> It's a matter of context. In C, and therefore in related discussions
> (which includes the internals of PHP), integers are referred to as
> "short" (for 16-bit), "long" (for 32-bit) and "long  long" (for 64-bit),
> but never as "big". SQL is unusual in calling a 32-bit integer "BigInt"
> rather than some variant of "long" or "32", and since we're not
> discussing databases here, it's of only marginal relevance.
> 
> I can see why, having spent more time in SQL than C, you might jump to
> the wrong meaning of "BigInt", and it might even be worth considering
> this potential confusion when writing the end-user documentation for
> this new feature, should it be implemented.
> 
> But, as others have said, that's really not an issue which should
> dominate the discussion at this stage.

There were only two things on my wish list for PHP6 ... Unicode and
proper handling of BIGINT. The use of a 64 bit value in databases as the
primary key has been long established and a related 64 bit element is
timestamp, While the timestamp can be handled as two 32 bit values (on
some databases!), the primary key is a 64 bit number. Many database
engines started using 64 bit builds years ago and have native 64bit
integers, but still handle that on 32bit builds as well. Bringing that
key into PHP has always been something of a gamble as to how it will be
represented, but the database drivers do seem to line up now.

You are right that *I* had miss interpreted Andrea's use of bigint but I
have always commented on it in the context of using a 64bit integer key
for arrays. In relation to the CURRENT discussion, if a 32bit build is
going to default to a 'BigInteger' string rather than a simple 64bit
value then there is a problem. I would rather THAT did not happen, but
it still leaves the problem of 64bit keys for arrays in 32bit builds.
There are many reasons why a 32bit primary key was limiting in databases
and while I have no doubt at some point it will be upgraded to 128bit, a
64bit value for timestamps and record counts will last long after most
of even you youngsters have past away? BigInteger is a PHP8 problem
64bit array keys is the PHP7 one!

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #77954 — Stas Malyshev — 2014-10-14T08:51:39+00:00

URL: https://externals.io/message/77863#77954 · In reply to: #77863

Hi!


> Since I don’t want this to languish as a ‘Draft’ forever, despite the
> patch being incomplete, I am finally putting the Big Integer Support
> RFC “Under Discussion”.
> 
> The RFC can be found here: https://wiki.php.net/rfc/bigint

This introduces new type, IS_BIGINT. However, given that GMP now
supports arithmetical operations, I wonder if it won't be easier to do
it in slightly different way, specifically create a hook that is going
to be called when an operation is about to cause over/underflow and let
GMP hook there and produce a GMP number (I'm not sure about the exact
details how to actually do it, so it's just an idea now, but if it makes
sense we can try to work out technical details).

Of course, this would require some rough edges to be polished, such as
what happens if you try to use it as int, or convert, etc. but this is
already present with IS_BIGINT too, and additionally we already have
conversion handlers for objects, which aren't consistently used in all
cases but can be made so. The benefit is we're not creating anything
completely new, we just improving how objects work.

This would also allow anybody who doesn't like GMP big integers easily
implement their own module to replace them.

Moreover, this also allows to make the support for bigints optional -
i.e., if you don't need bigints, you don't have to carry GMP and thus do
not have to be bound by its license.

What do you think?
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #77970 — Andrea Faulds — 2014-10-14T12:18:01+00:00

URL: https://externals.io/message/77863#77970 · In reply to: #77954

> 
> 
> Hi!
> 
> 
> > […]
> 
> This introduces new type, IS_BIGINT. However, given that GMP now
> supports arithmetical operations, I wonder if it won't be easier to do
> it in slightly different way, specifically create a hook that is going
> to be called when an operation is about to cause over/underflow and let
> GMP hook there and produce a GMP number (I'm not sure about the exact
> details how to actually do it, so it's just an idea now, but if it makes
> sense we can try to work out technical details).
> 
> Of course, this would require some rough edges to be polished, such as
> what happens if you try to use it as int, or convert, etc. but this is
> already present with IS_BIGINT too, and additionally we already have
> conversion handlers for objects, which aren't consistently used in all
> cases but can be made so. The benefit is we're not creating anything
> completely new, we just improving how objects work.
> 
> This would also allow anybody who doesn't like GMP big integers easily
> implement their own module to replace them.
> 
> Moreover, this also allows to make the support for bigints optional -
> i.e., if you don't need bigints, you don't have to carry GMP and thus do
> not have to be bound by its license.
> 
> What do you think?

I'm not sure what this would solve. Sure, you could just use objects instead of a new type, but both present exactly the same challenges. Adding a new type isn't hard in itself. The problem is updating everything which handles numbers and their associated tests. This doesn't make my job any easier. It also wouldn't cover a few places that a new type can, like constants. Another problem is this means that bigints are a separate thing from ints, meaning users have to worry about a new type which sometimes behaves differently. This isn't good. Under this RFC's proposal, however, bigints are a mere implementation detail. So far as the user cares, there are just ints.

Making it optional destroys most of the benefits of the RFC. Instead of reducing platform differences, it adds a massive new one. Now developers have to check whether or not bigints are enabled and have two different code paths. That's much worse than the status quo.

--
Andrea Faulds
http://ajf.me/

---

## #78039 — Stas Malyshev — 2014-10-14T17:25:12+00:00

URL: https://externals.io/message/77863#78039 · In reply to: #77970

Hi!

> I'm not sure what this would solve. Sure, you could just use objects
> instead of a new type, but both present exactly the same challenges.
> Adding a new type isn't hard in itself. The problem is updating
> everything which handles numbers and their associated tests. This

Exactly. Since objects are convertable to numbers (and to anything, in
fact) we get double profit here - we make objects work better and we
achieve big integer support. And we don't need to handle new type where
we don't need numbers

> doesn't make my job any easier. It also wouldn't cover a few places
> that a new type can, like constants. Another problem is this means

I'm not sure I see much case for bigint constants. Would be pretty hard
for me to come up with a case where you need such a constant, and if you
do, you could just have a string constant and convert it to GMP in runtime.

> that bigints are a separate thing from ints, meaning users have to
> worry about a new type which sometimes behaves differently. This
> isn't good. Under this RFC's proposal, however, bigints are a mere
> implementation detail. So far as the user cares, there are just
> ints.

No, they are not implementation detail - they are whole new type, which
means every extension and every piece of PHP code aware of types now
needs to know about it and needs special code to handle it. I.e. you
pass it to mysql - mysql needs to handle this type. You pass it to SOAP
- SOAP needs to handle this type. Etc. But if it's an object, they
already deal with objects, one way or another.

> Making it optional destroys most of the benefits of the RFC. Instead
> of reducing platform differences, it adds a massive new one. Now

I'm not saying we have to make it optional. I'm just saying it's possible.

> developers have to check whether or not bigints are enabled and have
> two different code paths. That's much worse than the status quo.

I don't see why you'd have two code paths. If you need bigints and they
are not there, then you just fail, like with any extension your code
needs and is not installed. If it's there, you just continue working.
All the code existing now doesn't need bigints, and even in the future
most code won't need it. But for some code it would just work like
before, only with unlimited range now.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #78040 — Lester Caine — 2014-10-14T17:48:35+00:00

URL: https://externals.io/message/77863#78040 · In reply to: #78039

> I don't see why you'd have two code paths. If you need bigints and they
> are not there, then you just fail, like with any extension your code
> needs and is not installed. If it's there, you just continue working.
> All the code existing now doesn't need bigints, and even in the future
> most code won't need it. But for some code it would just work like
> before, only with unlimited range now.

'bitinteger!'
I'm still waiting to see how we handle 'BIGINT' under this rfc since
that is something every database driver does need to handle.

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #78042 — Andrea Faulds — 2014-10-14T18:01:59+00:00

URL: https://externals.io/message/77863#78042 · In reply to: #78039

> Hi!
> 
> > […]
> 
> Exactly. Since objects are convertable to numbers (and to anything, in
> fact) we get double profit here - we make objects work better and we
> achieve big integer support. And we don't need to handle new type where
> we don't need numbers

Handling a new type in cases where we don’t need numbers isn’t really a problem.

> 
> > […]
> 
> I'm not sure I see much case for bigint constants. Would be pretty hard
> for me to come up with a case where you need such a constant, and if you
> do, you could just have a string constant and convert it to GMP in runtime.

Still, it’s inconvenient. More for developers to worry about.

> 
> > […]
> 
> No, they are not implementation detail - they are whole new type, which
> means every extension and every piece of PHP code aware of types now
> needs to know about it and needs special code to handle it.

No, only extensions. It is *completely* transparent to userland. That’s the whole point.

> I.e. you
> pass it to mysql - mysql needs to handle this type. You pass it to SOAP
> - SOAP needs to handle this type. Etc. But if it's an object, they
> already deal with objects, one way or another.

Yes, but they don’t handle large integer objects already. So you pass it a GMP object, it converts to a string, then that overflows and you end up with a float when it converts it to a number. Which isn’t what you wanted. Or, it handles it as a string, which is also not ideal, as while a string and an int may be the same thing to some extensions, they are not to others.

> 
> > […]
> 
> I don't see why you'd have two code paths. If you need bigints and they
> are not there, then you just fail, like with any extension your code
> needs and is not installed.

It’s not about “extensions your code needs”. If you need ext/gmp, you can already require it. This RFC is about removing cross-platform integer handling differences.

> All the code existing now doesn't need bigints, and even in the future
> most code won't need it. But for some code it would just work like
> before, only with unlimited range now.

No, but existing code does have to handle float overflow. If you allow that to optionally be int overflow, you now need to worry about handling both.

--
Andrea Faulds
http://ajf.me/

---

## #78043 — Andrea Faulds — 2014-10-14T18:03:26+00:00

URL: https://externals.io/message/77863#78043 · In reply to: #78040

> 
> > […]
> 
> 'bitinteger!'
> I'm still waiting to see how we handle 'BIGINT' under this rfc since
> that is something every database driver does need to handle.

If you mean 64-bit ints, this RFC enables them to work on 32-bit too with exactly the same semantics. No more float overflow. On a 64-bit machine, they’re IS_LONG internally, and on 32-bit machines they’re IS_BIGINT, but the user doesn’t need to worry, they both act the same.

Assuming I actually get round to updating the DB drivers.
--
Andrea Faulds
http://ajf.me/

---

## #78045 — Stas Malyshev — 2014-10-14T18:11:45+00:00

URL: https://externals.io/message/77863#78045 · In reply to: #78042

Hi!

> Still, it’s inconvenient. More for developers to worry about.

I still have no idea why one would need a bigint constant, could you
give an common example where you would do that?

> No, only extensions. It is *completely* transparent to userland.
> That’s the whole point.

I'm not sure how it can be completely transparent if it's a different
type. Is it still identifying as int? In this case, this is dangerous as
some functions may not be able to accept big integers when accepting int
arguments, but checks for is_int, etc. would pass.

> Yes, but they don’t handle large integer objects already. So you pass
> it a GMP object, it converts to a string, then that overflows and you
> end up with a float when it converts it to a number. Which isn’t what
> you wanted. Or, it handles it as a string, which is also not ideal,
> as while a string and an int may be the same thing to some
> extensions, they are not to others.

If it's not, the extension has to handle it, the same way it has to
handle bigint anyway if it makes difference for it. The point is many
common cases are already covered, e.g. if the extension just needs a
string, or if the bigint actually represents a small int, etc.

> It’s not about “extensions your code needs”. If you need ext/gmp, you
> can already require it. This RFC is about removing cross-platform
> integer handling differences.

But nothing changes there - it is still removing the diffs and it still
requires GMP. The only change is you're not paying for it if you don't
need it.

> No, but existing code does have to handle float overflow. If you
> allow that to optionally be int overflow, you now need to worry about
> handling both.

What's "float overflow"? I'm not sure I'm getting your point here. You
don't need to handle anything - if your code doesn't care about big
ints, you just do math as usual. If it does, then you have to check big
ints are there, then do math as usual but be aware that int can be now
of two different types. I don't see any difference from the RFC here.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #78047 — Andrea Faulds — 2014-10-14T18:24:57+00:00

URL: https://externals.io/message/77863#78047 · In reply to: #78045

> Hi!
> 
> > […]
> 
> I still have no idea why one would need a bigint constant, could you
> give an common example where you would do that?

The main point is why you should prohibit it. The point of bigints is to remove cross-platform integer differences. Why shouldn’t you here? Why should I conditionally do different things on 64-bit and 32-bit?


> > […]
> 
> I'm not sure how it can be completely transparent if it's a different
> type. Is it still identifying as int?

Yes.

> In this case, this is dangerous as
> some functions may not be able to accept big integers when accepting int
> arguments, but checks for is_int, etc. would pass.

We already have this danger for another type: boolean. phpng got rid of IS_BOOL in favour of IS_TRUE and IS_FALSE. If we can update everything to handle the IS_BOOL change, surely we can update everything to handle bigints, too.

> If it's not, the extension has to handle it, the same way it has to
> handle bigint anyway if it makes difference for it. The point is many
> common cases are already covered, e.g. if the extension just needs a
> string, or if the bigint actually represents a small int, etc.

Many common cases are easily covered by a new type anyway. You overestimate the effort, I have already done the work, it’s not much. Objects make nothing easier.

> 
> > […]
> 
> What's "float overflow”?

Beyond PHP_INT_MAX, integers magically become floats in PHP. They have done so for a long time.

> I'm not sure I'm getting your point here. You
> don't need to handle anything - if your code doesn't care about big
> ints, you just do math as usual.

Then get weird results when someone passes a large number in.

> If it does, then you have to check big
> ints are there, then do math as usual but be aware that int can be now
> of two different types. I don't see any difference from the RFC here.

The main point of the RFC is to make integers completely consistent across platforms and to remove the need to worry about overflow. Adding optional overflow to GMP means you still have to worry about it. It doesn’t solve anything. You can already use GMP for applications which explicitly need to use large numbers. This RFC doesn’t exist for that purpose.

--
Andrea Faulds
http://ajf.me/

---

## #78050 — Andrea Faulds — 2014-10-14T18:33:30+00:00

URL: https://externals.io/message/77863#78050

> If a 64bit build of PHP is using a simple integer key for a BIGINT key
> from the database, what will be the equivalent on a 32bit build?
> 
> It may be that we have to add code to the DB drivers to ensure that
> BIGINT remains a standard string conversion on both platforms in order
> to maintain consistent results. One being a simple integer key and the
> other a GMP based key IS a problem when the key is constructed from
> other shifted elements. This may be no more complicated than happens
> now, but is a real life situation that needs a consistent result.

So, we’re talking about array keys, right? Well, strings sometimes become integer keys, and sometimes become string keys in arrays. This is actually the same behaviour the RFC and patch currently have for bigints: if it’s in the range of a long, it’s an integer key, otherwise a string key.

So the handling would actually be the same as now for array keys.
--
Andrea Faulds
http://ajf.me/

---

## #78052 — Stas Malyshev — 2014-10-14T18:39:55+00:00

URL: https://externals.io/message/77863#78052 · In reply to: #78047

Hi!

> We already have this danger for another type: boolean. phpng got rid
> of IS_BOOL in favour of IS_TRUE and IS_FALSE. If we can update
> everything to handle the IS_BOOL change, surely we can update
> everything to handle bigints, too.

No, it's not the same thing at all. For bool, you still have only true
and false. For bigint, your function now should be able to handle
infinite integers internally, but what if it has fixed resources that
assume integers have fixed range? For extensions, it's a commonplace,
but even for user code that can happen. That means, any call that you do
to an internal function with int argument now could fail since the
internal function is unable to support bigint, and you can't even guard
for this since your code can not distinguish regular int from bigint. I
don't think it is a good situation.

> Then get weird results when someone passes a large number in.

Why would you get weird results? You describe some vague dangers but I
didn't see any concrete example of what is different.

> The main point of the RFC is to make integers completely consistent
> across platforms and to remove the need to worry about overflow.

Which does not change with my proposal.

> Adding optional overflow to GMP means you still have to worry about
> it. It doesn’t solve anything. You can already use GMP for

You seem to misunderstand what my proposal is. It doesn't add any
additional overflow, it just changes from using separate type
masquerading as int to using objects. All the rest stays the same.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #78055 — Andrea Faulds — 2014-10-14T19:44:43+00:00

URL: https://externals.io/message/77863#78055 · In reply to: #78052

> > […]
> 
> No, it's not the same thing at all. For bool, you still have only true
> and false. For bigint, your function now should be able to handle
> infinite integers internally, but what if it has fixed resources that
> assume integers have fixed range?

You throw an error. Just as plenty of functions already can’t handle ridiculously large integer arguments.

> For extensions, it's a commonplace,
> but even for user code that can happen. That means, any call that you do
> to an internal function with int argument now could fail since the
> internal function is unable to support bigint, and you can't even guard
> for this since your code can not distinguish regular int from bigint. I
> don't think it is a good situation.

If a function can’t support a large integer argument, this is usually for an obvious reason. I am not tormented daily in Python by the fact that I can’t seek by 2^69 bytes in a file, and I doubt any PHP developer would be.

> 
> > […]
> 
> Why would you get weird results? You describe some vague dangers but I
> didn't see any concrete example of what is different.

Integers beyond 2^64 (on 64-bit systems) or 2^32 (on 32-bit systems) overflow to floats and lose accuracy. Then, if they’re casted back to integers, are truncated silently and wrap around.

> 
> > […]
> 
> Which does not change with my proposal.

No, it does: There are now integers, and objects that represent large integers, which behave differently.

--
Andrea Faulds
http://ajf.me/

---

## #78056 — Andrea Faulds — 2014-10-14T19:46:14+00:00

URL: https://externals.io/message/77863#78056

> The real life situation is that databases have used a 64bit integer as
> the primary key for records in a table for a long time now. Loading
> these records into arrays using the primary key as the array key is a
> natural process but as you have identified currently once they go over a
> 32 bit value they switch from simple integer to strings. Upgrading PHP
> to natively support 64bit integers on 64bit platforms but leaving them
> as 32bit on 32bit platforms is creating an inconsistency which this rfc
> seems to be making more complex rather than less. This why I was simply
> looking for a 64bit integer type that works as a simple integer on 32bit
> platforms as well. GMP is overkill for this simple case and only needed
> when one actually needs integers bigger than 64bit, and normal
> programming does not need that in PHP7, just a clean 64bit integer.
> Currently we can do any necessary 64bit maths on the keys in the
> database, but for cross database working a consistent solution inside
> PHP would help.

Well, you also get 64-bit support for free with GMP. Maybe it’s “overkill”, but it does solve your use case.

If you really want to go and add 64-bit emulation to 32-bit builds, be my guest. But nobody’s gone and done that.
--
Andrea Faulds
http://ajf.me/

---

## #78060 — Andrea Faulds — 2014-10-14T19:56:52+00:00

URL: https://externals.io/message/77863#78060

> Just to break this down a bit:
> 
> 1) The RFC should probably aim to make array keys consistent across platforms. This is currently left as an unanswered question in the RFC, but it seems natural to include it if the stated aim is to eliminate inconsistencies.

It’s something I do have an answer for, but it’ll be a separate RFC which I need to write a patch for and have pass before this one.

> 2) By using an unlimited type, we open the door to constructing "integer"-keyed arrays from 128-bit or larger sources. We also eliminate weirdness encountered at the boundaries, such as $foo[PHP_MAX_INT] = 'hello'; $foo[] = 'world'; - see http://3v4l.org/69mif

That’s something which I would handle in the aforementioned not-yet-existant RFC.

> 3) On the other hand, an implementation might be possible that allowed 64-bit integers on 32-bit systems, but nothing more; this would potentially be both simpler and more efficient.

More efficient, as in faster? Sure, that’d be faster. Simpler? Not really. The main difficulty in this patch is updating thousands of tests and functions. The basics of it (the new type, changes to operators) are very easy.

> Implementing both is probably a bad idea, as some core code would then need to handle 3 different types of integer (native, emulated 64-bit, and GMP), but it could be looked at as an alternative if the current RFC is rejected on complexity or licensing grounds.

As all bigint ops are abstracted, you could modify the patch to implement 64-bit integers if you wanted. Granted, they’d be unnecessarily allocated their own memory, but it’d be doable.

--
Andrea Faulds
http://ajf.me/

---

## #78063 — Stas Malyshev — 2014-10-14T20:47:39+00:00

URL: https://externals.io/message/77863#78063 · In reply to: #78055

Hi!

> You throw an error. Just as plenty of functions already can’t handle
> ridiculously large integer arguments.

The problem is, if you function can handle the int range and you checked
for `is_int()` and everything worked fine - now it's broken because
 `is_int()` no longer implies fixed range and there's no way to check if
you're dealing with fixed-range number or infinite-range number.

> No, it does: There are now integers, and objects that represent large
> integers, which behave differently.

IS_INT and IS_BIGINT would necessarily behave differently too - since
some functions may support both and some only integers. Again, no change
here.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #78073 — Andrea Faulds — 2014-10-14T21:48:24+00:00

URL: https://externals.io/message/77863#78073 · In reply to: #78063

> > […]
> 
> The problem is, if you function can handle the int range and you checked
> for `is_int()` and everything worked fine - now it's broken because
> `is_int()` no longer implies fixed range and there's no way to check if
> you're dealing with fixed-range number or infinite-range number.

Yes, but you *can* check if the function errors. I don’t really think this is a massive problem. People will probably not realistically expect that all functions can accept really large numbers, whether that range cuts off at 2**64-1 or something more arbitrary. It’s a problem if `abs()` or sign() don’t work for bigints. It isn’t if `str_repeat()` doesn’t, because a similarly-sized non-bigint would error too.

> 
> > […]
> 
> IS_INT and IS_BIGINT would necessarily behave differently too - since
> some functions may support both and some only integers.

All functions would “support” both for integer arguments. But some might choose to reject bigints which are larger than the internal integer type the function uses… much like a function written for PHP currently might reject longs larger than the internal integer type the function uses.

> Again, no change
> here.

The point is the degree to which they can act the same. Objects can only go so far.
--
Andrea Faulds
http://ajf.me/

---

Next page: https://externals.io/message/77863.md?page=2
