positive/negative 取模以获得范围内的项目
positive/negative Modulo to get items in range
我看了 https://math.stackexchange.com/questions/519845/modulo-of-a-negative-number and Modulo operation with negative numbers 但我仍然想知道如何使用负模值来获取范围内的项目。
下面是我的问题之前的一个简化用例,我有一个包含 3 张幻灯片的幻灯片 (slidesCount = 3
):
slide indexes: 0 1 2
现在我想从这些数字访问正确的幻灯片:
-2 -1 0 1 2 3 4
should match slide:
1 2 0 1 2 0 1
因此 index % slidesCount
我涵盖了案例:
0 1 2 3 4
但不是负值。 -1 % 3
returns -1
如果索引为负数,那么 slidesCount + index % slidesCount
是正确的表达式吗?
首先,有没有simpler/smarter的写法:
index = index % slidesCount + (index < 0 ? slidesCount : 0)
现在我的问题是每张幻灯片有 3 个可见项目的幻灯片放映,
最后一张幻灯片可能只有一个项目(下面的索引 9),因此从这些数字来看:
-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
我要匹配幻灯片:
9 0 3 6 9 0
我希望下面的图表有意义!请帮我用最少 if
s:
得到正确的方程式
-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
|________| |______________________________________________|
|| || ||
|| Math.floor( i / visibleSlides )
Math.ceil(i / visibleSlides) || ||
|| || ||
\/ \/ \/
-1 0 1 2 3 4
|___| |_______________________| |___|
|| || ||
slidesCnt + i % visibleSlides i % visibleSlides || ??
|| || ||
\/ \/ \/
3 0 1 2 3 0
|| i * visibleSlides
\/
9 0 3 6 9 0
最后我是这样解决问题的:
// Prevents out of range.
// e.g. -1, -2, 12, 13
let newIndex = (index + slidesCount) % slidesCount
// Calculates how many items are missing on last slide.
// e.g. if 10 slides with 3 visible slides, 2 missing slides on last slide.
let lastSlideItems = slidesCount % visibleSlides || visibleSlides
let missingItems = visibleSlides - lastSlideItems
// If index is negative adjust by adding the missing slides.
newIndex += index < 0 ? missingItems : 0
// Always get the first item of the series of visible items.
// e.g. if index is 8 and 3 visible slides, newIndex will be 6.
newIndex = Math.floor(newIndex / visibleSlides) * visibleSlides
我看了 https://math.stackexchange.com/questions/519845/modulo-of-a-negative-number and Modulo operation with negative numbers 但我仍然想知道如何使用负模值来获取范围内的项目。
下面是我的问题之前的一个简化用例,我有一个包含 3 张幻灯片的幻灯片 (slidesCount = 3
):
slide indexes: 0 1 2
现在我想从这些数字访问正确的幻灯片:
-2 -1 0 1 2 3 4
should match slide:
1 2 0 1 2 0 1
因此 index % slidesCount
我涵盖了案例:
0 1 2 3 4
但不是负值。 -1 % 3
returns -1
如果索引为负数,那么 slidesCount + index % slidesCount
是正确的表达式吗?
首先,有没有simpler/smarter的写法:
index = index % slidesCount + (index < 0 ? slidesCount : 0)
现在我的问题是每张幻灯片有 3 个可见项目的幻灯片放映, 最后一张幻灯片可能只有一个项目(下面的索引 9),因此从这些数字来看:
-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
我要匹配幻灯片:
9 0 3 6 9 0
我希望下面的图表有意义!请帮我用最少 if
s:
-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
|________| |______________________________________________|
|| || ||
|| Math.floor( i / visibleSlides )
Math.ceil(i / visibleSlides) || ||
|| || ||
\/ \/ \/
-1 0 1 2 3 4
|___| |_______________________| |___|
|| || ||
slidesCnt + i % visibleSlides i % visibleSlides || ??
|| || ||
\/ \/ \/
3 0 1 2 3 0
|| i * visibleSlides
\/
9 0 3 6 9 0
最后我是这样解决问题的:
// Prevents out of range.
// e.g. -1, -2, 12, 13
let newIndex = (index + slidesCount) % slidesCount
// Calculates how many items are missing on last slide.
// e.g. if 10 slides with 3 visible slides, 2 missing slides on last slide.
let lastSlideItems = slidesCount % visibleSlides || visibleSlides
let missingItems = visibleSlides - lastSlideItems
// If index is negative adjust by adding the missing slides.
newIndex += index < 0 ? missingItems : 0
// Always get the first item of the series of visible items.
// e.g. if index is 8 and 3 visible slides, newIndex will be 6.
newIndex = Math.floor(newIndex / visibleSlides) * visibleSlides