| ︙ | | |
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
+
+
+
+
+
+
+
+
+
+
|
#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
|
| ︙ | | |
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
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
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
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)
|
| ︙ | | |
629
630
631
632
633
634
635
636
637
638
639
640
641
642
|
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
|
+
|
{"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 },
|
| ︙ | | |
721
722
723
724
725
726
727
728
729
730
731
732
733
734
|
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
|
+
|
{"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 },
|
| ︙ | | |
745
746
747
748
749
750
751
752
753
754
755
756
757
758
|
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
+
+
|
{"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;
|
| ︙ | | |