swift-numerics icon indicating copy to clipboard operation
swift-numerics copied to clipboard

[BigInt tests] ❌🐰 Int properties

Open LiarPrincess opened this issue 2 years ago • 0 comments
trafficstars

Please read the #242 Using tests from “Violet - Python VM written in Swift” before.


🐰 Discussion

  • bitWidth - current implementation rounds up to nearest multiple of 64 (I assume that this is because UInt.bitWidth == 64 on my platform). In Violet I went with counting min required bit count to represent a given number.

    func test_trivial() {
      let zero = BigInt(0)
      XCTAssertEqual(zero.bitWidth, 1) //  0 is just 0
    
      let plus1 = BigInt(1)
      XCTAssertEqual(plus1.bitWidth, 2) // 1 needs '0' prefix -> '01'
    
      let minus1 = BigInt(-1)
      XCTAssertEqual(minus1.bitWidth, 1) // -1 is just 1
    }
    
  • trailingZeroBitCount - what should be the value for 0? Current implementation gives 64 (same reason as for bitWidth), Violet returns 1.

    // There is an edge case for '0':
    // - 'int' is finite, so they can return 'bitWidth'
    // - 'BigInt' is infinite, but we can't return this
    //
    // So, trailingZeroBitCount is equal to bitWidth, see:
    // https://developer.apple.com/documentation/swift/binaryinteger/trailingzerobitcount
    func test_zero() {
      let zero = BigInt(0)
      XCTAssertEqual(zero.trailingZeroBitCount, 1)
    }
    

Let me know what you think, I will fix the tests.

❌ Failures

func test_magnitude() {
  let big = BigInt(-9223372036854775808)
  let expect = UInt64(9223372036854775808)
  XCTAssertEqual(big.magnitude, BigInt(expect))
}

LiarPrincess avatar Jan 18 '23 15:01 LiarPrincess