OODBBean->fresh() returns different double precision between PHP 7.4 and 8.2.

41 views
Skip to first unread message

Aaron Cooper

unread,
May 31, 2023, 6:04:34 AM5/31/23
to redbeanphp
Versions
Redbean: 5.7.4
PHP: 7.4.33 & 8.2.6
MySQL: 5.7

I have the following code in a test (simplified):

```
$question = R::dispense('question');
$question->slackts = '1511996272.000308';
R::store($question);

// Some more stuff

$question = $question->fresh();
$this->assertNotNull('1511996272.000308', $question->slackts);
```
On PHP 7.4, the test passes fine. Exact same codebase on PHP 8.2, the assertion fails because $question->slackts = '1511996272.0003' (two decimals dropped).

Step debugging this on 8.2, I found that $question->slackts is set to '1511996272.000308' up until $question->fresh(); is called. After which $question->slackts is set to 1511996272.0003. I also find the record in the Database is indeed storing slackts as 1511996272.000308. The field is a Double.

I have stepped into OODBBean->fresh() and ran the expression in that method, and indeed the result of $this->beanHelper->getToolbox()->getRedBean()->load($this->getMeta( 'type' ), $this->properties['id'] ); is the truncated 1511996272.0003, despite the record in the DB showing 1511996272.000308 at the time.

I also checked ini_get('precision'); inside this test and it's set to 14.

Any thoughts on what the cause of this might be?



Aaron Cooper

unread,
May 31, 2023, 6:29:20 AM5/31/23
to redbe...@googlegroups.com
Sorry that assertNotNull should be assertEquals



--
You received this message because you are subscribed to the Google Groups "redbeanphp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/redbeanorm/d248aed0-d91e-4ff0-a0af-a5aa556d91den%40googlegroups.com.

Aaron Cooper

unread,
May 31, 2023, 7:56:41 PM5/31/23
to redbe...@googlegroups.com
The answer was actually in my question:


> I also checked ini_get('precision'); inside this test and it's set to 14.

1511996272.000308 is 16 significant digits. Setting precision to 16 corrected the issue. But I think we'll turn this DB field into a Decimal(16,6) instead

gabor

unread,
Jun 4, 2023, 11:22:35 AM6/4/23
to redbeanphp
Sorry for my late reply,

It was a very busy week for me. I see you already answered your own question. Good.
But maybe I should add this hint to the manual, in case somebody else experiences this issue.

cheers,
Gabor
Reply all
Reply to author
Forward
0 new messages