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
=== modified file 'app/ubuntu-calculator-app.qml'
--- app/ubuntu-calculator-app.qml 2015-04-06 12:49:36 +0000
+++ app/ubuntu-calculator-app.qml 2015-04-24 19:58:34 +0000
@@ -102,6 +102,10 @@
102 }102 }
103103
104 function formulaPush(visual) {104 function formulaPush(visual) {
105 mathJs.config({
106 number: 'bignumber', // Choose 'number' (default) or 'bignumber'
107 precision: 64
108 });
105 // If the user press a number after the press of "=" we start a new109 // If the user press a number after the press of "=" we start a new
106 // formula, otherwise we continue with the old one110 // formula, otherwise we continue with the old one
107 if (!isNaN(visual) && isLastCalculate) {111 if (!isNaN(visual) && isLastCalculate) {
@@ -160,6 +164,10 @@
160 }164 }
161165
162 function calculate() {166 function calculate() {
167 mathJs.config({
168 number: 'bignumber', // Choose 'number' (default) or 'bignumber'
169 precision: 64
170 });
163 if ((longFormula === '') || (isLastCalculate === true)) {171 if ((longFormula === '') || (isLastCalculate === true)) {
164 errorAnimation.restart();172 errorAnimation.restart();
165 return;173 return;
@@ -175,6 +183,21 @@
175183
176 try {184 try {
177 var result = mathJs.eval(longFormula);185 var result = mathJs.eval(longFormula);
186
187 // Maximum length of the result number
188 var NUMBER_LENGTH_LIMIT = 12;
189
190 if (mathJs.format(result).toString().length > NUMBER_LENGTH_LIMIT) {
191 if (result.toExponential().toString().length > NUMBER_LENGTH_LIMIT) {
192 // long format like: "1.2341322e+22"
193 result = mathJs.format(result, {notation: 'auto', precision: NUMBER_LENGTH_LIMIT});
194 } else {
195 // short format like: "1e-10"
196 result = result.toExponential();
197 }
198 } else {
199 result = mathJs.format(result)
200 }
178 } catch(exception) {201 } catch(exception) {
179 // If the formula isn't right and we added brackets, we remove them202 // If the formula isn't right and we added brackets, we remove them
180 for (var i = 0; i < numberOfOpenedBrackets; i++) {203 for (var i = 0; i < numberOfOpenedBrackets; i++) {
@@ -205,6 +228,7 @@
205 id: mainStack228 id: mainStack
206229
207 Component.onCompleted: {230 Component.onCompleted: {
231
208 push(calculatorPage);232 push(calculatorPage);
209 calculatorPage.forceActiveFocus();233 calculatorPage.forceActiveFocus();
210 }234 }
211235
=== modified file 'tests/autopilot/ubuntu_calculator_app/tests/test_main.py'
--- tests/autopilot/ubuntu_calculator_app/tests/test_main.py 2015-03-26 21:16:14 +0000
+++ tests/autopilot/ubuntu_calculator_app/tests/test_main.py 2015-04-24 19:58:34 +0000
@@ -105,6 +105,18 @@
105 self._assert_result_is(u'1e−10')105 self._assert_result_is(u'1e−10')
106 self._assert_history_contains(u'0.000000001÷10=1e−10')106 self._assert_history_contains(u'0.000000001÷10=1e−10')
107107
108 def test_operation_on_large_numbers(self):
109 self.app.main_view.insert('99999999999*99999999999=')
110 self._assert_result_is(u'9.9999999998e+21')
111 self._assert_history_contains(u'99999999999×99999999999='
112 '9.9999999998e+21')
113
114 self.app.main_view.insert('*100=')
115
116 self._assert_result_is(u'9.9999999998e+23')
117 self._assert_history_contains(u'9.9999999998e+21×100='
118 '9.9999999998e+23')
119
108 def test_brackets_precedence(self):120 def test_brackets_precedence(self):
109 self.app.main_view.insert('2*')121 self.app.main_view.insert('2*')
110 self.app.main_view.press_universal_bracket()122 self.app.main_view.press_universal_bracket()
@@ -169,16 +181,16 @@
169181
170 def test_divide_with_infinite_number_as_result(self):182 def test_divide_with_infinite_number_as_result(self):
171 self.app.main_view.insert('1/3=')183 self.app.main_view.insert('1/3=')
172 self._assert_result_is(u'0.3333333333333333')184 self._assert_result_is(u'0.333333333333')
173 self._assert_history_contains(u'1÷3=0.3333333333333333')185 self._assert_history_contains(u'1÷3=0.333333333333')
174186
175 def test_operation_on_infinite_number(self):187 def test_operation_on_infinite_number(self):
176 self.app.main_view.insert('5/3=')188 self.app.main_view.insert('5/3=')
177 self._assert_result_is(u'1.6666666666666667')189 self._assert_result_is(u'1.666666666667')
178 self._assert_history_contains(u'5÷3=1.6666666666666667')190 self._assert_history_contains(u'5÷3=1.666666666667')
179191
180 self.app.main_view.insert('-1=')192 self.app.main_view.insert('-1=')
181 self._assert_result_is(u'0.6666666666666667')193 self._assert_result_is(u'0.666666666667')
182194
183 def test_square(self):195 def test_square(self):
184 self.app.main_view.insert('4')196 self.app.main_view.insert('4')
@@ -289,6 +301,11 @@
289 self._assert_result_is(u'−66')301 self._assert_result_is(u'−66')
290 self._assert_history_contains(u'66i×i=−66')302 self._assert_history_contains(u'66i×i=−66')
291303
304 def test_floating_point_round_error(self):
305 self.app.main_view.insert('0.1+0.2=')
306 self._assert_result_is(u'0.3')
307 self._assert_history_contains(u'0.1+0.2=0.3')
308
292 def _assert_result_is(self, value):309 def _assert_result_is(self, value):
293 self.assertThat(self.app.main_view.get_result,310 self.assertThat(self.app.main_view.get_result,
294 Eventually(Equals(value)))311 Eventually(Equals(value)))

Subscribers

People subscribed via source and target branches