# RFC: Make GMP number work like PHP number

- Status: Withdrawn
- Authors: Yasuo Ohgaki, yohgaki@ohgaki.net
- Date: 2013-12-31
- Version: 0.9
- Original: https://wiki.php.net/rfc/gmp_number
- Page: https://externals.io/rfc/gmp_number
- 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

## Discussions on the mailing list

- [\[RFC\] Floating Point support in GMP extension](https://externals.io/message/70998.md) — started on 2014-01-04, 8 messages

---

## Introduction

GMP arithmetic with normal operator is added by [https://wiki.php.net/rfc/operator\_overloading\_gmp](https://externals.io/rfc/operator_overloading_gmp)

GMP numbers work like normal PHP number(int/float) with normal PHP arithmetic operators now.

This RFC proposes GMP number compatible with PHP number(int/float).

Current GMP implementation does not support GMP float/rational numbers. For the sake of completeness, GMP float support is needed at least. There are functions to be modified to treat GMP number as normal PHP numbers.

## Proposal

### Add GMP float support to GMP module

&lt;del&gt;Add GMP float support to GMP module.

- Add all GMP float API wrapper to module.
- GMP float API wrapper supports PHP int/float and GMP int/float. Conversion is done automatically.
- Default precision: 256 (?)&lt;/del&gt;

Extracted out to a separate RFC: [GMP Floating Point Support](https://externals.io/rfc/gmp-floating-point)

### Add math function compatibility

Add math function compatibility.

- Down grade GMP number (int/float) to PHP number (int/float) automatically.

Some other RFC may implement, full GMP number support in math functions, but this is out of this RFC scope.

### Add variable function compatibility

Add variable function compatibility.

- empty() - evaluated GMP int 0 and GMP float 0.0 as TRUE, otherwise FALSE.
- is\_scalar() - evaluated GMP number as TRUE.
- (anything else?)

## References

##### Current empty() and is\_scalar() behavior

\[yohgaki@dev php-src\]$ ./php-bin -a Interactive shell

```
php > $g = gmp_init(123);
php > var_dump($v);
object(GMP)#3 (1) {
  ["num"]=>
  string(1) "0"
}
php > $v = $g * 0;

php > var_dump($v, empty($v), is_scalar($v));
object(GMP)#4 (1) {
  ["num"]=>
  string(3) "0"
}
bool(false)
bool(false)
```

##### Current math function behavior

GMP float is not supported.

```
php > $f = 2.2;
php > $gf = gmp_init($f);

Warning: gmp_init(): Unable to convert variable to GMP - wrong type in php shell code on line 1
```

max()/min()

```
php > $g = gmp_init(123);
php > var_dump($v);
object(GMP)#4 (1) {
  ["num"]=>
  string(1) "0"
}
php > $g = gmp_init(5);
php > var_dump(max(1,2,$g));
object(GMP)#1 (1) {
  ["num"]=>
  string(1) "5"
}
php > var_dump(min(1,2,$g));
int(1)
php > var_dump(min($g, 6, 7));
object(GMP)#1 (1) {
  ["num"]=>
  string(1) "5"
}
```

##### Current Ruby and PHP behavior

```
[yohgaki@dev php-src]$ irb
2.0.0p247 :001 > 10 / 3
 => 3 
2.0.0p247 :002 > 10.0 / 3
 => 3.3333333333333335 
2.0.0p247 :003 > 
```

```
[yohgaki@dev php-src]$ php -a
Interactive shell

php > $g = gmp_init('10');
php > echo $g / 3;
3

php > echo 10 / 3;
3.3333333333333
```

## Backward Incompatible Changes

## Proposed PHP Version(s)

PHP 5.6 or later

## Open Issues

- Ways to implement GMP object evaluation in empty(), is\_scalar() 
    - Treat GMP object as special object
    - Implement scalar object
    - Implement autoboxing like feature
- How GMP number should behave (Like PHP or Ruby?)

## Future Scope

Math functions is better to support GMP float.

## Proposed Voting Choices

Yes/No

## Patches and Tests

I really appreciate if anyone could make patch for this RFC.

## Implementation

After the project is implemented, this section should contain

1. the version(s) it was merged to
2. a link to the git commit(s)
3. a link to the PHP manual entry for the feature

## References

- [https://wiki.php.net/rfc/operator\_overloading\_gmp](https://externals.io/rfc/operator_overloading_gmp)
- <https://externals.io/rfc/autoboxing>

## Rejected Features

Keep this updated with features that were discussed on the mail lists.
