apple / swift-numerics

Advanced mathematical types and functions for Swift
Apache License 2.0
1.69k stars 147 forks source link

[BigInt tests] ❌ Binary `+` and `-` #246

Open LiarPrincess opened 1 year ago

LiarPrincess commented 1 year ago

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


❌ Failures

func test_binarySub() {
  // https://www.wolframalpha.com/input?i=-922337203685477587+-+%28-9223372036854775808%29
  let lhs = BigInt("-922337203685477587")!
  let rhs = BigInt("-9223372036854775808")!
  let expected = BigInt("8301034833169298221")!
  XCTAssertEqual(lhs - rhs, expected)
  // The same on Swift.Int:
  XCTAssertEqual(-922337203685477587 - (-9223372036854775808), 8301034833169298221)
}

func test_binarySub_2() {
  typealias Word = BigIntPrototype.Word

  let intMax = Int.max
  let intMaxAsWord = Word(intMax.magnitude)

  // intMax - (-(Word.max - intMaxAsWord)) =
  // intMax - (-Word.max + intMaxAsWord) =
  // intMax + Word.max - intMaxAsWord =
  // Word.max
  let max = BigInt(intMax)
  let value = BigInt(.negative, magnitude: Word.max - intMaxAsWord)
  let expected = BigInt(.positive, magnitude: Word.max)
  XCTAssertEqual(max - value, expected)
}