ldecNumber

Check-in [655e4351ee]
Login

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

Overview
Comment:Add floor function and associated tests and doco.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:655e4351ee03e5a63f1784fa185b6a959ef4e8b5
User & Date: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc 2006-09-28 22:31:19
Context
2006-10-02
20:00
Integrate Asko's generalized Makefile. Add __concat to decimal numbers. Update doco and README.txt check-in: 6225bab0ea user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
2006-09-28
22:31
Add floor function and associated tests and doco. check-in: 655e4351ee user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
2006-09-27
18:08
Add duplicate context. Include setcontext in context methods. Add some rounding tests. Update doco. check-in: 17e9bff5b9 user: e@6e5be3b1-1950-f047-a965-c680c9cf6ecc tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to doc/ldecNumber.odt.

cannot compute difference between binary files

Changes to ldecNumber.c.

51
52
53
54
55
56
57










58
59
60
61
62
63
64
...
332
333
334
335
336
337
338





























339
340
341
342
343
344
345
...
629
630
631
632
633
634
635

636
637
638
639
640
641
642
...
721
722
723
724
725
726
727

728
729
730
731
732
733
734
...
745
746
747
748
749
750
751


752
753
754
755
756
757
758
#define DN_CONTEXT_META " decNumber_CoNTeXT_MeTA"
#define DN_DNUMBER_META " decNumber_NuMBeR_MeTA"

#define DN_VERSION "Lua " DECFULLNAME " version " SVN_REVS " for " LUA_VERSION " with " DECVERSION

const char *dn_context_meta = DN_CONTEXT_META;
const char *dn_dnumber_meta = DN_DNUMBER_META;











/* ***************** context support functions ***************** */

/*
There is one decNumber decContext per Lua thread. This library stores 
these decContexts as full userdata instances in the LUA_ENVIRONINDEX; 
the keys of this table are the thread addresses of the thread owning the
................................................................................
    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;
}

/* samequantum -- call needs no context */

static int dn_samequantum (lua_State *L)
................................................................................
    {"quantize",        dn_quantize      },
    {"remainder",       dn_remainder     },
    {"remaindernear",   dn_remaindernear },
    {"rescale",         dn_rescale       },
    {"samequantum",     dn_samequantum   },
    
    {"mod",             dn_mod           },


    {"iszero",          dn_iszero        },
    {"isnegative",      dn_isneg         },
    {"isnan",           dn_isnan         },
    {"isqnan",          dn_isqnan        },
    {"issnan",          dn_issnan        },
    {"isinfinite",      dn_isinf         },
................................................................................
    {"quantize",        dn_quantize      },
    {"remainder",       dn_remainder     },
    {"remaindernear",   dn_remaindernear },
    {"rescale",         dn_rescale       },
    {"samequantum",     dn_samequantum   },

    {"mod",             dn_mod           },


    {"iszero",          dn_iszero        },
    {"isnegative",      dn_isneg         },
    {"isnan",           dn_isnan         },
    {"isqnan",          dn_isqnan        },
    {"issnan",          dn_issnan        },
    {"isinfinite",      dn_isinf         },
................................................................................
    {"cachestats",      dn_cache_stats   },
#endif
    {NULL, NULL}
};

LUALIB_API int luaopen_ldecNumber(lua_State *L)
{


    /* create a shared environment for the decNumber functions */
    lua_createtable (L, 0, 5);
    lua_replace(L, LUA_ENVIRONINDEX);
    /* create decNumber global table and register decNumber functions */
    luaL_register (L, DN_NAME, dn_lib);
    {
        int i = 0;







>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>







 







>







 







>
>







51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
...
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
...
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
...
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
...
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
#define DN_CONTEXT_META " decNumber_CoNTeXT_MeTA"
#define DN_DNUMBER_META " decNumber_NuMBeR_MeTA"

#define DN_VERSION "Lua " DECFULLNAME " version " SVN_REVS " for " LUA_VERSION " with " DECVERSION

const char *dn_context_meta = DN_CONTEXT_META;
const char *dn_dnumber_meta = DN_DNUMBER_META;

// decNumber constants initialized when library loaded
static decNumber dnc_one;

static void dn_dnc_init (void)
{
    decContext dc;
    decContextDefault (&dc, LDN_CONTEXT_DEFAULT);
    decNumberFromString (&dnc_one, "1", &dc);
}

/* ***************** context support functions ***************** */

/*
There is one decNumber decContext per Lua thread. This library stores 
these decContexts as full userdata instances in the LUA_ENVIRONINDEX; 
the keys of this table are the thread addresses of the thread owning the
................................................................................
    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;
}

/* floor -- needs to be fudged from divideinteger */

static int dn_floor (lua_State *L)
{
    decContext *dc = ldn_get_context (L);
    decNumber *dn1 = ldn_get (L, dc, 1);
    decNumber *dn2 = ldn_get (L, dc, 2); // optional? (default to 1)
    decNumber *dnr = ldn_make_decNumber (L);
    decNumberDivideInteger (dnr, dn1, dn2, dc);
    if (decNumberIsNegative(dn1) != decNumberIsNegative(dn2))
    {
        decNumber dnm;
#if 0
        // ugh! get remainder
        decNumberRemainder (&dnm, dn1, dn2, dc);
#else
        // see if D = q * d, i.e., if r is 0
        decNumberMultiply (&dnm, dnr, dn2, dc); // m = q * d
        decNumberCompare (&dnm, &dnm, dn1, dc); // m = (m == D)
#endif
        if (!decNumberIsZero(&dnm))
        {
            // subtract one from result
            decNumberSubtract (dnr, dnr, &dnc_one, dc);
        }
    }
    return 1;
}

/* samequantum -- call needs no context */

static int dn_samequantum (lua_State *L)
................................................................................
    {"quantize",        dn_quantize      },
    {"remainder",       dn_remainder     },
    {"remaindernear",   dn_remaindernear },
    {"rescale",         dn_rescale       },
    {"samequantum",     dn_samequantum   },
    
    {"mod",             dn_mod           },
    {"floor",           dn_floor         },

    {"iszero",          dn_iszero        },
    {"isnegative",      dn_isneg         },
    {"isnan",           dn_isnan         },
    {"isqnan",          dn_isqnan        },
    {"issnan",          dn_issnan        },
    {"isinfinite",      dn_isinf         },
................................................................................
    {"quantize",        dn_quantize      },
    {"remainder",       dn_remainder     },
    {"remaindernear",   dn_remaindernear },
    {"rescale",         dn_rescale       },
    {"samequantum",     dn_samequantum   },

    {"mod",             dn_mod           },
    {"floor",           dn_floor         },

    {"iszero",          dn_iszero        },
    {"isnegative",      dn_isneg         },
    {"isnan",           dn_isnan         },
    {"isqnan",          dn_isqnan        },
    {"issnan",          dn_issnan        },
    {"isinfinite",      dn_isinf         },
................................................................................
    {"cachestats",      dn_cache_stats   },
#endif
    {NULL, NULL}
};

LUALIB_API int luaopen_ldecNumber(lua_State *L)
{
    /* initialize constants */
    dn_dnc_init ();
    /* create a shared environment for the decNumber functions */
    lua_createtable (L, 0, 5);
    lua_replace(L, LUA_ENVIRONINDEX);
    /* create decNumber global table and register decNumber functions */
    luaL_register (L, DN_NAME, dn_lib);
    {
        int i = 0;

Changes to test/ldecNumberUnitTest.lua.

339
340
341
342
343
344
345






































































346
347
348
349
350
351
352
        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
end







































































local round_funcs = lunit.TestCase("Rounding")

function round_funcs:test()
-- decNumber.ROUND_CEILING     round towards +infinity
-- decNumber.ROUND_UP          round away from 0
-- decNumber.ROUND_HALF_UP     0.5 rounds up







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
        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
end

function mod_funcs:test_floor()
    local ctx = decNumber.getcontext()
    ctx:setdefault(decNumber.INIT_DECIMAL128)
    -- explicit rounding ROUND_FLOOR
    ctx:setround(decNumber.ROUND_FLOOR)
    assert_equal (decNumber.tointegralvalue("17.50"), decNumber.tonumber "17")
    assert_equal (decNumber.tointegralvalue("-17.50"), decNumber.tonumber "-18")
    -- setup args
    local p = decNumber.tonumber "1750"
    local n = decNumber.tonumber "-1750"
    local h = decNumber.tonumber "100"
    -- divideinteger ~= floor, even with ctx:setround(decNumber.ROUND_FLOOR)
    assert_equal (p:divideinteger(h), decNumber.tonumber "17")
    assert_equal (n:divideinteger(h), decNumber.tonumber "-17")
    -- restore rounding to the default
    ctx:setround(decNumber.ROUND_HALF_EVEN)
    -- now test our floor
    assert_equal (p:floor(h), decNumber.tonumber "17")
    assert_equal (n:floor(h), decNumber.tonumber "-18")
    assert_equal (p:floor("10"), decNumber.tonumber "175")
    assert_equal (n:floor("10"), decNumber.tonumber "-175")
    -- identity tests
        for i = 1,100 do
        x = math.random(999999999)
        y = math.random(999999999)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a%b, a - b * a:floor(b))
        assert_equal ((-a)%(b), (-a) - b * (-a):floor(b))
        assert_equal (a%(-b), a + b * a:floor(-b))
        assert_equal ((-a)%(-b), (-a) + b * (-a):floor(-b))
    end
    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%b, a - b * a:floor(b))
        assert_equal ((-a)%(b), (-a) - b * (-a):floor(b))
        assert_equal (a%(-b), a + b * a:floor(-b))
        assert_equal ((-a)%(-b), (-a) + b * (-a):floor(-b))
    end
    for i = 1,200 do
        x = (-1^math.random(2)) * math.random(999999999)/2^math.random(29)
        y = (-1^math.random(2)) * math.random(999999999)/2^math.random(29)
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_equal (a%b, a - b * a:floor(b))
        assert_equal ((-a)%(b), (-a) - b * (-a):floor(b))
        assert_equal (a%(-b), a + b * a:floor(-b))
        assert_equal ((-a)%(-b), (-a) + b * (-a):floor(-b))
    end
    for x,y in pairs {[0]=1, [1]=0} do
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_nequal (a%b, a - b * a:floor(b))
        assert_nequal ((-a)%(b), (-a) - b * (-a):floor(b))
        assert_nequal (a%(-b), a + b * a:floor(-b))
        assert_nequal ((-a)%(-b), (-a) + b * (-a):floor(-b))
    end
    for x,y in pairs {[0]=0, [1]=1} do
        a = decNumber.tonumber(x)
        b = decNumber.tonumber(y)
        assert_nequal (a%b, a - b * a:floor(b))
        assert_nequal ((-a)%(b), (-a) - b * (-a):floor(b))
        assert_nequal (a%(-b), a + b * a:floor(-b))
        assert_nequal ((-a)%(-b), (-a) + b * (-a):floor(-b))
    end
end

local round_funcs = lunit.TestCase("Rounding")

function round_funcs:test()
-- decNumber.ROUND_CEILING     round towards +infinity
-- decNumber.ROUND_UP          round away from 0
-- decNumber.ROUND_HALF_UP     0.5 rounds up