Commit ce233f79 authored by Nicolas Wavrant's avatar Nicolas Wavrant

erp5_web_renderjs_ui: try to get a more precise float value on all platforms

Mathematically, 1/(x^y) is the same as x^-y, and despite float caculation the results is usually precise enough :
> Math.pow(10, -5)
0.00001
I tested this on few platforms : firefox 68.12, firefox 77.0, node v10.19.0

Unfortunately on (recent ?) chromes :
> Math.pow(10, -5)
0.000009999999999999999
Which is a horrible value to use as step (and it prevents the form submission as most floats the user will enter won't match the init value * x * the step.

For an unknown reason, I get a more consistent result using the formula "1 / Math.pow(10, 5)", which returns 0.00001 on all tested platforms.

If we find more issues in the future, we maybe should try building the step using strings. Using strings for manipulating floats is in reality widespread, and many languages do so to round floats (ie: https://github.com/python/cpython/blob/4a97b1517a6b5ff22e2984b677a680b07ff0ce11/Objects/floatobject.c#L925)

The precision of 5 is not random-picked, it is the minimum precision needed to manipulate prices for currencies with 2 digits, like euros.
parent 56d84e2f
......@@ -56,7 +56,7 @@
state_dict.append = "%";
}
if (!window.isNaN(state_dict.precision)) {
state_dict.step = Math.pow(10, -state_dict.precision);
state_dict.step = 1 / Math.pow(10, state_dict.precision);
state_dict.value = state_dict.value.toFixed(state_dict.precision);
}
if (!window.isNaN(state_dict.value)) {
......
......@@ -220,7 +220,7 @@
</item>
<item>
<key> <string>actor</string> </key>
<value> <string>zope</string> </value>
<value> <string>nicolas</string> </value>
</item>
<item>
<key> <string>comment</string> </key>
......@@ -234,7 +234,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>982.42549.32458.46916</string> </value>
<value> <string>983.35869.13162.60928</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -252,8 +252,8 @@
</tuple>
<state>
<tuple>
<float>1584702324.85</float>
<string>UTC</string>
<float>1599097800.89</float>
<string>GMT+2</string>
</tuple>
</state>
</object>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment