I ran across this rather strange failure in one of my tests and am at a loss as 
to what's causing it. Can someone please enlighten me?

For the purposes of this post the filename is roundxxx.nim. I left in a bunch 
of extra assertions just to show it otherwise works as expected. 
    
    
    import math
    
    proc round*(n: float, d: int = 0): float =
      floor(n * pow(10, (d.float * -1)) + 0.5) * pow(10, d.float)
    
    when isMainModule:
      block:
        doAssert round(0.1) == 0.0
        doAssert round(0.1, 0) == 0.0
        doAssert round(0.1, -1) == 0.1
        doAssert round(1.49) == 1.0
        doAssert round(1.49, 0) == 1.0
        doAssert round(1.49, -1) == 1.5
        doAssert round(1.49, -2) == 1.49
        doAssert round(1.5) == 2.0
        doAssert round(1.9) == 2.0
        doAssert round(1.79, -1) == 1.8
        doAssert round(1.95, -1) == 2.0
        doAssert round(1.95, -2) == 1.95
        doAssert round(1.955, -2) == 1.96
        doAssert round(1, 0) == 1.0
        doAssert round(1, 1) == 0.0
        doAssert round(9, 1) == 10.0
        doAssert round(149) == 149.0
        doAssert round(149, 0) == 149.0
        doAssert round(149, 1) == 150.0
        doAssert round(149, 2) == 100.0
        doAssert round(150, 2) == 200.0
        doAssert round(499, 2) == 500.0
        doAssert round(499, 3) == 0.0
        doAssert round(15) == 15.0
        doAssert round(19) == 19.0
        doAssert round(19, 1) == 20.0
        doAssert round(195, 1) == 200.0
        doAssert round(195, 2) == 200.0
        doAssert round(195.4) == 195.0
        doAssert round(195.5) == 196.0
        # XXX this is some strangeness going on here
        doAssert round(1.89, -1) == 1.9 # XXX I've no clue why this fails
        let n = round(1.89, -1)         # XXX when the exact same values if
        doAssert $n == "1.9"            # XXX casted to strings pass just fine
    
    
    Run

The result of nim c -r roundxxx.nim is as follows 
    
    
    roundxxx.nim(39)         roundxxx
    system.nim(3790)         failedAssertImpl
    system.nim(3783)         raiseAssert
    system.nim(2830)         sysFatal
    Error: unhandled exception:
    [redacted]/roundxxx.nim(39, 14)
    `round(1.89, -1) == 1.9`  [AssertionError]
    Error: execution of an external program failed:
    '[redacted]/roundxxx '
    
    
    Run

FYI: nim --version
    
    
    Nim Compiler Version 0.19.0 [Linux: amd64]
    Compiled at 2018-09-26
    Copyright (c) 2006-2018 by Andreas Rumpf
    
    git hash: f6c5c636bb1a1f4e1301ae0ba5a8afecef439132
    active boot switches: -d:release
    
    
    Run

Reply via email to