Fix significant figures and round off for trigonometric values#61
Fix significant figures and round off for trigonometric values#61nswarup14 wants to merge 12 commits intosugarlabs:masterfrom
Conversation
|
Thanks. Reviewed 815959b.
Why six digits?
A repeating code pattern you have used is counting the number of
significant digits, limiting to six, and then rounding. Please
refactor this.
significant() uses a local variable "integral", which has ambiguous
meaning (wiktionary), would "integer" be more appropriate?
round() is removed from body of source, but not from function table,
and continues to be used in 13 other places, so the built-in function
is now used. Commit message doesn't tell me if this was intentional.
docstring of xor() was unnecessarily changed.
Our documentation, and possibly also teacher lesson plans we don't
have access to, still refer to ceil() and round().
Please either leave these functions available, or update
https://help.sugarlabs.org/en/calculate.html
|
Yes, I think so too. I wasn't aware of the ambiguous meaning for "integral".
I have corrected that. Might have slipped me the first time.
Yes, now the built-in function for I have updated the
I have asked this on the issue. Based on consensus, I shall refactor the code Thanks! |
|
Tested. Same review comments as @quozl |
58d9a0c to
f01497a
Compare
|
@quozl @pro-panda I have also removed the condition of displaying at most upto 6 decimal figures. Thanks |
|
Thanks. Tested f01497a. With number of shown digits set to 6, calculation With number of shown digits set to 15, calculation With number of shown digits set to 6, calculation With number of shown digits set to 6, calculation I'm having trouble assessing this pull request because the target has changed along the way, and so the commit messages are out of date. Could you write a new commit message please? Either as a comment marked as a proposed commit message, or if you want to do it the git way; revert all five commits in reverse order, then make a new commit with the changes and the complete commit message. Let me know if you need more detail on that procedure. |
Yes, this is because the calculated result itself is 70.7106781187. Hence the displayed result is also the same.
No, because the the result calculated is 70.7106781187, the displayed result is just the first 6 digits i.e 70.7106. The result is rounded off only when it is computed. However, the display result is computed is differently.
No. This is because 123.456 + 0.0007 yields 123.4567 and the truncated result is 123.456. The reason being same above. The present patch hopes to correct the noise introduced in trigonometric calculations by computing the number of significant figures and then rounding off. However, the displayed result is computed differently. The rounding off is not a part of truncating the final display result, hence 123.4567, even with the number of shown digits as 6, is displayed as 123.456 and not 123.457. |
|
Swarup and I had a call. Summary: |
trignometric values
f01497a to
7565cf7
Compare
|
@quozl @pro-panda Rather than modifying the result during calculation, the result is rounded off just before display. The following evaluates the user experience.
eg: With number of shown digits set to 6, calculation sin(45) yields 0.707106 Thanks! |
|
Thanks. Reviewed, did some testing. Please do more testing to ensure that this change does not break anything else |
|
Thanks. Tested 7565cf7 on Ubuntu 19.04. Results seem okay. Wish we had a comprehensive test case to run. What do you plan for FIXME: Should this case be handled differently? |
|
@quozl For example, if 1234.5678 was the answer to a problem and the number of shown digits property was set to At the moment, no manipulation of the result takes place for such cases. I propose that, we can perhaps round of the figure to a certain number of decimal places (which will be a predefined number) without affecting the integer part of the answer. We could even display the result with no manipulation, as it is being done now Thanks |
|
I'm not sure I understand. In the example you give, 1234.5678 should be shown as 1234.568 when number of shown digits is three? At the moment it is shown as 1234.5678. |
|
@quozl What I meant in the FIXME: is that, in cases when the length of the integer part of the answer exceeds the property set, which in this case is I wasn't sure what would be the ideal user behaviour in such cases, hence I added a comment to discuss it later. Thanks! |
|
Thanks. I'm still a little lost. Commit message for 7565cf7 is much improved, and I can see what you plan for how it is to work, but can you explain in the commit message how it used to work? |
7565cf7 to
34c0230
Compare
Based on the number of shown digits parameter, the displayed result is rounded off to the right number of decimal places. If the value lies between (-1, 1), then the value is directly rounded off. If the value is otherwise, then the length integer part of the result is calculated and the result is rounded to the remaining decimal places. If the length of the integer part of the result is greater than the number of digits shown parameter, the result is displayed unchanged/unmodified. eg: With number of shown digits set to 6, calculation sin(45) yields 0.707106 eg: With number of shown digits set to 15, calculation sin(45)*100 yields 70.7106781187. This is because the length of the result calculated itself is 12. eg: With number of shown digits set to 6, calculation sin(45)*100 yields 70.7107 eg: With number of shown digits set to 6, calculation 123.456+0.0007 yields 123.457 How it used to work before the patch For non-trignometric decimal calculations, the display result was truncated to the number of shown digits. No rounding off used to take place. For trignometric decimal calculations, the result is once again truncated without rounding off. eg: With number of shown digits set to 9, cos(90) yields 6.12323399x10^-17 eg: With number of shown digits set to 3, cos(90) yields 6.12x10^-17 eg: With number of shown digits set to 6, sin(45) yields 0.707106 eg: With number of shown digits set to 15, sin(45)*100 yields 70.7106781186547 eg: With number of shown digits set to 15, sin(45) yields 0.707106781186547 eg: With number of shown digits set to 6, calculation 123.456+0.0007 yields 123.456 eg: With number of shown digits set to 6, calculation sin(45)*100 yields 70.7106
|
Yes, I have updated the commit message with more test cases from 475ea8b. Interestingly, after I do the round off operation in I'm yet to find a cause for this unexpected behavior. Thanks |
|
Look at the Python source code. It is more accurate than the documentation. My guess is that the number is converted into a shorter float type prior to formatting as decimal. You can use |
34c0230 to
c308646
Compare
…he display result. Updated test cases eg: With number of shown digits set to 15, sin(45)*100 yields 70.7106781186547 eg: With number of shown digits set to 15, sin(45) yields 0.707106781186547
c308646 to
d9c98ea
Compare
|
@quozl I replaced the |
|
Please find out why |
|
Also, please resolve conflicts 😺 |
|
@pro-panda |
|
Taken from Python 2 docs,
FYI, this behavior does not occur in Python 3 str() |
|
Looking at the overall change relative to master, you are still adding a FIXME comment. I take it you're not yet finished? |
In the case where the integer part of a result is greater than the number of shown digits property, the result is rounded off to those many decimal places.
db4a6f8 to
caa7071
Compare
|
Thanks. Reviewed. There are some unnecessary parentheses added |
|
@quozl |
|
Thanks. Reviewed 1d9259e.
@@ -206,7 +206,7 @@ ceil.__doc__ = _('ceil(x), return the smallest integer larger than x.')
def cos(x):
- if(_scale_angle(x) % (math.pi/2) == 0 and _scale_angle(x) % (math.pi) != 0):
+ if _scale_angle(x) % math.pi / 2 == 0 and _scale_angle(x) % math.pi != 0:
return 0
return math.cos(_scale_angle(x))
cos.__doc__ = _(and here @ -510,9 +510,9 @@ sub.__doc__ = _('sub(x, y), return x - y')
def tan(x):
- if(_scale_angle(x) % (math.pi) == 0):
+ if _scale_angle(x) % math.pi == 0:
return 0
- if(_scale_angle(x) % (math.pi/2) == 0 and _scale_angle(x) % (math.pi) != 0):
+ if _scale_angle(x) % math.pi / 2 == 0 and _scale_angle(x) % math.pi != 0:
return "Infinity"
return math.tan(_scale_angle(x))
tan.__doc__ = _(
See Python operator precedence.
|
1d9259e to
5890827
Compare
|
@quozl
Isn't the vice-versa true? If a number is divisible by, a variable called x, then it will be definitely divisible by (x/2). Thanks |
|
Let x be 10 instead of Existing code pattern has two conditions and-ed together; here's a truth table; for n in range(25):
a = (n % (10 / 2) == 0)
b = (n % 10 != 0)
print (n, a, b, a and b)A simplified code pattern; for n in range(25):
print (n, ((n + 5) % 10 == 0))Same results. if (_scale_angle(x) + math.pi / 2) % math.pi == 0:I've not tested this change, I'm speculating and asking you to comment. |
|
@quozl Thanks |
|
@quozl @chimosky @Saumya-Mishra9129 Without applying the changes in this PR, what does the buttons 3,6,9 do? I tried tan(56) it returned 1.4825609685127403 irrespective of 3,6,9 being selected. Seems like the buttons have no effect on the number of digits displayed. |
Yes, that's the issue. #48 |

Fixes #48
Changes
functions.pyUpdates
cos(90),sin(0).tan(90)returns infinity as the answer rather than an arbitrary large valueTested on Ubuntu 18.04 and Sugar v0.114