-
-
Notifications
You must be signed in to change notification settings - Fork 219
Sheffield | 25-ITP-SEP | Declan Williams | Sprint 1 | Data groups #844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sheffield | 25-ITP-SEP | Declan Williams | Sprint 1 | Data groups #844
Conversation
…om filtered numbers
… and empty arrays
…ile preserving first occurrences
…s, returns the largest
…he closest one to zero
…gest decimal number
…ax and ignores non-numeric values
…nd adds numbers to total sum
| test("given an array with decimal numbers in returns the correct total sum", () => { | ||
| expect(sum([10.5, 20.2, 30.3])).toBe(61.0); | ||
| expect(sum([10.5, -20.2, 30.3])).toBe(20.6); | ||
| }); | ||
|
|
||
| // Given an array containing non-number values | ||
| // When passed to the sum function | ||
| // Then it should ignore the non-numerical values and return the sum of the numerical elements | ||
| test("given an array containing non-numerical values ignore non-numerical and returns correct total sum", () => { | ||
| expect(sum([2, "hello", 26.5, "hey", -6.6, 8])).toBe(29.9); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 only yield a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.
So the following could happen
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 ); // This fail
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 ); // This pass
expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 ); // This fail
console.log(1.2 + 0.6 + 0.005 == 1.805); // false
console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // falseCan you find a more appropriate way to test a value (that involves decimal number calculations) for equality?
Suggestion: Look up
- Checking equality in floating point arithmetic in JavaScript
- Checking equality in floating point arithmetic with Jest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using toBeCloseTo() in the jest test will help see how close numbers a, b are in to consider to them being equal in values because it accounts for floating-point imprecision, instead of requiring exact bit-for-bit equality
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using toBeCloseTo() in the jest test will help see how close numbers a, b are in to consider to them being equal in values because it accounts for floating-point imprecision, instead of requiring exact bit-for-bit equality as order of operations can affect floating point sums.
…nd enhancing code readability
…oko/Module-Data-Groups into Data-Groups-sprint-1
…t using the original array
|
All good! Well done. |
Learners, PR Template
Self checklist
Changelist