1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 07:29:33 +00:00

Fix broken/incomplete floating point parsing

- Fractional digit scale was broken
- Base was partially ignored
- Exponent sign was ignored
- Exponent for hex float is 2 not 10
This commit is contained in:
bbbradsmith 2023-05-02 23:49:40 -04:00
parent 805e98a7aa
commit a109f475ed

View File

@ -687,20 +687,21 @@ static void NumericConst (void)
/* Check for a fractional part and read it */
if (SB_Peek (&Src) == '.') {
Double Scale;
Double Scale, ScaleDigit;
/* Skip the dot */
SB_Skip (&Src);
/* Read fractional digits */
Scale = FP_D_Make (1.0);
ScaleDigit = FP_D_Div (FP_D_Make (1.0), FP_D_FromInt (Base));
Scale = ScaleDigit;
while (IsXDigit (SB_Peek (&Src)) && (DigitVal = HexVal (SB_Peek (&Src))) < Base) {
/* Get the value of this digit */
Double FracVal = FP_D_Div (FP_D_FromInt (DigitVal * Base), Scale);
Double FracVal = FP_D_Mul (FP_D_FromInt (DigitVal), Scale);
/* Add it to the float value */
FVal = FP_D_Add (FVal, FracVal);
/* Scale base */
Scale = FP_D_Mul (Scale, FP_D_FromInt (DigitVal));
/* Adjust Scale for next digit */
Scale = FP_D_Mul (Scale, ScaleDigit);
/* Skip the digit */
SB_Skip (&Src);
}
@ -712,12 +713,15 @@ static void NumericConst (void)
unsigned Digits;
unsigned Exp;
int Sign;
/* Skip the exponent notifier */
SB_Skip (&Src);
/* Read an optional sign */
Sign = 0;
if (SB_Peek (&Src) == '-') {
Sign = 1;
SB_Skip (&Src);
} else if (SB_Peek (&Src) == '+') {
SB_Skip (&Src);
@ -747,9 +751,11 @@ static void NumericConst (void)
Warning ("Floating constant exponent is too large");
}
/* Scale the exponent and adjust the value accordingly */
/* Scale the exponent and adjust the value accordingly.
** Decimal exponents are base 10, hexadecimal exponents are base 2 (C99).
*/
if (Exp) {
FVal = FP_D_Mul (FVal, FP_D_Make (pow (10, Exp)));
FVal = FP_D_Mul (FVal, FP_D_Make (pow ((Base == 16) ? 2.0 : 10.0, (Sign ? -1.0 : 1.0) * Exp)));
}
}