1
0
mirror of https://github.com/janet-lang/janet synced 2025-04-15 23:33:17 +00:00

Add hexfloats - Address #1550

This commit is contained in:
Calvin Rose 2025-01-30 07:36:18 -06:00
parent fa75a395cb
commit f63a33884f
2 changed files with 13 additions and 2 deletions

View File

@ -298,8 +298,10 @@ int janet_scan_number_base(
}
/* If still base is 0, set to default (10) */
int exp_base = base;
if (base == 0) {
base = 10;
exp_base = 10;
}
/* Skip leading zeros */
@ -322,6 +324,12 @@ int janet_scan_number_base(
} else if (*str == '&') {
foundexp = 1;
break;
} else if (base == 16 && (*str == 'P' || *str == 'p')) { /* IEEE hex float */
foundexp = 1;
exp_base = 10;
base = 2;
ex *= 4; /* We need to correct the current exponent after we change the base */
break;
} else if (base == 10 && (*str == 'E' || *str == 'e')) {
foundexp = 1;
break;
@ -360,9 +368,9 @@ int janet_scan_number_base(
}
while (str < end) {
int digit = digit_lookup[*str & 0x7F];
if (*str > 127 || digit >= base) goto error;
if (*str > 127 || digit >= exp_base) goto error;
if (ee < (INT32_MAX / 40)) {
ee = base * ee + digit;
ee = exp_base * ee + digit;
}
str++;
seenadigit = 1;

View File

@ -208,5 +208,8 @@
(parser/consume p `")`)
(assert (= (parser/produce p) ["hello"]))
# Hex floats
(assert (= math/pi +0x1.921fb54442d18p+0001))
(end-suite)