Skip to content

Boogie v3.5.8

Latest

Choose a tag to compare

@github-actions github-actions released this 03 Oct 19:05
· 10 commits to master since this release
fcecf73
fix: Parse negative `BigDec`s correctly (#1152)

## The bug

`BigDec.FromString` keeps the sign only in the integral part it parses,
then asks *that value* whether the number is negative (`BigDec.cs:98` on
master):

```csharp
integral = BIM.Parse(v.Substring(0, len));
...
if (integral.Sign == -1) { return new BigDec(integral - fraction, exponent); }
else                     { return new BigDec(integral + fraction, exponent); }
```

For a magnitude below one the integral part is `"-0"`, and `BigInteger`
has no negative zero — so `BIM.Parse("-0")` gives `Sign == 0`, the test
misses, the fraction is **added** instead of subtracted, and the sign is
gone:

```
BigDec.FromString("-0.5")     ->  mantissa 5,   exponent -1   (+0.5)
BigDec.FromString("-0.0625")  ->  mantissa 625, exponent -4   (+0.0625)
```

The same values spelled so the integral part is not zero were always
right, which is why this survived: `"-5e-1"` *is* the `"-0.5"` above,
and `FloorAndCeil` has covered it since its case list was written. So
was `"-1.5"`, which `FromStringNegative` pins — and that test records an
earlier fix "where BigDec would not parse strings with negative numbers
correctly", which was incomplete rather than wrong.

A sweep over 3168 accepted spellings (sign × integral part × fraction ×
exponent, with zero-padding and scaling), checked against an independent
exact parse: **324 wrong before, 0 after.** Every failure had a zero
integral part.

## Reachability

No Boogie program reaches this. `Parser.cs:2485` is the only caller, and
both numeric tokens it can pass begin with a digit (`BoogiePL.atg`):

```
decimal   = digit {digit} 'e' [ '-' ] digit {digit} .
dec_float = digit {digit} '.' digit {digit} [ 'e' [ '-' ] digit {digit} ] .
```

A sign is allowed only inside the exponent, and unary minus is a
separate expression. So nothing changes in what the parser builds or
what a prover is sent — the defect is in the public API, which
downstream consumers reach.

## The change

Read the sign from the text: once the integral part has parsed, a `-` in
it can only be that sign. Two lines, plus nine `TestCase` rows. The test
fails on exactly the seven buggy spellings and passes on the two that
already worked, so it pins the bug rather than the implementation.

## Left alone

- `BigDec` still cannot represent −0: `FromString("-0")` gives `+0`.
That is inherent to a mantissa carrying the sign, not part of this bug.
- `"-.5"` throws `FormatException`, because the integral part is a bare
`-`. The grammar above does not allow that spelling either.

---

Independent of #1151 (no shared files); either order works.

Co-authored-by: Shaz Qadeer <shaz.qadeer@gmail.com>