Skip to content
Astera Technology

Why does forcing an integer counter to REAL lose one tick?

Keep exact integer accumulation within its declared range.

Download complete case notes and answer keys ↓

Prepare the report input

Original extreme fixture adds integer 9007199254740992 and one, well within signed 64-bit total range. Multiplying by 1.0 forces REAL before summing and loses the one. Keep the integer SUM; cast the finished value to text for this large-value transport.

Every record and report rule below is original and fictional. Reproduce the setup in a fresh SQLite test database; input previews include every table in this small fixture.

Synthetic input: counters
idticks
A9007199254740992
B1
Download editable CSV ↓
Complete CREATE and INSERT fixture
CREATE TABLE counters(id TEXT PRIMARY KEY,ticks INTEGER NOT NULL); INSERT INTO counters VALUES('A',9007199254740992),('B',1);

Compare misleading and corrected results

Misleading SELECT: reproduce the defect
SELECT CAST(CAST(SUM(1.0*ticks) AS INTEGER) AS TEXT) AS total_ticks FROM counters;
Literal misleading result
total_ticks
9007199254740992
Download editable CSV ↓
Corrected SELECT
SELECT CAST(SUM(ticks) AS TEXT) AS total_ticks FROM counters;
Literal corrected result
total_ticks
9007199254740993
Download editable CSV ↓

Bad text total ends in 992; correct total ends in 993. The input preview also casts ticks to text so a JavaScript number conversion cannot blur the displayed integer.

Check a fresh boundary case

Add integer −1. The corrected total returns to 9007199254740992.

Recreate the original setup in another fresh database, then apply the change and corrected query below. For an empty result, the table displays a labelled empty-result marker rather than a data record.

Boundary change and corrected query
INSERT INTO counters VALUES('C',-1);
SELECT CAST(SUM(ticks) AS TEXT) AS total_ticks FROM counters;
Literal boundary result
total_ticks
9007199254740992
Download editable CSV ↓

What exact base sum should be transported?

Show the worked answer

The text "9007199254740993". Integer arithmetic is exact for this bounded sum; real conversion is the source of loss. Larger sums need an independently tested overflow policy.

Related resources

System integration services →