[TIMOB-14194] TiAPI: Problem with floating point numbers in Titanium
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Medium |
Status | Closed |
Resolution | Invalid |
Resolution Date | 2018-07-11T20:39:45.000+0000 |
Affected Version/s | Release 3.1.0, Release 3.2.0 |
Fix Version/s | n/a |
Components | iOS |
Labels | n/a |
Reporter | Calvin Chan |
Assignee | Pedro Enrique |
Created | 2013-06-07T09:13:03.000+0000 |
Updated | 2018-07-11T20:39:45.000+0000 |
Description
*Problem*
The output of floating point values in Titanium differ from browser JavaScript parsers such as [jsbin.com](http://www.jsbin.com/).
For example, Ti.API.info(94.1) displays 94.0999999
On jsbin.com the output of alert(94.1) is 94.1
*Note*
Since I have to input floating number for some modules, I must use floating number solution. I can't use a string method. I have also tried parseFloat(x.toFixed(1)), Math.round(x*10)/10 etc. The output was the same.
*Test case*
var win = Ti.UI.createWindow({
backgroundColor : "#FFF"
});
for (var i = 0; i < 50; i++)
Ti.API.info(94.1);
win.open();
*Extended test case*
var win = Ti.UI.createWindow({
backgroundColor : "#FFF"
});
for (var i = 0; i < 1000; i++)
Ti.API.info(i + 0.1);
for (var i = 0; i < 1000; i++)
Ti.API.info(i + 0.2);
for (var i = 0; i < 1000; i++)
Ti.API.info(i + 0.3);
for (var i = 0; i < 1000; i++)
Ti.API.info(i + 0.4);
for (var i = 0; i < 1000; i++)
Ti.API.info(i + 0.5);
for (var i = 0; i < 1000; i++)
Ti.API.info(i + 0.6);
for (var i = 0; i < 1000; i++)
Ti.API.info(i + 0.7);
for (var i = 0; i < 1000; i++)
Ti.API.info(i + 0.8);
for (var i = 0; i < 1000; i++)
Ti.API.info(i + 0.9);
win.open();
*Log - Unusual Titanium values*
[INFO] : 64.09999999999999
[INFO] : 65.09999999999999
[INFO] : 66.09999999999999
[INFO] : 67.09999999999999
[INFO] : 68.09999999999999
[INFO] : 69.09999999999999
...
[INFO] : 70.09999999999999
[INFO] : 71.09999999999999
[INFO] : 72.09999999999999
[INFO] : 73.09999999999999
[INFO] : 74.09999999999999
[INFO] : 75.09999999999999
[INFO] : 76.09999999999999
[INFO] : 77.09999999999999
[INFO] : 78.09999999999999
[INFO] : 79.09999999999999
[INFO] : 80.09999999999999
[INFO] : 81.09999999999999
[INFO] : 82.09999999999999
[INFO] : 83.09999999999999
[INFO] : 84.09999999999999
[INFO] : 85.09999999999999
[INFO] : 86.09999999999999
[INFO] : 87.09999999999999
[INFO] : 88.09999999999999
[INFO] : 89.09999999999999
[INFO] : 90.09999999999999
[INFO] : 91.09999999999999
[INFO] : 92.09999999999999
[INFO] : 93.09999999999999
[INFO] : 94.09999999999999
[INFO] : 95.09999999999999
[INFO] : 96.09999999999999
[INFO] : 97.09999999999999
[INFO] : 98.09999999999999
[INFO] : 99.09999999999999
...
[INFO] : 8.199999999999999
[INFO] : 9.199999999999999
...
[INFO] : 8.300000000000001
[INFO] : 9.300000000000001
...
[INFO] : 64.40000000000001
[INFO] : 65.40000000000001
[INFO] : 66.40000000000001
[INFO] : 67.40000000000001
[INFO] : 68.40000000000001
[INFO] : 69.40000000000001
[INFO] : 70.40000000000001
[INFO] : 71.40000000000001
[INFO] : 72.40000000000001
[INFO] : 73.40000000000001
[INFO] : 74.40000000000001
[INFO] : 75.40000000000001
[INFO] : 76.40000000000001
[INFO] : 77.40000000000001
[INFO] : 78.40000000000001
[INFO] : 79.40000000000001
[INFO] : 80.40000000000001
[INFO] : 81.40000000000001
[INFO] : 82.40000000000001
[INFO] : 83.40000000000001
[INFO] : 84.40000000000001
[INFO] : 85.40000000000001
[INFO] : 86.40000000000001
[INFO] : 87.40000000000001
[INFO] : 88.40000000000001
[INFO] : 89.40000000000001
[INFO] : 90.40000000000001
[INFO] : 91.40000000000001
[INFO] : 92.40000000000001
[INFO] : 93.40000000000001
[INFO] : 94.40000000000001
[INFO] : 95.40000000000001
[INFO] : 96.40000000000001
[INFO] : 97.40000000000001
[INFO] : 98.40000000000001
[INFO] : 99.40000000000001
...
[INFO] : 64.59999999999999
[INFO] : 65.59999999999999
[INFO] : 66.59999999999999
[INFO] : 67.59999999999999
[INFO] : 68.59999999999999
[INFO] : 69.59999999999999
[INFO] : 70.59999999999999
[INFO] : 71.59999999999999
[INFO] : 72.59999999999999
[INFO] : 73.59999999999999
[INFO] : 74.59999999999999
[INFO] : 75.59999999999999
[INFO] : 76.59999999999999
[INFO] : 77.59999999999999
[INFO] : 78.59999999999999
[INFO] : 79.59999999999999
[INFO] : 80.59999999999999
[INFO] : 81.59999999999999
[INFO] : 82.59999999999999
[INFO] : 83.59999999999999
[INFO] : 84.59999999999999
[INFO] : 85.59999999999999
[INFO] : 86.59999999999999
[INFO] : 87.59999999999999
[INFO] : 88.59999999999999
[INFO] : 89.59999999999999
[INFO] : 90.59999999999999
[INFO] : 91.59999999999999
[INFO] : 92.59999999999999
[INFO] : 93.59999999999999
[INFO] : 94.59999999999999
[INFO] : 95.59999999999999
[INFO] : 96.59999999999999
[INFO] : 97.59999999999999
[INFO] : 98.59999999999999
[INFO] : 99.59999999999999
...
[INFO] : 8.800000000000001
[INFO] : 9.800000000000001
...
[INFO] : 64.90000000000001
[INFO] : 65.90000000000001
[INFO] : 66.90000000000001
[INFO] : 67.90000000000001
[INFO] : 68.90000000000001
[INFO] : 69.90000000000001
[INFO] : 70.90000000000001
[INFO] : 71.90000000000001
[INFO] : 72.90000000000001
[INFO] : 73.90000000000001
[INFO] : 74.90000000000001
[INFO] : 75.90000000000001
[INFO] : 76.90000000000001
[INFO] : 77.90000000000001
[INFO] : 78.90000000000001
[INFO] : 79.90000000000001
[INFO] : 80.90000000000001
[INFO] : 81.90000000000001
[INFO] : 82.90000000000001
[INFO] : 83.90000000000001
[INFO] : 84.90000000000001
[INFO] : 85.90000000000001
[INFO] : 86.90000000000001
[INFO] : 87.90000000000001
[INFO] : 88.90000000000001
[INFO] : 89.90000000000001
[INFO] : 90.90000000000001
[INFO] : 91.90000000000001
[INFO] : 92.90000000000001
[INFO] : 93.90000000000001
[INFO] : 94.90000000000001
[INFO] : 95.90000000000001
[INFO] : 96.90000000000001
[INFO] : 97.90000000000001
[INFO] : 98.90000000000001
[INFO] : 99.90000000000001
Attachments
File | Date | Size |
---|---|---|
螢幕快照 2013-06-07 下午05.11.43.png | 2013-06-07T09:13:03.000+0000 | 64672 |
螢幕快照 2013-06-07 下午06.19.37.png | 2013-06-07T10:20:39.000+0000 | 24811 |
Tested and confirmed with iOS 6 simulator, Ti SDK 3.1 GA and 3.2 CI.
Yes, it only in iOS, and I have fixed it by modifying the module. Detail:http://stackoverflow.com/questions/16981033/bug-of-using-floating-number-in-titanium-sdk Thanks all.
Calvin, Have you submitted a PR for your fix?
Marking as Invalid an closing. This is just how floating point numbers work. If you need to represent dollar amounts or specific decimal values, then you need to handle that in a different way. The linked stack overflow post and answer go directly to that. There are libraries intended to handle precisely this use case: - https://github.com/dtrebbien/BigDecimal.js - https://github.com/MikeMcl/decimal.js/ - http://mathjs.org/docs/datatypes/bignumbers.html It's possible that someday this support will make it into a future JS spec, as V8 has just added BigInts: https://developers.google.com/web/updates/2018/05/bigint