ldecNumber

Check-in [3eab477814]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix bug in mod for zero remainder, update tests and doco. Tweak makefile.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:3eab4778144b1b4e4aa7fb9c5becd10bcc516ec4
User & Date: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc 2006-09-25 14:43:52
Context
2006-09-26
18:48
Fix typos in doco (thanks Mike!). check-in: 9853890ce4 user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
2006-09-25
14:43
Fix bug in mod for zero remainder, update tests and doco. Tweak makefile. check-in: 3eab477814 user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
00:48
Fix setdefault(INIT_BASE) to clear traps. Document that. Add some tests of divide by zero. Remove .xtml file from distro. check-in: 90090ab04d user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Makefile.

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#T= $(MYLIB).so
T= $(MYLIB).dll

TESTLIBS=test/lperformance.DLL

all: $T

$T:	$(OBJS)
	$(CC) $(SHFLAGS) -o $@ -shared $(OBJS) $(LIBS)

$(MYLIB).o: version.h

version.h: vcheck

vcheck:







|







34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#T= $(MYLIB).so
T= $(MYLIB).dll

TESTLIBS=test/lperformance.DLL

all: $T

$T:	version.h $(OBJS)
	$(CC) $(SHFLAGS) -o $@ -shared $(OBJS) $(LIBS)

$(MYLIB).o: version.h

version.h: vcheck

vcheck:

Changes to doc/ldecNumber.odt.

cannot compute difference between binary files

Changes to ldecNumber.c.

328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
static int dn_mod (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    decNumber *dn2 = ldn_get (L, dc, 2);
    decNumber *dnr = ldn_make_decNumber (L);
    decNumberRemainder (dnr, dn1, dn2, dc);
    if (decNumberIsNegative(dn1) != decNumberIsNegative(dn2))
    {
        // convert remainder to modulo for mismatched signs
        decNumberAdd (dnr, dnr, dn2, dc);
    }
    return 1;
}








|







328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
static int dn_mod (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    decNumber *dn2 = ldn_get (L, dc, 2);
    decNumber *dnr = ldn_make_decNumber (L);
    decNumberRemainder (dnr, dn1, dn2, dc);
    if (decNumberIsNegative(dn1) != decNumberIsNegative(dn2) && !decNumberIsZero(dnr))
    {
        // convert remainder to modulo for mismatched signs
        decNumberAdd (dnr, dnr, dn2, dc);
    }
    return 1;
}

Changes to test/ldecNumberUnitTest.lua.

233
234
235
236
237
238
239






















240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    assert_true (zero:divideinteger(zero):isnan())
    assert_true (zero:remainder(zero):isnan())
    assert_true (zero:remaindernear(zero):isnan())
    assert_true (one:divide(zero):isinfinite())
    assert_true (one:divideinteger(zero):isinfinite())
    assert_true (one:remainder(zero):isnan())
    assert_true (one:remaindernear(zero):isnan())






















    a = one
    b = one
    assert_equal (a:remainder(b),a - b * a:divideinteger(b))
    assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
    assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
    assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
    for i = 1,100 do
        x = math.random(999999999)
        y = math.random(999999999)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a:remainder(b),a - b * a:divideinteger(b))
        assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
        assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
        assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
        assert_equal (a%b,decNumber.tonumber(x%y))
        assert_equal ((-a)%b,decNumber.tonumber((-x)%y))
        assert_equal (a%(-b),decNumber.tonumber(x%(-y)))
        assert_equal ((-a)%(-b),decNumber.tonumber((-x)%(-y)))
    end
    ctx:setround (decNumber.ROUND_CEILING)
    a = one
    b = zero
    assert_nequal (a:remainder(b),a - b * a:divideinteger(b))
    assert_nequal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
    assert_nequal (a:remainder(-b),a + b * a:divideinteger(-b))
    assert_nequal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
    for i = 1,100 do
        x = math.random(999999999)
        y = math.random(999999999)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a:remainder(b),a - b * a:divideinteger(b))
        assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
        assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
        assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
        assert_equal (a%b,decNumber.tonumber(x%y))
................................................................................
    a = zero
    b = zero
    assert_nequal (a:remainder(b),a - b * a:divideinteger(b))
    assert_nequal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
    assert_nequal (a:remainder(-b),a + b * a:divideinteger(-b))
    assert_nequal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
    for i = 1,100 do
        x = math.random(999999999)/2^math.random(99)
        y = math.random(999999999)/2^math.random(99)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a:remainder(b),a - b * a:divideinteger(b))
        assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
        assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
        assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
        assert_qequal (a%b,decNumber.tonumber(x%y))
        assert_qequal ((-a)%b,decNumber.tonumber((-x)%y))
        assert_qequal (a%(-b),decNumber.tonumber(x%(-y)))
        assert_qequal ((-a)%(-b),decNumber.tonumber((-x)%(-y)))
    end
    ctx:setround (decNumber.ROUND_CEILING)
    a = zero
    b = one
    assert_equal (a:remainder(b),a - b * a:divideinteger(b))
    assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
    assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
    assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
    for i = 1,100 do
        x = math.random(999999999)/2^math.random(99)
        y = math.random(999999999)/2^math.random(99)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a:remainder(b),a - b * a:divideinteger(b))
        assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
        assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
        assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
        assert_qequal (a%b,decNumber.tonumber(x%y))







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|






|
|












|






|
|







 







|
|













|





|
|







233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
...
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
    assert_true (zero:divideinteger(zero):isnan())
    assert_true (zero:remainder(zero):isnan())
    assert_true (zero:remaindernear(zero):isnan())
    assert_true (one:divide(zero):isinfinite())
    assert_true (one:divideinteger(zero):isinfinite())
    assert_true (one:remainder(zero):isnan())
    assert_true (one:remaindernear(zero):isnan())
    -- special cases
    x = 3
    y = -1
    a = decNumber.tonumber(x)
    b = decNumber.tonumber(y)
    assert_equal (a%b,decNumber.tonumber(x%y))
    x = 3
    y = 1
    a = decNumber.tonumber(x)
    b = decNumber.tonumber(y)
    assert_equal (a%b,decNumber.tonumber(x%y))
    x = -3
    y = 1
    a = decNumber.tonumber(x)
    b = decNumber.tonumber(y)
    assert_equal (a%b,decNumber.tonumber(x%y))
    x = -3
    y = -1
    a = decNumber.tonumber(x)
    b = decNumber.tonumber(y)
    assert_equal (a%b,decNumber.tonumber(x%y))
    -- 
    a = one + one
    b = one
    assert_equal (a:remainder(b),a - b * a:divideinteger(b))
    assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
    assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
    assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
    for i = 1,100 do
        x = (-1^math.random(2)) * math.random(999999999)
        y = (-1^math.random(2)) * math.random(999999999)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a:remainder(b),a - b * a:divideinteger(b))
        assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
        assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
        assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
        assert_equal (a%b,decNumber.tonumber(x%y))
        assert_equal ((-a)%b,decNumber.tonumber((-x)%y))
        assert_equal (a%(-b),decNumber.tonumber(x%(-y)))
        assert_equal ((-a)%(-b),decNumber.tonumber((-x)%(-y)))
    end
    ctx:setround (decNumber.ROUND_CEILING)
    a = one + one
    b = zero
    assert_nequal (a:remainder(b),a - b * a:divideinteger(b))
    assert_nequal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
    assert_nequal (a:remainder(-b),a + b * a:divideinteger(-b))
    assert_nequal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
    for i = 1,100 do
        x = (-1^math.random(2)) * math.random(999999999)
        y = (-1^math.random(2)) * math.random(999999999)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a:remainder(b),a - b * a:divideinteger(b))
        assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
        assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
        assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
        assert_equal (a%b,decNumber.tonumber(x%y))
................................................................................
    a = zero
    b = zero
    assert_nequal (a:remainder(b),a - b * a:divideinteger(b))
    assert_nequal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
    assert_nequal (a:remainder(-b),a + b * a:divideinteger(-b))
    assert_nequal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
    for i = 1,100 do
        x = (-1^math.random(2)) * math.random(999999999)/2^math.random(99)
        y = (-1^math.random(2)) * math.random(999999999)/2^math.random(99)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a:remainder(b),a - b * a:divideinteger(b))
        assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
        assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
        assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
        assert_qequal (a%b,decNumber.tonumber(x%y))
        assert_qequal ((-a)%b,decNumber.tonumber((-x)%y))
        assert_qequal (a%(-b),decNumber.tonumber(x%(-y)))
        assert_qequal ((-a)%(-b),decNumber.tonumber((-x)%(-y)))
    end
    ctx:setround (decNumber.ROUND_CEILING)
    a = zero
    b = one + one
    assert_equal (a:remainder(b),a - b * a:divideinteger(b))
    assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
    assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
    assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
    for i = 1,100 do
        x = (-1^math.random(2)) * math.random(999999999)/2^math.random(99)
        y = (-1^math.random(2)) * math.random(999999999)/2^math.random(99)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a:remainder(b),a - b * a:divideinteger(b))
        assert_equal ((-a):remainder(b),(-a) - b * (-a):divideinteger(b))
        assert_equal (a:remainder(-b),a + b * a:divideinteger(-b))
        assert_equal ((-a):remainder(-b),(-a) + b * (-a):divideinteger(-b))
        assert_qequal (a%b,decNumber.tonumber(x%y))