Skip to content

Replace Float with Double as backing datatype for chart data#87

Merged
gpbl merged 2 commits intogpbl:masterfrom
trein:master
Jan 7, 2018
Merged

Replace Float with Double as backing datatype for chart data#87
gpbl merged 2 commits intogpbl:masterfrom
trein:master

Conversation

@trein
Copy link
Copy Markdown

@trein trein commented Jan 1, 2018

This PR prevents rounding errors when charts are constructed with data composed of Double values.

@gpbl
Copy link
Copy Markdown
Owner

gpbl commented Jan 2, 2018

Thanks! Could you provide an example to replicate the issue this PR is fixing?

@trein
Copy link
Copy Markdown
Author

trein commented Jan 2, 2018

Hi @gpbl,

Absolutely. I was plotting a set of Double points representing timestamps (Date#timeIntervalSince1970) and currency values. My timestamps values where dividing a single hour into 60 intervals (i.e. one point per minute). After playing with SwiftChart, I noticed that my timestamps points started overlapping.

After some investigation, I realized that SwiftChart exposes an initializer ChartSeries(data: [(x: Double, y: Double)]) but converts data internally into [(x: Float, y: Float)]. Float is unable to express a Double accurately due differences in internal representation (Float uses 32 bits and Double uses 64 bits).

Bottom line, my timestamps values were losing precision and ended up creating overlapping points in the graph. Here is a sample of my timestamps values showing the problem:

Original timestamp values: [1514895300.0, 1514895360.0, 1514895420.0, 1514895480.0, 1514895540.0, 1514895600.0, 1514895660.0, 1514895720.0, 1514895780.0, 1514895840.0, 1514895900.0, 1514895960.0, 1514896020.0, 1514896080.0, 1514896140.0, 1514896200.0, 1514896260.0, 1514896320.0, 1514896380.0, 1514896440.0, 1514896500.0, 1514896560.0, 1514896620.0, 1514896680.0, 1514896740.0, 1514896800.0, 1514896860.0, 1514896920.0, 1514896980.0, 1514897040.0, 1514897100.0, 1514897160.0, 1514897220.0, 1514897280.0, 1514897340.0, 1514897400.0, 1514897460.0, 1514897520.0, 1514897580.0, 1514897640.0, 1514897700.0, 1514897760.0, 1514897820.0, 1514897880.0, 1514897940.0, 1514898000.0, 1514898060.0, 1514898120.0, 1514898180.0, 1514898240.0, 1514898300.0, 1514898360.0, 1514898420.0, 1514898480.0, 1514898540.0, 1514898600.0, 1514898660.0, 1514898720.0, 1514898780.0, 1514898840.0, 1514898900.0]
Converted timestamp values: [1.51489536e+09, 1.51489536e+09, 1.51489536e+09, 1.51489549e+09, 1.51489549e+09, 1.51489562e+09, 1.51489562e+09, 1.51489574e+09, 1.51489574e+09, 1.51489587e+09, 1.51489587e+09, 1.514896e+09, 1.514896e+09, 1.51489613e+09, 1.51489613e+09, 1.51489626e+09, 1.51489626e+09, 1.51489638e+09, 1.51489638e+09, 1.51489638e+09, 1.51489651e+09, 1.51489651e+09, 1.51489664e+09, 1.51489664e+09, 1.51489677e+09, 1.51489677e+09, 1.5148969e+09, 1.5148969e+09, 1.51489702e+09, 1.51489702e+09, 1.51489715e+09, 1.51489715e+09, 1.51489728e+09, 1.51489728e+09, 1.51489728e+09, 1.51489741e+09, 1.51489741e+09, 1.51489754e+09, 1.51489754e+09, 1.51489766e+09, 1.51489766e+09, 1.51489779e+09, 1.51489779e+09, 1.51489792e+09, 1.51489792e+09, 1.51489805e+09, 1.51489805e+09, 1.51489818e+09, 1.51489818e+09, 1.51489818e+09, 1.5148983e+09, 1.5148983e+09, 1.51489843e+09, 1.51489843e+09, 1.51489856e+09, 1.51489856e+09, 1.51489869e+09, 1.51489869e+09, 1.51489882e+09, 1.51489882e+09, 1.51489894e+09]

Take the first two values of each stream. Although they are represented by two distinct Double numbers
[1514895300.0, 1514895360.0] they ended up mapped to same values [1.51489536e+09, 1.51489536e+09] after conversion to Float.

Please, let me know if it helps.

- Handle gracefully empty series data by preventing
  optionals from being forced-unwrapped.

- Introduce minor cosmetics improvements.
@trein
Copy link
Copy Markdown
Author

trein commented Jan 6, 2018

Hi @gpbl . Just a correction here. This is not an enhancement. This is a bug fix. Anyone using this constructor will potentially experience representation issues. Simply put, Double variables cannot be represented as Float.

@gpbl
Copy link
Copy Markdown
Owner

gpbl commented Jan 6, 2018

Thanks for your help! I think this is the time to cut a v1.0 release 👍🏽

@gpbl gpbl merged commit 79659fc into gpbl:master Jan 7, 2018
gpbl added a commit that referenced this pull request Jan 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants