Merge lp:~gang65/ubuntu-calculator-app/ubuntu-calculator-app-round-fix into lp:ubuntu-calculator-app

Proposed by Bartosz Kosiorek
Status: Merged
Approved by: Bartosz Kosiorek
Approved revision: 171
Merged at revision: 170
Proposed branch: lp:~gang65/ubuntu-calculator-app/ubuntu-calculator-app-round-fix
Merge into: lp:ubuntu-calculator-app
Diff against target: 112 lines (+46/-5)
2 files modified
app/ubuntu-calculator-app.qml (+24/-0)
tests/autopilot/ubuntu_calculator_app/tests/test_main.py (+22/-5)
To merge this branch: bzr merge lp:~gang65/ubuntu-calculator-app/ubuntu-calculator-app-round-fix
Reviewer Review Type Date Requested Status
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Bartosz Kosiorek Needs Information
Riccardo Padovani Needs Fixing
Review via email: mp+256745@code.launchpad.net

Commit message

Enable BigNumber to resolve issues with rounding floating point numbers

Description of the change

Enable BigNumber to resolve issues with rounding floating point numbers

To post a comment you must log in.
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Riccardo Padovani (rpadovani) wrote :

Great work Bartosz, thanks for working on this and finally find a way to enable that :-)

But there is an improvement to do: we need to truncate numbers when they are too long. Try to do 1/3 here and in trunk: you see there it will occupy too much space in the history!

review: Needs Fixing
Revision history for this message
Bartosz Kosiorek (gang65) wrote :

Thanks Riccardo.
Changing to BigNumber do not causing longer numbers.

Truncating numbers will not help with all cases: for example long formula.
My idea was to use few lines to display history: If formula is short then we will display it in one row, if not we will use few rows.

I will create bug report regarding to that.
I think we need analyse possible solutions, and select one which fit our need the best.

review: Needs Information
Revision history for this message
Bartosz Kosiorek (gang65) wrote :
Revision history for this message
Bartosz Kosiorek (gang65) wrote :

@Riccardo
You have right the length of the infinity numbers was increased dramatically.
I will try to fix that.

168. By Bartosz Kosiorek

Change default BigNumber precision from 64 to 16

169. By Bartosz Kosiorek

Format calculation result

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
170. By Bartosz Kosiorek

Add autopilot test to operate on big numbers

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Needs Fixing (continuous-integration)
171. By Bartosz Kosiorek

Fix pep8 warnings

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'app/ubuntu-calculator-app.qml'
2--- app/ubuntu-calculator-app.qml 2015-04-06 12:49:36 +0000
3+++ app/ubuntu-calculator-app.qml 2015-04-24 19:58:34 +0000
4@@ -102,6 +102,10 @@
5 }
6
7 function formulaPush(visual) {
8+ mathJs.config({
9+ number: 'bignumber', // Choose 'number' (default) or 'bignumber'
10+ precision: 64
11+ });
12 // If the user press a number after the press of "=" we start a new
13 // formula, otherwise we continue with the old one
14 if (!isNaN(visual) && isLastCalculate) {
15@@ -160,6 +164,10 @@
16 }
17
18 function calculate() {
19+ mathJs.config({
20+ number: 'bignumber', // Choose 'number' (default) or 'bignumber'
21+ precision: 64
22+ });
23 if ((longFormula === '') || (isLastCalculate === true)) {
24 errorAnimation.restart();
25 return;
26@@ -175,6 +183,21 @@
27
28 try {
29 var result = mathJs.eval(longFormula);
30+
31+ // Maximum length of the result number
32+ var NUMBER_LENGTH_LIMIT = 12;
33+
34+ if (mathJs.format(result).toString().length > NUMBER_LENGTH_LIMIT) {
35+ if (result.toExponential().toString().length > NUMBER_LENGTH_LIMIT) {
36+ // long format like: "1.2341322e+22"
37+ result = mathJs.format(result, {notation: 'auto', precision: NUMBER_LENGTH_LIMIT});
38+ } else {
39+ // short format like: "1e-10"
40+ result = result.toExponential();
41+ }
42+ } else {
43+ result = mathJs.format(result)
44+ }
45 } catch(exception) {
46 // If the formula isn't right and we added brackets, we remove them
47 for (var i = 0; i < numberOfOpenedBrackets; i++) {
48@@ -205,6 +228,7 @@
49 id: mainStack
50
51 Component.onCompleted: {
52+
53 push(calculatorPage);
54 calculatorPage.forceActiveFocus();
55 }
56
57=== modified file 'tests/autopilot/ubuntu_calculator_app/tests/test_main.py'
58--- tests/autopilot/ubuntu_calculator_app/tests/test_main.py 2015-03-26 21:16:14 +0000
59+++ tests/autopilot/ubuntu_calculator_app/tests/test_main.py 2015-04-24 19:58:34 +0000
60@@ -105,6 +105,18 @@
61 self._assert_result_is(u'1e−10')
62 self._assert_history_contains(u'0.000000001÷10=1e−10')
63
64+ def test_operation_on_large_numbers(self):
65+ self.app.main_view.insert('99999999999*99999999999=')
66+ self._assert_result_is(u'9.9999999998e+21')
67+ self._assert_history_contains(u'99999999999×99999999999='
68+ '9.9999999998e+21')
69+
70+ self.app.main_view.insert('*100=')
71+
72+ self._assert_result_is(u'9.9999999998e+23')
73+ self._assert_history_contains(u'9.9999999998e+21×100='
74+ '9.9999999998e+23')
75+
76 def test_brackets_precedence(self):
77 self.app.main_view.insert('2*')
78 self.app.main_view.press_universal_bracket()
79@@ -169,16 +181,16 @@
80
81 def test_divide_with_infinite_number_as_result(self):
82 self.app.main_view.insert('1/3=')
83- self._assert_result_is(u'0.3333333333333333')
84- self._assert_history_contains(u'1÷3=0.3333333333333333')
85+ self._assert_result_is(u'0.333333333333')
86+ self._assert_history_contains(u'1÷3=0.333333333333')
87
88 def test_operation_on_infinite_number(self):
89 self.app.main_view.insert('5/3=')
90- self._assert_result_is(u'1.6666666666666667')
91- self._assert_history_contains(u'5÷3=1.6666666666666667')
92+ self._assert_result_is(u'1.666666666667')
93+ self._assert_history_contains(u'5÷3=1.666666666667')
94
95 self.app.main_view.insert('-1=')
96- self._assert_result_is(u'0.6666666666666667')
97+ self._assert_result_is(u'0.666666666667')
98
99 def test_square(self):
100 self.app.main_view.insert('4')
101@@ -289,6 +301,11 @@
102 self._assert_result_is(u'−66')
103 self._assert_history_contains(u'66i×i=−66')
104
105+ def test_floating_point_round_error(self):
106+ self.app.main_view.insert('0.1+0.2=')
107+ self._assert_result_is(u'0.3')
108+ self._assert_history_contains(u'0.1+0.2=0.3')
109+
110 def _assert_result_is(self, value):
111 self.assertThat(self.app.main_view.get_result,
112 Eventually(Equals(value)))

Subscribers

People subscribed via source and target branches