Unit conversion can turn nonzero values into zero, result in divide-by-zero errors

Aperta
#400 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript
Ambito
backend

Direzione di ricerca

Start with the comparison in src/util/units.ts at the linked lines, then find the tests for unit conversion and Divide, TruncatedDivide, and Modulo. Run the relevant tests and add coverage for the negative and positive examples in the issue. Done means the negative divisor produces the stated result without breaking the positive case or related operations.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

There is an issue with the unit conversion process that turns small negative values into zero, which turns the overall expression into a divide-by-zero error / null.

For instance:

define "DivBySmallNegative": 1 'm' div -0.00000001 'cm'

this currently throws, but the correct result should be -10000000000 '1'. The issue is specifically with negative numbers, since this returns the expected positive value correctly:

define "DivBySmallPositive": 1 'm' div 0.00000001 'cm'    // 10000000000 '1'

The sequence that causes the issue:

  1. Normalization first tries converting the divisor to m.
    -0.00000001 cm → -0.0000000001 m

  2. Unit conversion rounds that value to zero.
    convertUnit calls normalized(), which limits the decimal scale to eight places:

    -0.0000000001 → 0.00000000

  3. This new value is returned based on a comparison that doesn't work correctly for negative values:
    https://github.com/cqframework/cql-execution/blob/e8096c64ae8da45fe9a358bbf711bc36d749b696/src/util/units.ts#L132-L134
    newVal2.greaterThanOrEquals(val2)

    Here that becomes:

    0 >= -0.00000001 → true

    Consequently, it keeps meters and returns these normalized operands:

    dividend: 1 m
    divisor: 0 m

Note: I think that comparison newVal2.greaterThanOrEquals(val2) can also produce unexpected results for any negative divisor, but the impacts are most visible for these small negative numbers. The easiest fix might just be to do newVal2.abs().greaterThanOrEquals(val2.abs()) but I haven't fully tested that

This is relevant to Divide, TruncatedDivide, and Modulo.

Lingua principale
JavaScript
Stelle
84
Fork
41
Merge medio
5g 4h
PR unite (30g)
3

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cqframework/cql-execution

Tutte le issue di cqframework/cql-execution

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.