Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Calc9// File : Packages/ORIXUnits/Tests/ORIXUnitsTests/ExactArithmeticTests.swift10// Description : Big-integer quotient: exact termination, half-even11// rounding at 38 digits, signs, range.12// Module : ORIXUnitsTests13// Repository : https://git.spboucher.ai/orix-calc14// Created : 2026-11-2015// Last Modified : 2026-11-2016//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author : Simon-Pierre Boucher22// Position : Professor of Finance23// Institution : Université du Québec en Outaouais (UQO)24// Department : Département des sciences administratives25// Organization : SP Boucher26//27// Website : https://spboucher.ai28// Git : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General : info@spboucher.ai35// Professional : contact@spboucher.ai36// Direct : simon@spboucher.ai37// Research : research@spboucher.ai38// Development : dev@spboucher.ai39// Support : support@spboucher.ai40// Security/Admin : admin@spboucher.ai41// Notifications : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification : Public68// Distribution : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License : Proprietary, all rights reserved (see license file)82// License File : LICENSE83// Maintainer : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation91import Testing9293@testable import ORIXUnits9495@Suite("Exact arithmetic")96struct ExactArithmeticTests {97 private func quotient(_ numerators: [String], _ denominators: [String]) -> Decimal? {98 ExactArithmetic.quotient(numerators: numerators.map(dec), denominators: denominators.map(dec))99 }100101 @Test("Terminating quotients are exact")102 func terminating() {103 #expect(quotient(["0.3048"], ["0.3048"]) == 1)104 #expect(quotient(["2458.35"], ["9"]) == dec("273.15"))105 #expect(quotient(["1"], ["8"]) == dec("0.125"))106 #expect(quotient(["1", "1609.344"], ["1000"]) == dec("1.609344"))107 #expect(quotient(["0"], ["7"]) == 0)108 #expect(quotient(["-1"], ["4"]) == dec("-0.25"))109 #expect(quotient(["-1"], ["-4"]) == dec("0.25"))110 #expect(quotient(["1e30", "1e30", "1e30"], ["1e30", "1e30", "1e29"]) == 10)111 #expect(quotient(["12345678901234567890123456789012345678"], ["1"]) == dec("12345678901234567890123456789012345678"))112 // Operands whose product exceeds 38 digits but whose quotient is short.113 #expect(114 quotient(115 ["12345678901234567890123456789012345678", "98765432109876543210987654321098765432"],116 ["98765432109876543210987654321098765432"]) == dec("12345678901234567890123456789012345678"))117 }118119 @Test("Non-terminating quotients are rounded half-to-even to 38 digits")120 func rounding() {121 #expect(quotient(["1"], ["3"]) == dec("0.33333333333333333333333333333333333333"))122 #expect(quotient(["2"], ["3"]) == dec("0.66666666666666666666666666666666666667"))123 #expect(quotient(["1"], ["7"]) == dec("0.14285714285714285714285714285714285714"))124 #expect(quotient(["1"], ["149597870700"]) == dec("0.0000000000066845871222684454959959533702106362935"))125 // Ties at the 39th digit go to the even neighbour: (2·10³⁷ + 1)/2 and (2·10³⁷ + 3)/2.126 #expect(quotient(["20000000000000000000000000000000000001"], ["2"]) == dec("10000000000000000000000000000000000000"))127 #expect(quotient(["20000000000000000000000000000000000003"], ["2"]) == dec("10000000000000000000000000000000000002"))128 #expect(quotient(["20000000000000000000000000000000000001", "1.0000000001"], ["2", "1.0000000001"]) == dec("10000000000000000000000000000000000000"))129 // Just above a tie: (10³⁷ + 0.5)(1 + 10⁻²⁰) = 10³⁷ + 10¹⁷ + 0.5 + 5·10⁻²¹ rounds up to an odd digit.130 #expect(quotient(["20000000000000000000000000000000000001", "1.00000000000000000001"], ["2"]) == dec("10000000000000000000100000000000000001"))131 // Carry out of the 38th digit: (10⁵⁰ − 1)/10⁵⁰ = 0.99…9 (50 nines) rounds to 1.132 #expect(quotient(["9999999999999999999999999", "10000000000000000000000001"], ["1e50"]) == 1)133 }134135 @Test("Invalid operands and range")136 func invalid() {137 #expect(quotient(["1"], ["0"]) == nil)138 #expect(ExactArithmetic.quotient(numerators: [Decimal.nan], denominators: [1]) == nil)139 #expect(quotient(["1e100", "1e100"], ["1"]) == nil)140 #expect(quotient(["1e-100"], ["1e100"]) == nil)141 // Exponent underflow with spare digits keeps the leading digits.142 let tiny = quotient(["1e-100"], ["3"])143 #expect(tiny != nil)144 if let tiny { #expect(isClose(tiny, dec("3.33333333333333333333333333e-101"), relative: dec("1e-25"))) }145 }146147 @Test("Exact product comparison")148 func products() {149 #expect(ExactArithmetic.productsAreEqual([dec("0.5"), 4], [2]))150 #expect(ExactArithmetic.productsAreEqual([dec("1e10")], [dec("10000000000")]))151 #expect(!ExactArithmetic.productsAreEqual([dec("0.5"), 4], [dec("2.0000000000000000000000000000000000001")]))152 #expect(!ExactArithmetic.productsAreEqual([1], [-1]))153 #expect(ExactArithmetic.productsAreEqual([0, 5], [0]))154 #expect(ExactRational("1", over: "2").isEquivalent(to: ExactRational("2", over: "4")))155 #expect(ExactRational("1", over: "2") != ExactRational("2", over: "4"))156 #expect(ExactRational("3").power(-2).isEquivalent(to: ExactRational("1", over: "9")))157 #expect(ExactRational.powerOfTen(-3).decimalValue == dec("0.001"))158 #expect((ExactRational("7") / 2).decimalValue == dec("3.5"))159 #expect(ExactRational("2", over: "3").reciprocal.decimalValue == dec("1.5"))160 #expect(ExactRational("0.5").isPositive)161 let three: ExactRational = 3162 #expect(three.decimalValue == 3)163 }164165 @Test("Big integer digit counts")166 func digitCounts() {167 #expect(BigUInt(0).decimalDigitCount == 0)168 #expect(BigUInt(9).decimalDigitCount == 1)169 #expect(BigUInt(10).decimalDigitCount == 2)170 #expect(BigUInt(9_999_999_999_999_999_999).decimalDigitCount == 19)171 #expect(BigUInt(10_000_000_000_000_000_000).decimalDigitCount == 20)172 var big = BigUInt(1)173 big.multiplyByPowerOfTen(75)174 #expect(big.decimalDigitCount == 76)175 #expect((big * big).decimalDigitCount == 151)176 }177}178