Skip to content

Commit 01676b2

Browse files
committed
add inf/nan parsing
1 parent 52bc0e9 commit 01676b2

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

source/mir/bignum/decimal.d

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ bool parseDecimal(size_t maxSize64, C)(scope const(C)[] str, ref Decimal!maxSize
110110
}
111111
else
112112
{
113+
if (str.length == 2)
114+
{
115+
if ((d == 'i' - '0') & (cast(C[2])str[0 .. 2] == cast(C[2])"nf"))
116+
{
117+
key = DecimalExponentKey.infinity;
118+
return true;
119+
}
120+
if ((d == 'n' - '0') & (cast(C[2])str[0 .. 2] == cast(C[2])"an"))
121+
{
122+
key = DecimalExponentKey.nan;
123+
return true;
124+
}
125+
}
113126
return false;
114127
}
115128

@@ -194,6 +207,10 @@ enum DecimalExponentKey
194207
D = 'D' - '0',
195208
///
196209
E = 'E' - '0',
210+
///
211+
infinity,
212+
///
213+
nan,
197214
}
198215

199216
///
@@ -228,6 +245,15 @@ unittest
228245
assert(cast(double) decimal.view == 0);
229246
assert(key == DecimalExponentKey.E);
230247

248+
assert("-nan".parseDecimal(decimal, key));
249+
assert(cast(double) decimal.view == 0);
250+
assert(decimal.coefficient.sign);
251+
assert(key == DecimalExponentKey.nan);
252+
253+
assert("inf".parseDecimal(decimal, key));
254+
assert(cast(double) decimal.view == 0);
255+
assert(key == DecimalExponentKey.infinity);
256+
231257
assert(!"3.3.4".parseDecimal(decimal, key));
232258
assert(!"3.4.".parseDecimal(decimal, key));
233259
assert(!"4.".parseDecimal(decimal, key));

0 commit comments

Comments
 (0)