Make Int round off to nearest value

后端 未结 2 1066
失恋的感觉
失恋的感觉 2020-12-12 02:37

I\'ve got two Int values (they have to be Ints) and I want them to round off to the nearest value when in an equation;

var Example = Int()
var secondExample          


        
相关标签:
2条回答
  • 2020-12-12 02:44

    see Martin's answer! his idea is great, so i extend his solution for negative numbers

    func divideAndRound(n: Int, _ d: Int) -> Int {
        let sn = n < 0 ? -1 : 1
        let sd = d < 0 ? -1 : 1
        let s = sn * sd
        let n = n * sn
        let d = d * sd
        return (2 * n + d)/(2 * d) * s
    }
    
    divideAndRound(1, 2) // 1
    divideAndRound(1, 3) // 0
    divideAndRound(-1, 2) // -1
    divideAndRound(-1, 3) // 0
    divideAndRound(1, -2) // -1
    divideAndRound(1, -3) // 0
    

    the only trouble is that (2 * n + d) can overflow and code will crash.

    UPDATE! with help of mathematics for children

    func divr(a: Int, _ b: Int)->Int {
        return (a % b) * 2 / b  + a / b
    }
    

    it should work for any Int :-) except 0 denominator.

    0 讨论(0)
  • 2020-12-12 02:52

    For nonnegative integers, the following function gives the desired result in pure integer arithmetic :

    func divideAndRound(numerator: Int, _ denominator: Int) -> Int {
        return (2 * numerator + denominator)/(2 * denominator)
    }
    

    Examples:

    print(20000.0/7000.0) // 2.85714285714286
    print(divideAndRound(20000, 7000)) // 3 (rounded up)
    
    print(10000.0/7000.0) // 1.42857142857143
    print(divideAndRound(10000, 7000)) // 1 (rounded down)
    

    The idea is that

     a   1   2 * a + b
     - + - = ---------
     b   2     2 * b
    

    And here is a possible implementation for arbitrarily signed integers which also does not overflow:

    func divideAndRound(num: Int, _ den: Int) -> Int {
        return num / den + (num % den) / (den / 2 + den % 2)
    }
    

    (Based on @user3441734's updated solution, so we have a reference cycle between our answers now :)

    There is also a ldiv function which computes both quotient and remainder of a division, so the last function could also be implemented as

    func divideAndRound(num: Int, _ den: Int) -> Int {
        let div = ldiv(num, den)
        let div2 = ldiv(den, 2)
        return div.quot + div.rem / (div2.quot + div2.rem)
    }
    

    (I did not test which version is faster.)

    0 讨论(0)
提交回复
热议问题