Skip to content
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

Fixes #3100 Bug on simple rounding #3136

Merged
merged 11 commits into from
Feb 22, 2024

Conversation

BrianFugate
Copy link
Contributor

@BrianFugate BrianFugate commented Jan 24, 2024

Fixed rounding bug described by @adshrc in issue #3100. I started with Jos' advice in the comments of the issue and also found help from the mathjs documentation here and from a similar issue here.

I addition to fixing the bug, I added two lines to the round function unit test. One for @adshrc's exact bug and a second for essentially the same bug that would also occur if the number of digits was specified with a second arg to the function call.

Thanks for the opportunity to contribute!

FYI, I didn't move David's name in the AUTHORS file. It looks like it made the opposite move in his last pull request that you just approved...

@josdejong
Copy link
Owner

Thanks Brian for your PR! Indeed you can leave AUTHORS untouched, I normally update that when doing a release.

A bit more context: in mathjs, there are two "levels" of functions:

  • low level "plain" JS functions
  • high level functions with runtime type-checking, dependency injection and for example config

Some feedbacks:

  1. In this case, I think we should implement a solution in the high level function round, not in the low level function roundNumber, since the latter doesn't have access to config.epsilon. Can you move the implementation and tests to round.js? You can look up equalScalar.js to see how to use config.epsilon.
  2. Instead of parseFloat(value).toFixed(14) I think we can use roundNumber itself.
  3. I think we should use a value based on config.epsilon instead of 14, does that make sense?
  4. I think we also need the BigNumber implementation in function round to use a nearly equal comparision.

@BrianFugate
Copy link
Contributor Author

Thank you @josdejong for the detailed feedback!

  1. In this case, I think we should implement a solution in the high level function round, not in the low level function roundNumber, since the latter doesn't have access to config.epsilon. Can you move the implementation and tests to round.js? You can look up equalScalar.js to see how to use config.epsilon.

My first solution for this bug was actually in the high level function round. However, after I realized that the bug also affected round((0.145*100), 0), and tried unsuccessfully to correct that version of the bug, I changed gears and moved to the low level function. I will have to spend some time figuring out how your runtime type-checking and dependency injection work because my working bug fix for number: just threw errors when I put the same logic in for 'number, number': when I ran the test in Mocha.

  1. Instead of parseFloat(value).toFixed(14) I think we can use roundNumber itself.
  2. I think we should use a value based on config.epsilon instead of 14, does that make sense?
  3. I think we also need the BigNumber implementation in function round to use a nearly equal comparision.

I will get to work on these revisions today. Thanks again!

@BrianFugate
Copy link
Contributor Author

I believe I implemented the changes you requested to this bug fix, @josdejong. Let me know if you want me to adjust anything else.

Thanks!

Copy link
Owner

@josdejong josdejong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, this looks good! I made a few inline remarks, can you have a look at those?

src/function/arithmetic/round.js Outdated Show resolved Hide resolved
src/function/arithmetic/round.js Outdated Show resolved Hide resolved
src/function/arithmetic/round.js Outdated Show resolved Hide resolved
@BrianFugate
Copy link
Contributor Author

I fixed the missing argument in the two regular number functions. I also converted the if...else statements to use the ternary operators as you suggested, and I feel it does make the code look much cleaner.

You also asked for unit tests to verify an edge case where a number rounded to the precision of epsilon is not nearly equal to the original number. I spent more time trying to come up with an input that could make that happen than I did making the revisions to the code, but I could not come up with anything. Admittedly, I am not a mathematician (I even had to google the spelling of mathematician, lol) I am just an aspiring software developer pretending to be good at math to work on your software. 😉 But here is the logic as I understand it:

Rounding a number to the precision of an epsilon value can not change the number by more than half of the epsilon, which using our default epsilon of 1e-12 would be 5e-13. I ran a bunch of tests while watching the values calculated in nearlyEqual and the relative error that our difference is compared to was usually in the range of xe-11 as shown here using the original value from the bug report:

$ node roundtesting.js

config.epsilon 1e-12
epsilonExponent 12
x 14.499999999999998
xEpsilon 14.5

nearlyEqual relative error 1.45e-11
nearlyEqual return true

round(0.145 * 100) returns 15

If you know of an edge case, or can steer me in the right direction to figure one out, I'd be happy to add some lines to the test file for this. Otherwise, I'm content knowing that we should never actually get a false value returned from the nearlyEqual function call.

Let me know what you think, I welcome any advice you have good or bad.

Thanks!

@josdejong
Copy link
Owner

I am just an aspiring software developer pretending to be good at math

😂 I appreciate your work, thanks!

You also asked for unit tests to verify an edge case where a number rounded to the precision of epsilon is not nearly equal to the original number.

Thinking about it, it makes sense that we do not reach this second case of the original if/else: first we round x to epsilon digits, and next we check if the difference between the original and the rounded value is less than epsilon, which indeed is always the case. That is quite funny. So I guess we can simplify the code to:

function (x, n) {
  const xEpsilon = roundNumber(x, epsilonExponent)
  return roundNumber(xEpsilon, n)
}

I think that makes sense: the first step gets rid of any round-off errors, and the second step does the rounding to the actually desired precision.

I had one more thought: in case of BigNumber (defaulting to 64 digits), the user will not be able to round to say 20 digits with the default configuration of epsilon: 1e-12, since the 20 will be overwritten with 12. What do you think, should we add a case that calculates the applied epsilon as the max of the configured config.epsilon and the provided n?

return value was mathematically impossible by
user input.

Adding dynamic epsilon logic to cover cases when
a user requests to round a number to a higher
precision than epsilon in the config file.

Also adding tests to cover dynamic epsilon cases.
@BrianFugate
Copy link
Contributor Author

BrianFugate commented Feb 2, 2024

I removed the nearlyEqual comparisons and added a dynamic epsilon to the big number logic to cover cases where users asked for numbers to be rounded to a higher precision than epsilon. I then realized that the same thing could occur if a user tries to round a regular number to more than 12 decimals, so I added the dynamic epsilon there as well.

In addition, I added test cases to cover the dynamic epsilon logic and threw in some comments so others will know what the logic is for.

Let me know what you think.

Thanks!

@josdejong
Copy link
Owner

Thanks for the updates. You're right, we have to respect the n argument too. Thanks for adding all tests. I made a few new inline comments, can you have a look at those?

@BrianFugate
Copy link
Contributor Author

I cannot find any new inline comments. I can see the old resolved ones if I look at the older commits, but I see no comments on the newest commit.

@josdejong
Copy link
Owner

You should see them when scrolling up in this very tab "Conversation"

@BrianFugate
Copy link
Contributor Author

Unfortunately, I do not see them in the 'Conversation' tab either. Did you hit 'Submit' on your review in the 'Code' tab? I believe that will prevent others from seeing your comments.

@josdejong
Copy link
Owner

That's odd. This is what the current page looks for me, inside the red rectangle are the comments that I mean:

afbeelding

@BrianFugate
Copy link
Contributor Author

I found this thread at Stack Overflow ealier when I was looking for the missing comments:

https://stackoverflow.com/questions/10248666/how-to-view-line-comments-in-github

Apparently, if the comments show 'pending' on your screen nobody else can see them. The first answer talks about how to resolve them being pending. However, I can see them in your screenshot so I'll get to work on them.

As far as the (n < 15) and (n < 64) comparison, the low level roundNumber function will only round to 15 decimal places so if I add one to n and it is already 15 it will cause an error. I was under the impression that big numbers were limited to 64 decimals and was making a similar verification. What is the max number of decimals for a big number then?

Regarding the new Bignumber and toNumber() logic, that was the only way to make the unit test pass. If I did it any other way it would make other scenarios not work (I believe it was the complex number one). The logic in my head was that the original function was supposed to have a big number type as both the x and the n so I decided to give big number types to the returned function and use it as written (except for the xEplsilon part). This was the original returned funtion:

return x.toDecimalPlaces(n.toNumber())

I can try to find another solution though, if you'd like.

Copy link
Owner

@josdejong josdejong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I clicked "submit review" now, can you see the comments now?

src/function/arithmetic/round.js Outdated Show resolved Hide resolved
src/function/arithmetic/round.js Outdated Show resolved Hide resolved
src/function/arithmetic/round.js Outdated Show resolved Hide resolved
@BrianFugate
Copy link
Contributor Author

Yes, I can see them now

@BrianFugate
Copy link
Contributor Author

Well, I have no new commits for you today. In fact, I am leaning towards recommending for you to just reject this pull request and the associated issue all together. I thought rounding to epsilon precision before rounding a number to a whole value was safe mathematically and would handle round off error. I later on thought that if someone wanted to round to a higher precision than epsilon, we could just round to one higher decimal place first, then round to what they requested. However, I have now found an edge case than breaks both of those theories.

If a user wants to round the actual number of 1.4999999999995 (13 decimal places) without specifying a number of decimal places and we round it to epsilon precision first it will round to 2 instead of 1 like it should. This example could also be changed to break any epsilon with a negative 'e' value.

Similarly, if the user wants round(0.0149999999995, 2) they will get 0.02 instead of 0.01. And you can find similar examples that break our logic by moving the decimal place depending on what the user requested.

We cannot use nearly equal to compare the final rounding number or the solution wouldn't fix the example in the example posted in the issue. So, what is worse round off errors or these errors that we would induce with the fix to round off errors?

@josdejong
Copy link
Owner

Yes, I can see them now

😅 that's good to hear. Sorry, my bad. I didn't see that I was inside a review mode and that the comments where pending.

However, I have now found an edge case than breaks both of those theories.

Hm. Good to think this through. Isn't the thing that you can't fully represent 1.4999999999995 (13 decimal places) when you have selected epsilon to be 12 digits (the default)? That would make sense, right? Similarly, you can't represent a BigNumber with 66 digits when you've configured them to have max 64 digits?

@josdejong
Copy link
Owner

@BrianFugate I have given this some more thought. As far as I can see though there is not a problem, as long as you recon with the configured epsilon. If you want to support 1.4999999999995 (13 decimal places), you'll have to configure epsilon to be 1e-14 for example, then it will work, right?

@dvd101x
Copy link
Collaborator

dvd101x commented Feb 15, 2024

Hi, just a comment

Configuring epsilon to be 1e-14 to get 13 decimal places isn't assuming that epsilon is the absolute tolerance?

Previously I mistakenly thought that epsilon did not work. My mistake was because I thought it was absolute when currently it's relative.

@josdejong
Copy link
Owner

@dvd101x ah, you're right. Originally Brian had this construct nearlyEqual(x, roundNumber(x, epsilonExponent), config.epsilon) but I couldn't come up with a case where this would be false. Probably it should because nearlyEqual is using a relative difference. Can you come up with a case where this will return false?

@BrianFugate
Copy link
Contributor Author

With this latest commit I removed the dynamic epsilon logic and made it just skip the roundoff error correction if the user requests the same or more decimals as config.epsilon has.

I also added a test to the unit test file to verify that our outputs change if epsilon is changed during runtime. Look closely at the values of the assertations so you are sure this is how you want it to operate.

Let me know what you think.

Thanks!

@josdejong
Copy link
Owner

Thanks for the updates Brian, I think this is exactly what we're looking for. I still want to think it over in the weekend to be sure we're not overlooking anything obvious here 🤔. Do you still have concerns on whether this actually makes sense?

@dvd101x
Copy link
Collaborator

dvd101x commented Feb 17, 2024

@josdejong by trying a shotgun approach of testing:

  • epsExp from 1 to 15
  • x from 1, 0.1, 0.001 ... to 1e-16
  • default value of config epsilon

I found the following cases where the expression nearlyEqual(x, roundNumber(x, epsilonExponent), config.epsilon) yields false

x epsExp
0.01 1
0.001 1
0.001 2
0.0001 1
0.0001 2
0.0001 3
0.00001 1
0.00001 2
0.00001 3
0.00001 4
0.000001 1
0.000001 2
0.000001 3
0.000001 4
0.000001 5
1e-7 1
1e-7 2
1e-7 3
1e-7 4
1e-7 5
1e-7 6
1e-8 1
1e-8 2
1e-8 3
1e-8 4
1e-8 5
1e-8 6
1e-8 7
1e-9 1
1e-9 2
1e-9 3
1e-9 4
1e-9 5
1e-9 6
1e-9 7
1e-9 8
1e-10 1
1e-10 2
1e-10 3
1e-10 4
1e-10 5
1e-10 6
1e-10 7
1e-10 8
1e-10 9
1e-11 1
1e-11 2
1e-11 3
1e-11 4
1e-11 5
1e-11 6
1e-11 7
1e-11 8
1e-11 9
1e-11 10
1e-12 1
1e-12 2
1e-12 3
1e-12 4
1e-12 5
1e-12 6
1e-12 7
1e-12 8
1e-12 9
1e-12 10
1e-12 11
1e-13 1
1e-13 2
1e-13 3
1e-13 4
1e-13 5
1e-13 6
1e-13 7
1e-13 8
1e-13 9
1e-13 10
1e-13 11
1e-13 12
1e-14 1
1e-14 2
1e-14 3
1e-14 4
1e-14 5
1e-14 6
1e-14 7
1e-14 8
1e-14 9
1e-14 10
1e-14 11
1e-14 12
1e-14 13
1e-15 1
1e-15 2
1e-15 3
1e-15 4
1e-15 5
1e-15 6
1e-15 7
1e-15 8
1e-15 9
1e-15 10
1e-15 11
1e-15 12
1e-15 13
1e-15 14

image

@josdejong
Copy link
Owner

Thanks @dvd101x, this helps a lot.

So, we need to test (a couple of) these cases to see whether the current implementation of this PR gives the right results. If not, I think we have to re-introduce the initial approach of @BrianFugate with nearlyEqual(x, roundNumber(x, epsilonExponent), config.epsilon) instead of just a two-stage roundNumber.

round function.

Adding test case for changing epsilon at runtime.

Both tests for changing epsilon at runtime also
verify the false nearlyEqual scenario.
@BrianFugate
Copy link
Contributor Author

I reintroduced the nearlyEqual comparison and immediately realized that my test case for changing epsilon at runtime was also a false case in the nearlyEqual comparison. In addition, I added a test using bignumbers to the same changing epsilon test and it also is a false in with nearly equal.

I feel confident in this now finally, thank you @dvd101x for helping get me back on track!

Thanks!

@josdejong
Copy link
Owner

josdejong commented Feb 21, 2024

@BrianFugate @dvd101x can you come up with a unit test for each of the four function signatures that fails when we remove the nearlyEqual part from the code? It makes sense to add the nearlyEqual step, but if it makes sense, there should be a case to prove it 🤔. Right now, when I remove the nearlyEqual part, only one BigNumber test fails.

EDIT: sorry, I see that both number and BigNumber implementations of round(x, n) have a failing test without the nearlyEqual part. So only for the round(x) implementations there is no test that fails without the nearlyEqual section.

@BrianFugate
Copy link
Contributor Author

I do not think there is a case where we can make round(x) fail the nearly equal test and here are my reasons:

  1. The smallest number that can be flipped to round up to 1 is 0.45 if rounded to 1 decimal first (i.e. 1e-1 epsilon)
  2. According to @dvd101x 's shotgun test, with a 1e-1 epsilon, 0.1 passes the nearlyEqual test and 0.01 fails it. Regardless of where the actual breakover point is between those two numbers we will always round down to 0 regardless of the nearlyEquals result.
  3. Using 1e0 for our epsilon is the same as removing our two stage rounding process, because we will just round to 0 decimal places twice.

Unless there are plans to make the low level function roundNumber accept negative decimals (maybe for rounding to the tens or hundreds) or decimal.js doing the same for .toDecimals, or you are planning on changing how nearlyEqual uses epsilon in @dvd101x 's pull request #3152, then I do not think the nearlyEqual verification is necessary for round(x). Do you want me to remove it or leave it in place just in case something else changes in the future?

@josdejong
Copy link
Owner

I think indeed that we could remove the nearlyEqual check in the round(x) signatures. We can go both ways. Your reasoning about possible future changes is a good one though. Keeping the logic the same ensures that there cannot be subtle edge-cases where the behavior differs. Also, it is easier to understand the code of round when it has 4x the exact same logic rather than two different implementations.

OK then, let's leave it as it is right now, with 4x the same implementation, OK?

@BrianFugate
Copy link
Contributor Author

Sounds good to me!

@josdejong
Copy link
Owner

OK merging your PR now.

Brian, thanks a lot for your patience and for thinking along. This was trickier than I had expected.

@josdejong josdejong merged commit 85b65da into josdejong:develop Feb 22, 2024
9 checks passed
@josdejong
Copy link
Owner

Published now in v12.4.0, thanks again!

gauravchawhan added a commit to gauravchawhan/mathjs that referenced this pull request Oct 14, 2024
* fix: Find eigenvectors of defective matrices (josdejong#3037)

* fix: Find eigenvectors of defective matrices

  Previously, attempting to take the `eigs` of any defective matrix
  was doomed to fail in an attempt to solve a singular linear system.
  This PR detects the situation (as best as it can given the
  inherent numerical instability of the current methods used) and
  handles it. Note that in such cases, it's not possible to return
  a square matrix whose columns are the eigenvectors corresponding to
  the returned eigenvalues. In light of that fact and issue josdejong#3014, this
  PR also changes the return value of `eigs` so that the eigenvectors
  are passed back in a property `eigenvectors` which is an array of
  plain objects `{value: e, vector: v}`.

  Note that this PR makes the ancillary changes of correcting the
  spelling of the filename which was "realSymetric.js," and replacing
  the now-unnecessary auxiliary function "createArray" therein with
  `Array(size).fill(element)`. The rationale for performing these
  changes not strictly related to the issues at hand is that this
  file is rarely touched and with the level of maintenance hours we have
  at hand, it's more efficient to do these small refactorings in parallel
  with the actual bugfixes, which are orthogonal and so will not be
  obfuscated by this refactor. Note `git diff` does properly track the
  file name change.

  However, it also makes a potentially more pervasive change: in order for
  the numerically-sensitive algorithm to work, it changes the condition
  on when two very close (double) numbers are "nearlyEqual" from differing by
  less than DBL_EPSILON to differing by less than or equal to DBL_EPSILON.
  Although this may change other behaviors than the ones primarily being
  addressed, I believe it is an acceptable change because

  (a) It preserves all tests.
  (b) DBL_EPSILON is well below the standard config.epsilon anyway
  (c) I believe there are extant issues noting the odd/inconsistent
      behavior of nearlyEqual near 0 anyway, so I believe this will
      be overhauled in the future in any case. If so, the eigenvector
      computation will make a good test that a future nearlyEqual
      algorithm is working well.

  To be clear, the direct motivation for the change is that there are
  multiple cases in the eigenvector computation in which a coefficient
  that is "supposed" to be zero comes out to precisely DBL_EPSILON, which
  is fairly unsurprising given that these coefficients are produced by
  subtracting an eigenvalue from a diagonal entry of a matrix, which is
  likely to be essentially equal to that eigenvalue.

  As many tests of defective matrices as I could readily find by web
  searching have been added as unit tests (and one more in the typescript
  type testing). An additional case I found still fails, but in the
  _eigenvalue_ computation rather than the _eigenvector_ search, so that
  was deemed beyond the scope of this PR and has been filed as issue josdejong#3036.

  Resolves josdejong#2879.
  Resolves josdejong#2927.
  Resolves josdejong#3014.

* refactor: remove comma that lint now doesn't like

* test: add a test for eigs with a precision argument

* feat: Use simple shifts in QR eigenvalue iterations that improve convergence

  Although we might want to use better shifts in the future, we might just
  use a library instead. But for now I think this:
  Resolves josdejong#2178.

  Also responds to the review feedback provided in PR josdejong#3037.

* docs: update history

* fix: josdejong#3074 improve error message when using function `max` in `derivative`

* fix: josdejong#3073 parsing quotes inside a string

* fix: josdejong#2027 cannot use named operators like `to` or `mod` as property name

* chore: update devDependencies

* chore: run `npm audit fix`

* chore: publish v11.11.2

* Drop official support for Node.js 14 and 16

* fix: change toTex variable and function assignment from `:=` to `=` (see josdejong#2980, josdejong#3032)

* Update history

* docs: fix typo in `p.set` example in the documentation of matrices (josdejong#3080)

* feat: Add option to eigs() to turn off eigenvector computation (josdejong#3057)

* feat: Add option to eigs() to turn off eigenvector computation

  For large matrices, the eigenvector computation can be noticeably expensive
  and so it's worthwhile to have a way to turn it off if the eigenvectors
  will not be used.
  Resolves josdejong#2180.

* fix: Add test for precision in options arg of eigs

  And also a fix for a small bug that the new test uncovered.

* test: check eigs with matrix and options

* refactor: remove dead code from complexEigs.js

* fix: add new signatures of eigs to typescript

* test: ensure eigenvectors property not present with eigenvectors: false option

* fix: correct balancing code in complexEigs

* Fix: josdejong#3073 escaping in strings (josdejong#3082)

* chore: refactor parsing strings to not rely on `JSON.parse`

* fix: josdejong#3073 function `format` not escaping control characters and double quotes in strings

* chore: add more unit tests

* feat: implement subtractScalar (josdejong#3081, josdejong#2643)

* added subtractScaler

* added subtractScaler missing entries

* added test cases for 2 or more parameters, test for subtractScalar instead fo subtract

* replaced subtract with subtractScalar whereever possible

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* fix: function `clone` not throwing an error in case of an unsupported type like a function

* chore: make the unit test more robust

* fix: josdejong#2960 add type definition of function `symbolicEqual` (josdejong#3035)

* chore: update devDependencies

* chore: publish v11.12.0

* fix: josdejong#2919 TypeScript types not working with NodeNext module resolution (josdejong#3079)

* docs: update deprecation messages

* chore: publish v12.0.0

* chore: update history (forgot to mention a feature in v12)

* fix: josdejong#3088 error in the description of the return type of `pickRandom`

* fix josdejong#3087: extend function `mod` with support for negative divisors in when using `BigNumber` or `Fraction`

* fix josdejong#3092: a typo in an error message when converting a string into a number

* fix josdejong#3094: function `derivative` mutates the input expression when it fails

* feat: extend function `round` with support for units (josdejong#3095)

* fix josdejong#2761: implement support for units in function `round` (WIP)

* fix josdejong#2761: extend function `round` with support for units

* docs: describe all signatures in the docs of function round

* chore: fix linting issue

* chore: remove less-useful signatures for round with units and matrices

* chore: update devDependencies

* chore: publish v12.1.0

* docs: update the release date in history.md

* fix: josdejong#3096 embedded docs of `eigs` throwing an error

* chore: update history

* fix: accidentally passing a scope as third _and_ fourth argument to raw functions

* feat: lazy evaluation of and, or, &, |  (josdejong#3101, josdejong#3090)

* If fn has rawArgs set, pass unevaluated args

* Add shared helper function for evaluating truthiness

* Add and & or transform functions for lazy evaluation

* Add lazy evaluation of bitwise & and | operators

* Add unit tests for lazy evaluation

* Add lazy evaluation note to docs

* Move documentation to Syntax page

* Replace `testCondition()` with test evaluation
of logical function itself

* Use `isCollection()` to simplify bitwise transform functions

* fix: do not copy scope in raw OperatorNode, test lazy operators scope

* fix: linting issues

---------

Co-authored-by: Brooks Smith <brooks.smith@clearcalcs.com>

* docs: update history

* chore: update devDependencies

* chore: publish `v12.2.0`

* fix: josdejong#3109 method `Node.toHTML` not accepting a custom `handler`

* chore: upgrade node and Github actions versions for CI

* chore: upgrade devDependencies

* chore: publish v12.2.1

* chore: oopsie, update version number in version.js

* chore: up version number, and pin fraction.js at v4.3.4

* chore: publish v12.2.1

* docs: update maintenance badge to 2024

* docs: fix the github sponsors badge

* Support new metric prefixes: Q, R, r, and q (josdejong#3113)

* added Q, R, r, q metrix prefixes

* tests added for new prefixes

* removed duplicate tests

* maybe square and cubic tests will bump code cov into the positive

* Check numeric value

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* docs: change 2023 to 2024

* Unitless quantity conversion bug (josdejong#3117)

* Add test for conversion to unitless quantity

* Avoid access to missing array index

* Also check that other.units is not empty

* Add test for abs of dimensionless unit

* Fix: avoid access to missing units array index

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* fix `toSI()` wrongly converting `degC` (josdejong#3118)

* Add new test for degC toSI

* Convert value using to() if needed

* Only set ret.value = null when it is not already null

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* chore: update devDependencies

* chore: publish v12.3.0

* chore: run `npm audit fix`

* Infer types of arguments more precisely (josdejong#3123)

* Prefer inferring types of nodes as tuples

* Implement for IndexNode

* Test for types

* Use tuple type for array node

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* CodeEditorExample (josdejong#3027)

* broadcasting

* Simplified broadcasting

* Updated for broadcasting

* Changed to camel case

* Camel case and auto formating

* Added comments

* Skip if matrices have the same size

* Fixed issue with undefined variable

missing dot  in `A._size`

* Implemented broadcasting in all functions

* Added helper functions

* Added function to check for broadcasting rules

* Tests for broadcasted arithmetic

* Fixed issue with matrix the size of a vector

* Documented and updated broadcasting

* Included broadcast.test

* Included math to syntax when missing

* Add code editor example

* Vite mini project

* Initial example

* added alpine debounce

* Fixed display

* Added parser.clear

* Added mathjs-language

* Made module to get expressions

* Added custom events

* Issue with help formatting

* Simplified help format

* Restored package.json

* removed unneded icons

* Added readme file

* Fixed versions

* Commented getExpressions

* Documented main.js

* Fixed title

* Fixed alpine version

* Removed AlpineJS

* Added documentation and renamed variables for clarity

* Fixed naming errors

---------

Co-authored-by: David Contreras <david.contreras@guentner.com>
Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: minor refinement in the `code editor` example

* chore: update HISTORY.md

* fix: josdejong#3114 build warnings related to a number of wrong `/* #__PURE__ */` annotations

* chore: do not output documentation warnings unless running with a `--debug-docs` flag

* docs: update authors

* Fixed issue with long lines in Code Editor Example  (josdejong#3130)

* broadcasting

* Simplified broadcasting

* Updated for broadcasting

* Changed to camel case

* Camel case and auto formating

* Added comments

* Skip if matrices have the same size

* Fixed issue with undefined variable

missing dot  in `A._size`

* Implemented broadcasting in all functions

* Added helper functions

* Added function to check for broadcasting rules

* Tests for broadcasted arithmetic

* Fixed issue with matrix the size of a vector

* Documented and updated broadcasting

* Included broadcast.test

* Included math to syntax when missing

* Add code editor example

* Vite mini project

* Initial example

* added alpine debounce

* Fixed display

* Added parser.clear

* Added mathjs-language

* Made module to get expressions

* Added custom events

* Issue with help formatting

* Simplified help format

* Restored package.json

* removed unneded icons

* Added readme file

* Fixed versions

* Commented getExpressions

* Documented main.js

* Fixed title

* Fixed alpine version

* Removed AlpineJS

* Added documentation and renamed variables for clarity

* Fixed naming errors

* Fixed issue with long lines

* Edge case where multiple expressions are on the same line not ending in ";"

---------

Co-authored-by: David Contreras <david.contreras@guentner.com>
Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* docs: josdejong#3145 fix documentation about REPL, it does require a build step nowadays

* fix: josdejong#3142 support BigNumber values for the options of function `format`: `precision`, `wordSize`, `lowerExp`, `upperExp`

* Improve type definitions of function `hypot` (josdejong#3144)

* Addressed silentmissile's comment in josdejong#3125

* Added method overload to index.d.ts, have to revert commit due to changes to package-lock.json with using npm install to run the unit tests & lint tests

* chore: update HISTORY.md

* fix: josdejong#3141 `help(config)` altering the actual `config` when evaluating the examples

* chore: update devDependencies

* chore: publish `v12.3.1`

* chore: add a benchmark to get a feel for how fast scope variables are resolved

* chore: fix linting issue

* Fix not being able to use `and` and `or` inside a function definition (josdejong#3150)

* chore: write unit tests using `and` and `or` inside a function definition (WIP)

* fix: josdejong#3143 fix scope issues in rawArgs functions by implementing a `PartitionedMap`

* fix: add more unit tests for `ObjectWrappingMap`

* fix: don't let `ObjectWrappingMap` and `PartitionedMap` extend `Map` (risk of having non-overwritten methods)

* docs: update docs about `rawArgs` functions

* chore: update devDependencies

* chore: publish v12.3.2

* chore: publish v12.3.2

* fix: josdejong#3155 remove an outdated section about complex numbers from the docs

* docs: describe `getAllAsMap` in the Parser docs (josdejong#3158)

* chore: update history

* Determinant with small numbers fix (josdejong#3139)

* feat: trailing commas in matrices (josdejong#3154)

* chore: update history

* docs: fix broken example in the documentation about matrices (see josdejong#3159)

* fix: `PartitionedMap` and `ObjectWrappingMap` missing a property
  `Symbol.iterator`

* fix: linting issue

* fix: mode signature return types  (josdejong#3153)

* fix: mode type signatures

* Add ts tests for mode

* Add assertions mode type tests

* Update author Rich in AUTHORS file

---------

Co-authored-by: Rich Martinez <richmartinez@Edinas-MacBook-Pro.local>
Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* feat: improve the performance f `multiply` by adding matrix type inferencing (josdejong#3149)

* added type inference

* added back accidentally removed return statement and made it so that the explicitly defined type is returned at the end

* made sure that mixed types are ignored in the process data types check

* fixed issue with undefined _data for SparseMatrix and linting issues

* simplified syntax and added type inferencing to src/type/matrix/utils and src/function/matrix/dot.js

* shortened the final part of the type inferencing and moved it to matrix creation in multiply

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* Fix: josdejong#3100 function `round` not handling round-off errors (josdejong#3136)

* Fixing rounding bug from issue 3100

* Corrected syntax and converted if...else to logic using ternary operator

* Removing nearlyEqual comparison because a false
return value was mathematically impossible by
user input.

Adding dynamic epsilon logic to cover cases when
a user requests to round a number to a higher
precision than epsilon in the config file.

Also adding tests to cover dynamic epsilon cases.

* Removing dynamic epsilon and adding test for changing config.epsilon during runtime

* Reintroducing nearly equal verification for
round function.

Adding test case for changing epsilon at runtime.

Both tests for changing epsilon at runtime also
verify the false nearlyEqual scenario.

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* chore: update devDependencies (most notably eslint)

* chore: publish v12.4.0

* fix josdejong#3163: `toTex` wrongly returning `Infinity` for large BigNumbers

* fix josdejong#3162: add license information about CSParse (josdejong#3164)

* update history

* fix: faster startup time of the CLI and REPL by loading the bundled file

* feat: Interactive lorenz example (josdejong#3151)

* Interactive lorenz

* Separate Interactive Lorenz

* Cleanup

* Bigger graphs

* Full screen examples

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* fix: give the inputsDiv a white background (see josdejong#3151)

* chore: update history

* chore: remove `polyfill.io` inside example (josdejong#3167)

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* fix josdejong#3175: expose `math.Unit.ALIASES`, update history

* chore: update history

* doc: create CODE_OF_CONDUCT.md

See josdejong#3174

* fix: josdejong#3172 simplify `"true and true"`

* fix: josdejong#3175 cannot delete units using `math.Unit.deleteUnit`

* chore: update devDependencies

* chore: run `npm audit fix`

* chore: publish v12.4.1

* docs: fix misleading documentation for expression tree traverse (josdejong#3177)

Callback function for MathNode.traverse() returns void. Documentation says callback must return a replacement for the existing node (possibly copied from transform() above).

* chore: update history

* fix: josdejong#3180 fix type definitions of function `add` and `multiply` to allow
  more than two arguments

* chore: update devDependencies (most notably `gulp@5`)

* chore: replace utility function `values` with `Object.values` (fix josdejong#3194)

* fix josdejong#3192: function `isNaN` returns `false` for `NaN` units in a matrix or   array

* Use referToSelf() to recursively check if various types are NaN in an array or matrix

* fix array test description from isNegative to isNaN

* Add test for units in a matrix

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* chore: update devDependencies

* chore: publish `v12.4.2`

* chore: replace util functions `values` and `contains` with using native JS functions (see josdejong#3194)

* chore: replace util functions `values` and `contains` and usages of `indexOf` with using native JS functions `values` and `contains` (see josdejong#3194)

* fix: serialization of Units without a value, see josdejong#1240

* Fix: outdated, incorrect documentation about the order of precedence for
  operator modulus `%`. See josdejong#3189

* feat: nearly equal with relative and absolute tolerance (josdejong#3152)

* nearlyEqual with absolute and relative tolerances

* Format

* nearlyEqual for bigNumber

* Added skip for NaN

* Reduce diff a bit

* Issue with examples in jsdcos

* Updated all calls for nearlyEqual

* Fixed failing tests

* Changed epsilon to relTol, absTol

* Changed references to epsilon in docs and tests

* Added warning for config.epsilon

* Fix warning in zeta.test

* Added config test

* Added sinon to test console.warn

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: move `sinon` to devDependencies and fix two typos

* chore: adjust `isPositive`, `isNegative`, and `isZero` to the new `relTol` and `absTol`

* docs: document how to run tests for the type definitions

* Improve quantileSeq typings (josdejong#3198)

* Improve quantileSeq typings

* Add tests, revert comment changes

* Fix type tests

* chore: update HISTORY.md

* chore: cleanup entry files that are deprecated since `v8.0.0` (2020-11-06)

* fix: upgrade to `fraction.js@4.3.7`

* chore: convert CJS files to ESM (josdejong#3204)

* chore: added test cases to deepForEach (josdejong#3211)

* feat: implement support for `bigint` (josdejong#3207, josdejong#2737)

* chore: move browserslist from package.json into `.browserslistrc`

* chore: change browerslist to browsers that are not dead and fully support es6

* chore: improve browserslist to explicity require bigint support

* chore: publish v12.4.3

* chore: update package-lock.json

* chore: update devDependencies

* chore: publish v13.0.0

* docs: document dropping JS engines that do not support E6 or bigint in v13

* fix: example advanced/custom_argument_parsing.js

* chore: add unit tests for `deepMap`, `isCollection`, and `reduce`

* docs: fix example `convert_fraction_to_bignumber.js` by upgrading to `typed-function@4.2.1`

* Broadcast refactor (josdejong#3220)

* chore: update history

* fix: josdejong#3227 generated bundle containing `catch` blocks without parameters

* fix: josdejong#2348 update type definitions of the `Parser` methods (josdejong#3226)

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update devDependencies and run `npm audit fix`

* chore: publish v13.0.1

* Further improve quantileSeq typings (josdejong#3223)

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* chore: update devDependencies

* fix josdejong#3227: change the minimum required JS version to ES2020 in the docs

* chore: publish v13.0.2

* chore: update dependencies of the code editor example

* fix: josdejong#3232 fix type definitions of function `format` to support notations `hex`, `bin`, and `oct`

* fix: use exact values for US liquid volume units (josdejong#3229)

1 US gallon is defined as 231 cubic inches, which is exactly 3.785411784 L (since 1 inch is defined as 25.4 mm). Other units are defined against the gallon.

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* fix: types static methods and members for Unit class (josdejong#3230)

* fix: types static method for Unit class

Changes unit interface to declare class to enable the adding of static methods.

* refactor: change to not using declare class

* fix: adds more unit static methods and updates tests

* chore: moves test from wrong location

* fix: adds additional static methods and updates jsDocs

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* chore: update devDependencies

* chore: publish `v13.0.3`

* chore: update package-lock.json

* chore: revert updating devDependencies

* chore: revert reverting updating devDependencies

* chore: try use `ubuntu-24.04` instead of `ubuntu-latest`

* chore: try use `ubuntu-22.04` instead of `ubuntu-24.04`

* chore: try use `ubuntu-latest` instead of `ubuntu-22.04` again

* chore: disable testing on Node 22 for now until we get mocha working again in GitHub actions

* chore: publish `v13.0.3` for real

* chore: enable testing on Node 22 again

* feat: add matrix datatypes in more cases (josdejong#3235)

* chore: update history

* docs: add a link to the documentation page about the syntax expression from the function `evaluate` (fix josdejong#3238)

* feat: export util functions for maps and improve documentation of `scope`  (josdejong#3243)

* feat: export util functions `isMap`, `isPartitionedMap`, and `isObjectWrappingMap` and improve the documentation of `scope` (see josdejong#3150)

* chore: fix broken unit tests

* docs: refine the explanation about scopes

* chore: update history

* fix: josdejong#3244 fix broken link to `ResultSet` in the docs about classes

* fix: function `map` not always working with matrices (josdejong#3242)

* Removed maxArgumentCount in favor of applyCallback

* Making a pure _recurse function

* Added cbrt tests, removed unnecesary changes in functions.

* Fixed main bottleneck

* Restored back function before unintended change

* Fix format

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* chore: add tea.yaml file

* docs: spelling fixes in the embedded docs (josdejong#3252)

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* chore: add a benchmark testing `DenseMatrix.map(...)` and `DenseMatrix.forEach(...)` (see josdejong#3251)

* feat: support multiple inputs in function `map` (josdejong#3228)

* chore: update history

* chore: update devDependencies

* chore: publish `v13.1.0`

* fix: various security vulnerabilities (josdejong#3255)

* fix: disable parser functions in the CLI (security issue)

* fix: ensure `ObjectWrappingMap` doesn't allow deleting unsafe properties (security issue)

* fix: enable using methods and (safe) properties on plain arrays

* docs: update the "Less vulnerable expression parser" section in the docs

* chore: fix typos and linting issues

* chore: keep functions like `simplify` enabled in the CLI

* docs: update the security page

* fix: ensure `ObjectWrappingMap.keys` cannot list unsafe properties

* fix: when overwriting a rawArgs function with a non-rawArgs function it was still called with raw arguments

* docs: fix a typo

* chore: publish v13.1.1

* fix broken links in configuration.md (josdejong#3254)

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* fix: improve the type definitions of `ConstantNode` to support all data types (josdejong#3257)

* chore: update history

* chore: fix broken benchmark

* fix: josdejong#3259 function `symbolicEqual` missing in the TypeScript definitions

* chore: update AUTHORS file

* fix: josdejong#3259 revert the duplicate `symbolicEqual` definition and just export the existing definitions

* fix: josdejong#3246 add type definitions for function `leafCount`

* fix: josdejong#3253 cannot use identifiers containing special characters in function `derivative`

* chore: update history

* chore: extend the `map.js` benchmark

* chore: fix linting issue

* chore: improve performance of functions `map`, `filter` and `forEach` (josdejong#3256)

* Implement reduceCallback

* Add jsdocs

* implement simplifyCallback in other functions

* Moved recurse to array.js

* Format

* Separate transform callback

* forEach transform

* Renamed applyCallback to simplifyCallback

* Simplified index transform

* renamed to reducedCallback and simplifiedCallback to simpleCallback

* chore: fix linting issue

* Added forEach benchmark

* renamed simplifyCallback to optimizeCallback

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update history

* fix: josdejong#3267 implicit multiplication with a negative number and unit `in`

* feat: speed up the `map()` and `forEach()` functions in DenseMatrix.js (josdejong#3251)

* Optimize the map and forEach functions in DenseMatrix.js

* Changed index back to Array from Uint32Array and clone it using index.slice(0) instead of [...index]

* Fixed merge conflicts with the fast callback optimization

* Fixed the documentation for _forEach()

* Fixed _forEach comment and made it return an immutable index array

* Resolved DenseMatrix unit test suggestions

---------

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>

* chore: update HISTORY.md and AUTHORS

* chore: use `codecov/codecov-action`

* chore: try fix the codecov-action

* chore: try fix the codecov-action

* chore: try fix the codecov-action

* chore: try fix the codecov-action

* docs: document the syntax of `map` and `forEach` in the expression parser (josdejong#3272)

* chore: update docs

* chore: update devDependencies

* chore: publish `v13.2.0`

* chore: add a missing comma

* docs: fix a typo on the Syntax page (josdejong#3276)

* fix: update dependencies and devDependencies

* chore: cleanup unused imports

* chore: revert to `typescript@5.5.4` to keep the (old) eslint version happy

---------

Co-authored-by: Glen Whitney <glen@studioinfinity.org>
Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
Co-authored-by: Vincent Tam <VincentTam@users.noreply.github.com>
Co-authored-by: Vrushaket Chaudhari <82214275+vrushaket@users.noreply.github.com>
Co-authored-by: Juan Pablo Alvarado <63080419+juancodeaudio@users.noreply.github.com>
Co-authored-by: Brooks Smith <brooks.smith@clearcalcs.com>
Co-authored-by: Alex Edgcomb <aedgcomb@gmail.com>
Co-authored-by: Carl Osterwisch <costerwi@gmail.com>
Co-authored-by: S.Y. Lee <sylee957@gmail.com>
Co-authored-by: David Contreras <dvd.cnt@gmail.com>
Co-authored-by: David Contreras <david.contreras@guentner.com>
Co-authored-by: Hudsxn <143907857+Hudsxn@users.noreply.github.com>
Co-authored-by: Rich Martinez <6185506+rich-martinez@users.noreply.github.com>
Co-authored-by: Rich Martinez <richmartinez@Edinas-MacBook-Pro.local>
Co-authored-by: RandomGamingDev <83996185+RandomGamingDev@users.noreply.github.com>
Co-authored-by: Brian Fugate <fugateb@yahoo.com>
Co-authored-by: Sukka <isukkaw@gmail.com>
Co-authored-by: Rohil Shah <shah5963@gmail.com>
Co-authored-by: Laurent Gérin <41303636+lgerin@users.noreply.github.com>
Co-authored-by: Adam Jones <domdomegg+git@gmail.com>
Co-authored-by: Lucas Eng <lucaseng19@gmail.com>
Co-authored-by: Orel Ben Neriah <77707952+orelbn@users.noreply.github.com>
Co-authored-by: Vistinum <eugene225@gmail.com>
Co-authored-by: Vas Sudanagunta <vas@commonkarma.org>
Co-authored-by: Brooks Smith <42363318+smith120bh@users.noreply.github.com>
Co-authored-by: Jmar L. Pineda <63294460+Galm007@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants