-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
nf() produces problematic string-formatting of very large or small numbers #5710
Comments
Thanks @golanlevin! Adding the Math and Utilities Stewards @limzykenneth, @jeffawang, @AdilRabbani, and @kungfuchicken to take a look at this issue. |
@golanlevin, you said you'd "...expected to see something on the screen like 0.00...," and this would have been my expectation too. My opinion is that changing For small numbers, when we use Here's an example where we show that we've already had a loss of precision via rounding for exponents for (let i = 0; i < 10; i++) {
const n = pow(10, -i);
console.log(i, nf(n, 1, 2));
}
Very large numbers might warrant a separate discussion. My browser switches to scientific notation at I think the current behavior of just showing the scientific notation without the exponent should certainly change though. Maybe it's okay in this case to show the exponent part, since the number is already at least 21 characters long. @golanlevin, beyond fixing |
@Qianqianye I'd like to add the discussion label to this issue, since it seems to have some judgment calls in the decision-making. I don't have permission to do it though. |
Thanks @jeffawang. Just added the discussion label. We are working on optimizing the labeling process, and we will make sure the stewards can add labels in the future. |
Thanks @jeffawang. The behavior of the
...and here's what it prints out. Check out the unexpected behavior in line 8!
|
Ah yes, I think that is similar behavior in that the change happens when javascript changes to scientific notation. I believe mine had the |
I found this issue from long ago #1879 where the lack of support for scientific notation was noted. The I think clear expectation of what these functions should do that go along with clear examples can help us understand how to implement and fix them for the long term. |
Hi @limzykenneth, 1. Debugging!For me,
This produces the following output at the console: A simple change makes it much easier to 'read' the debugging results:
This produces: Not only do the numbers line up nicely, but they now also eliminate irrelevant digits that distract from showing trends in the data. (In this sense, repairing 2. Sensible user-facing numeric displays and documentsThe
This produces the following results in the console — it's clear which one would be preferable to show a user/reader:
3. Generating output files with a small memory footprintI do a lot of generative artwork with computer-controlled plotters, which take SVG vector graphics. When computationally generating SVG files with thousands or millions of vertices, it can be desirable for the resulting files to have a small memory footprint, by reducing the precision of numbers. It simply isn't necessary (or even technically possible) to physically execute vector designs that are accurate to a trillionth of an inch! Here's a short program fragment that generates an SVG path connecting 4 points:
This produces the following output (which would be saved to a text/SVG file):
SVG units are 1/96th of an inch, so it's clear that there's an enormous amount of unnecessary precision here, and it bloats the file. Instead, using |
Circling back to this after a couple of month, I'm glad to say I've found a use for
|
please assign this to me, i think i can take this forward |
Is their something remaining I can help with to close this issue ? |
There is only the question of |
Thank you all for working on this issue. I am inviting the current Math stewards to this discussion @limzykenneth, @ericnlchen, @ChihYungChang, @bsubbaraman, @albertomancia, @JazerUCSB, @tedkmburu, @perminder-17, @Obi-Engine10, @jeanetteandrews. Would love to hear what y'all think. Thanks! |
Most appropriate sub-area of p5.js?
p5.js version
1.4.1
Web browser and version
Google Chrome 103.0.5060.53 (Official Build) (arm64)
Operating System
MacOS 12.4
Steps to reproduce this
Steps:
nf()
, lose all exponent information. This could be misleading, confusing, or problematic.x
which I knew was between 0 and 1, usingnf(x,1,2);
. Unbeknownst to me, the number was something like 0.000000000001234, for which I would have expected to see something on the screen like 0.00 (because of rounding) — but instead I saw 1.23, which was very confusing. I was concerned that my variable had somehow gone outside the range of 0-1!Snippet:
Here's some code:
This produces the following output:
Recommendation:
Some possible fixes are:
nfe()
which prints exponent information. This function would be analogous tonfp()
.nfx()
output all "extended" information, including the exponent as well as the +/- sign.nfp()
so that it always outputs exponent information, as well as the +/- signnf()
, even if it blows out the character count (yuck)In the examples above, I feel the desired result should be the strings
1.42e-9
and1.95e+86
.The text was updated successfully, but these errors were encountered: