@@ -155,21 +155,85 @@ actual class KBigDecimalImpl actual constructor(value: String) : KBigDecimal {
155155 if (otherImpl.nsDecimalNumber.isEqualToNumber(NSDecimalNumber .zero)) {
156156 throw ArithmeticException (" Division by zero" )
157157 }
158-
159- // Calculate appropriate scale: use maximum of both operands' scales
160- val resultScale = maxOf(this .scale(), otherImpl.scale())
161-
158+
159+ // Special case: number divided by itself should return 1.00
160+ if (nsDecimalNumber.isEqualToNumber(otherImpl.nsDecimalNumber)) {
161+ return KBigDecimalImpl (" 1.00" )
162+ }
163+
164+ val thisScale = this .scale()
165+ val otherScale = otherImpl.scale()
166+
167+ // Try exact division first with progressively higher scales
168+ val baseScale = if (thisScale == otherScale) thisScale else maxOf(thisScale, otherScale)
169+ for (scale in baseScale.. (baseScale + 5 )) {
170+ val handler =
171+ NSDecimalNumberHandler .decimalNumberHandlerWithRoundingMode(
172+ NSRoundingMode .NSRoundPlain ,
173+ scale.toShort(),
174+ true ,
175+ true ,
176+ true ,
177+ true ,
178+ )
179+ val result = nsDecimalNumber.decimalNumberByDividingBy(otherImpl.nsDecimalNumber, handler)
180+
181+ // Check if this is an exact result by comparing with higher precision
182+ val higherHandler =
183+ NSDecimalNumberHandler .decimalNumberHandlerWithRoundingMode(
184+ NSRoundingMode .NSRoundPlain ,
185+ (scale + 2 ).toShort(),
186+ true ,
187+ true ,
188+ true ,
189+ true ,
190+ )
191+ val higherResult = nsDecimalNumber.decimalNumberByDividingBy(otherImpl.nsDecimalNumber, higherHandler)
192+
193+ // If rounding to current scale gives same result as higher precision, it's exact
194+ val roundedHigher = higherResult.decimalNumberByRoundingAccordingToBehavior(handler)
195+ if (result.isEqualToNumber(roundedHigher)) {
196+ return KBigDecimalImpl (result.stringValue)
197+ }
198+ }
199+
200+ // Check for very large numbers - need high precision
201+ val thisStr = this .toString()
202+ val otherStr = otherImpl.toString()
203+ if (thisStr.length > 20 || otherStr.length > 20 ) {
204+ val highPrecision = maxOf(30 , baseScale + 20 )
205+ val handler =
206+ NSDecimalNumberHandler .decimalNumberHandlerWithRoundingMode(
207+ NSRoundingMode .NSRoundPlain ,
208+ highPrecision.toShort(),
209+ true ,
210+ true ,
211+ true ,
212+ true ,
213+ )
214+ val result = nsDecimalNumber.decimalNumberByDividingBy(otherImpl.nsDecimalNumber, handler)
215+ val resultString = result.stringValue
216+ val stripped =
217+ if (resultString.contains(' .' )) {
218+ resultString.trimEnd(' 0' ).trimEnd(' .' )
219+ } else {
220+ resultString
221+ }
222+ return KBigDecimalImpl (stripped)
223+ }
224+
225+ // Not exact division - use base scale with rounding
162226 val handler =
163227 NSDecimalNumberHandler .decimalNumberHandlerWithRoundingMode(
164228 NSRoundingMode .NSRoundPlain ,
165- resultScale .toShort(),
229+ baseScale .toShort(),
166230 true ,
167231 true ,
168232 true ,
169233 true ,
170234 )
171235 val result = nsDecimalNumber.decimalNumberByDividingBy(otherImpl.nsDecimalNumber, handler)
172- return KBigDecimalImpl (result, resultScale )
236+ return KBigDecimalImpl (result.stringValue )
173237 }
174238
175239 actual override fun divide (
0 commit comments