Drupal test run
---------------

Tests to be run:
  - Drupal\field\Tests\Number\NumberFieldTest

Test run started:
  Wednesday, July 29, 2015 - 14:09

Test summary
------------

Drupal\field\Tests\Number\NumberFieldTest                    270 passes   1 fails

Comments

alexpott’s picture

This is a float precision issue - mysql and postrges by default are 4 bytes but sqlite is 8.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new903 bytes

So let's test the 'number_formatter' rather than the database backend. But to be honest this issue makes me think we should remove the float field because its behaviour is so unpredictable.

This passes on pgsql, mysql and sqlite.

alexpott’s picture

StatusFileSize
new1.34 KB
new1.59 KB

Alternatively we could just make all float fields big. Which would have the advantage of making float fields less surprising.

amateescu’s picture

Another option would be to add some hand-holding code for the float field type in the 'number' widget.

alexpott’s picture

@amateescu I'm not sure what you mean?

amateescu’s picture

StatusFileSize
new1.02 KB

I was thinking of something like this.

alexpott’s picture

+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php
@@ -73,6 +73,14 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
+    // Ensure that floating point numbers are represented correctly to account
+    // for differences in various storage engines. For example, SQLite stores
+    // 8-byte IEEE floating point numbers by default, while MySQL and PostgreSQL
+    // store 4-byte floating point numbers.
+    if ($this->fieldDefinition->getType() == 'float') {
+      $value = sprintf ("%.2f", $value);
+    }

But this does not work as you expect :)... 3.141 would be stored correctly in all engines in HEAD. The point can float :)

amateescu’s picture

Right, it was just an idea and the patch was to show you what I mean :P

I think that the patch in #3 is most likely the better option here.

Status: Needs review » Needs work

The last submitted patch, 6: 2542132-6.patch, failed testing.

amateescu’s picture

Status: Needs work » Reviewed & tested by the community

I should've posted a -do-not-test patch :/ #3 is RTBC if it passes on all three db drivers.

amateescu’s picture

I added DrupalCI test requests for #3 and NumberFieldTest passed on both:

https://dispatcher.drupalci.org/job/default/3422/consoleFull (sqlite)
https://dispatcher.drupalci.org/job/default/3423/consoleFull (postgres)

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +D8 upgrade path
StatusFileSize
new1.59 KB

Just uploading #3 again so that the patch @amateescu rtbc'd is last.

I think 4 byte floats are designed to cause user frustration and we should consider either removing the field or making it be double instead. I have no idea how to explain to a user that 10000.1 and 3.141 are stored without alteration but 1234.5678 is stored as 1234.57. Moving to doubles mitigates this problem to a certain extent but I still wonder if we should just ditch floats in favour of decimals.

Testing the update path once a float field exists with data has shown something interesting. The update appears to be successful but it is not - the float is not changed to a double - and nothing is reported to the user. And then the update does not show up again! If there is no data the update works and the float is changed to a double as expected. I think in this instance we will need to write an upgrade function to handle the data change.

alexpott’s picture

Issue tags: +sqlite
StatusFileSize
new903 bytes

I think given the issues around this in order to have sqlite tests pass I think we should commit #2. Since that passes on all dbs and tests our code rather than database differences.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Agreed :)

catch’s picture

Status: Reviewed & tested by the community » Fixed

With the two follow-ups I think this is OK. Committed/pushed to 8.0.x, thanks!

  • catch committed 7e8fb08 on 8.0.x
    Issue #2542132 by alexpott, amateescu: Drupal\field\Tests\Number\...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.