Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix floating point modulo in floatDiffInDays() when $hoursDiff is a float #1690

Merged
merged 2 commits into from
Apr 10, 2019

Conversation

pflueg
Copy link
Contributor

@pflueg pflueg commented Apr 9, 2019

Hi there!

I experienced an issue in the Carbon\Traits\Difference::floatDiffInDays() function when the used function floatDiffInHours() is returning a decimal part.

Carbon is using the modulo operand directly. Unfortunately PHP is stripping any decimal parts beforehand and is using the numbers only as real integers:

php > $hoursDiff = 48.13;
php > var_dump($hoursDiff % 24);
int(0)

For floating-point modulo the library needs to use the php function fmod().

php > $hoursDiff = 48.13;
php > var_dump(fmod($hoursDiff, 24));
float(0.13)

In this PR i changed it, added a case to the unittests and adapted all expected values in the other assertions, that are now using the decimal part as well

Cheers o/

@kylekatarnls
Copy link
Collaborator

Thanks for your help, it seems correct. I will compare precision after/before to be sure and check where else we use modulo. It could be in the next minor release.

@kylekatarnls kylekatarnls merged commit 7808a1f into briannesbitt:master Apr 10, 2019
@kylekatarnls kylekatarnls added this to the 2.17 milestone Apr 10, 2019
@pflueg pflueg deleted the fix-floating-point-modulo branch April 10, 2019 06:44
@pflueg
Copy link
Contributor Author

pflueg commented Apr 17, 2019

@kylekatarnls : Do you already know when the next version is going to be released with this fix in it?

@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Apr 17, 2019

Before the end of the week. Now that Carbon is stable enough, I plan to release 1 minor version per month on Carbon 2.x.
Roadmap: #1681

@kylekatarnls
Copy link
Collaborator

2.17.0 released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants