superlistapp / super_sliver_list

Drop-in replacement for SliverList and ListView that can handle large amount of items with variable extents and reliably jump / animate to any item.
https://superlistapp.github.io/super_sliver_list/
MIT License
277 stars 15 forks source link

ExtentList.totalExtent can drop below 0.0 #46

Closed kpsroka closed 5 months ago

kpsroka commented 5 months ago

Due to floating-point rounding, some sequence of updates to individual extents via ExtentList.setExtent can make total extent value to be below zero. Here's a simplified code that simulates the changes to two extents by same values (imagine two slivers changing sizes simultaneously):

void main() {
  final el = ExtentList();

  for (final newExtent in [31.500089999999997, 22.500045, 15.750045000000004, 9.000045000000004, 0.0]) {
    el.updateExtents(newExtent);
    print(el.totalExtent);
  }
}

class ExtentList {
  double totalExtent = 108.0;
  final extents = [54.0, 54.0];

  void updateExtents(double newExtent) {
    for (int index = 0; index < extents.length; index++) {
      totalExtent -= extents[index];
      extents[index] = newExtent;
      totalExtent += newExtent;
    }
  }
}

The totalExtent after each call to updateExtents() are as follows:

63.00018
45.00009
31.500090000000004
18.000090000000004
-3.552713678800501e-15