CS FundamentalsModerateTCS NQT · Infosys pattern
A column `price NUMERIC` (arbitrary precision decimal) is compared against a literal using `price = 19.99`. Why is this generally SAFE from the classic floating-point equality problem that `price = 19.99` would risk if price were a `FLOAT`/`DOUBLE` column instead?
- A. NUMERIC and FLOAT behave identically in every database
- B. NUMERIC/DECIMAL types store an exact base-10 representation (not a binary floating-point approximation), so a value like 19.99 is stored exactly, unlike FLOAT/DOUBLE, which can only approximate most decimal fractions in binary, making equality comparisons on them unreliable
- C. FLOAT columns cannot be compared with = at all
- D. NUMERIC columns automatically round to the nearest cent
Correct answer: B. NUMERIC/DECIMAL types store an exact base-10 representation (not a binary floating-point approximation), so a value like 19.99 is stored exactly, unlike FLOAT/DOUBLE, which can only approximate most decimal fractions in binary, making equality comparisons on them unreliable
IEEE floating-point (FLOAT/DOUBLE) represents numbers in binary, and many ordinary decimal fractions (like 0.1 or 19.99) have no exact finite binary representation, leading to tiny rounding errors that break naive equality checks. NUMERIC/DECIMAL types are defined to store an exact decimal value (often as scaled integers internally), so a stored 19.99 really is exactly 19.99, and comparing it for equality against the literal 19.99 is reliable.
More DBMS and SQL questions
- A relation R(A, B, C, D) has candidate key ABD (the ONLY candidate key) and FD AB → C (C is non-prime). Which normal form violation does…
- A query optimizer must decide between two equivalent plans for A JOIN B: (1) nested-loop join using an index on B, with A as the outer…
- A relation R(A, B, C, D, E) has FDs AB → C, C → DE. Which of the following is the candidate key, and how many non-prime attributes does R…
- Which pair of properties must every primary key satisfy?
- Which ACID property guarantees that a transaction's changes are applied completely or not at all?
- Which of these SQL commands is a DDL (Data Definition Language) command?