]> git.madduck.net Git - etc/awesome.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

luatz/timetable: Fix incorrect math for negative carry operations
authordaurnimator <quae@daurnimator.com>
Fri, 27 Jan 2017 00:46:24 +0000 (11:46 +1100)
committerdaurnimator <quae@daurnimator.com>
Fri, 27 Jan 2017 00:46:24 +0000 (11:46 +1100)
Closes #10

luatz/timetable.lua
spec/timetable_spec.lua

index e2930d8dcc287cdaef8aeb92aab6272cffc5c52e..0dbf4c8eedb5d6fdce95d0f80187ef08e1363de0 100644 (file)
@@ -76,8 +76,8 @@ local function carry ( tens , units , base )
                tens  = tens + idiv ( units , base )
                units = units % base
        elseif units < 0 then
-               tens  = tens - 1 + idiv ( -units , base )
-               units = base - ( -units % base )
+               tens  = tens + idiv ( units , base )
+               units = ( base + units ) % base
        end
        return tens , units
 end
index ddc05c6d6fb5825cc81e02b92fecc66aa34d30aa..930d36b32b94f398f260580352ee980a6e4497d8 100644 (file)
@@ -91,6 +91,13 @@ describe ( "Timetable library" , function ( )
                assert.same({ 2017,2,15,0,0,0 } , { timetable.normalise(2016,14.5,1,0,0,0) })
        end )
 
+       it ( "#normalise handles negative carry (issue #10)", function()
+               assert.same({ 1970,01,01,00,59,00 }, { timetable.normalise(1970,01,01,01,00,-60) })
+               assert.same({ 1970,01,01,00,58,58 }, { timetable.normalise(1970,01,01,01,00,-62) })
+               assert.same({ 1969,12,31,23,55,58 }, { timetable.normalise(1970,01,01,01,-63,-62) })
+               assert.same({ 2017,02,3,0,0,0 }, { timetable.normalise(2017,02,13,0,-14400,0) })
+       end )
+
        local function round_trip_add(t, field, x)
                local before = t:clone()
                t[field]=t[field]+x;