Comments (5)
@edgar-bonet I've been experimenting with variants of code…
#include <assert.h>
#include <math.h>
#include <stdio.h>
int main() {
double max = 3.00000000000000000000000001;
double min = 3.00000000000000000000000002;
assert(max != min);
double numerator = 123.0;
double denominator = min - max;
assert(isfinite(denominator) && (denominator != 0.0));
double res = numerator / denominator;
printf("%f\n", res);
return 0;
}
…and it seems that once min
and max
have different values at runtime this no longer gets to inf
in practice. So my experiments seem to support your statement and that would mean that we only need part "one" out of my original threefold proposal. Cool!
from ttyplot.
I'm thinking the fix here could be threefold:
- one, the place of data entry —
scanf
— would be made to e.g. throw away any non-finite data points and store 0.0 instead - two, the place handing doubles over to
draw_line
in functionplot_values
could check againstINT_MIN
andINT_MAX
and ceil/floor against these when passing, respecting theint
contract - three, function
draw_line
could check valuesl1
andl2
for safe upcoming use in formulas…- a)
ph+1-l1
andph+1-l2
and - b)
l2-l1
.
- a)
The key idea here is to both protect against bad data entering the application and protect the draw layer from unpaintable results existing ttyplot's "math engine" in addition, to be waterproof.
@edgar-bonet @tenox7 what do you think?
from ttyplot.
@hartwork: This approach looks very defensive to me. I am fine defensive programming, but all this may not be strictly needed:
-
NANs are the real problem caught by the sanitizer, so yes, something has to be done about them. Either replace with zeroes, or skip these datapoints (
if (isfinite(v1[i]) && isfinite(v2[i])) draw_line(...);
. The result should look the same. -
If finite, the numbers being cast to
int
are between0.0
andph
(the plot height), so they certainly lie betweenINT_MIN
andINT_MAX
. -
The way
l2
andl2
are computed, they also fit within the terminal height.
from ttyplot.
- If finite, the numbers being cast to
int
are between0.0
andph
(the plot height), so they certainly lie betweenINT_MIN
andINT_MAX
.
@edgar-bonet is that guaranteed? E.g. if min
and max
ever are equal then max-=min;
will make max
0 and then we divide by zero which should give infinity, multiplied by ph
is infinity again. I think we protect against them being equal — not sure if we cover all places — but min
and max
being very close — say 3.000000000001 and 3.000000000002 — can still shoot over the int
range when dividing by max
, no? That division is what worries me mostly.
from ttyplot.
@hartwork wrote:
if
min
andmax
ever are equal [...] then we divide by zero
Indeed, we should check whether this can ever happen, or whether this is already well protected against. It is not very clear to me, as there are some manipulations tweaking max
that depend on softmax
and harmin
...
but
min
andmax
being very close — say 3.000000000001 and 3.000000000002 — can still shoot over theint
range when dividing bymax
, no?
Nope. If min
and max
are close enough, IEEE-754 guarantees that computing their difference will give an exact result. This, by itself, is remarkable: not many floating point computations guarantee exact results. Then, if values1[i]
lies between min
and max
, the quantity (values1[i]-min)/(max-min)
will be between zero and one.
from ttyplot.
Related Issues (20)
- arguments broken HOT 22
- Please add a Fish shell completion file HOT 1
- [1.5.2] `ttyplot -c X -2` fails to use ASCII character "X" for drawing? HOT 2
- (Plans and remaing todos for) Release 1.6.0 HOT 6
- ttyplot doesn't build (in general and on Void Linux) because of issues in the Makefile HOT 15
- Use of `M_PI` in `stresstest.c` is not C99 and unfortunately breaks compilation with musl libc HOT 5
- [1.6.1] Chart output corrupts after a few minutes on macOS? HOT 20
- Shortcut for live plot of memory usage? HOT 7
- [master] Fails to compile with error: cannot find -lcurses: No such file or directory
- [master] "make deb" is broken
- use hline() vline() instead of drawing by hand HOT 1
- better SIGWINCH handler HOT 2
- stdin closes on SIGWINCH on some systems HOT 3
- [1.5.1] Minimum window size assumptions can be violated? HOT 4
- error: format not a string literal and no format arguments HOT 6
- handle window resize on older systems HOT 2
- Multi-byte characters not supported? HOT 17
- Average should be calculated from the available samples instead of the total number of potentially availalbe values HOT 4
- Release a new version and support -v option? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ttyplot.