Floating-Point Data Types: REAL, FLOAT, and DOUBLE, Explained

UPI Study › Courses › Database Programming › Free lessons › Lesson 4.2 Free with a free account

Floating-Point Data Types: REAL, FLOAT, and DOUBLE

Database Programming · Chapter 4: SQL Data Types in DepthSection 4.2 · lesson 21 of 769 min · free with a free account

← Precision Number Types: DECIMAL and NUMERICNext lesson: Working with BOOLEAN Values in SQL →

A free account plays all of Chapter 1 and 3 more lessons of Database Programming. Create a free account →

Earn 3 college credits for Database ProgrammingRecommended for credit by NCCRS · Computer Science 381 · self-paced · one payment, lifetime access

In this lesson

How REAL, FLOAT, and DOUBLE store approximate binary values.

  • Describe the sign, exponent, and fraction parts of IEEE 754 single and double precision values
  • Compare what REAL, FLOAT, and DOUBLE mean in MySQL, SQL Server, and SQLite
  • Explain why decimal fractions such as 0.1 cannot be stored exactly in binary
  • Apply a tolerance test instead of exact equality when comparing measured values

Free study notes

Get this lesson's notes by email

The key points and 4 practice questions with answers, in your inbox. One email, free.

One email. Unsubscribe in one click.

Transcript

Everything said in the video, section by section (1,032 words). Read along, or search it.

Opening Question

Why 0.1 Plus 0.2 Fails Equality

Leo Park, the developer at Lantern Books, sees a sum displayed as zero point three. Yet his equality test rejects it. This SQLite query adds one tenth and two tenths, then compares the sum with three tenths. Run it, and compare the displayed sum with the separate equality result. The sum looks familiar, but the equality result is zero, meaning false. The calculation and comparison use stored binary approximations. That is the central floating-point surprise: a short decimal display can hide a difference that still matters to an operator. We need to understand the representation, choose the appropriate type, and compare measured values according to their actual accuracy.

Definition

Sign, Exponent, and Fraction Bits

Floating point resembles scientific notation: significant digits multiplied by a power of the base. The point can move as the exponent changes. The usual binary formats follow the standard called I E E E seven fifty-four. Each value has a sign, an exponent, and a fraction field. Single precision uses thirty-two bits: one for sign, eight for exponent, and twenty-three stored fraction bits. Double precision uses sixty-four bits: one for sign, eleven for exponent, and fifty-two stored fraction bits. That gives roughly seven significant decimal digits for single precision, and fifteen for double. These are significant digits, not fixed decimal places. Their maximum magnitudes are roughly three point four times ten to the thirty-eight, and one point eight times ten to the three hundred eight. More exponent capacity gives range; more significand capacity gives detail. Double precision improves both, but it still cannot represent every decimal fraction.

Dialect Comparison

REAL, FLOAT, and DOUBLE Differ by System

The names real, float, and double are not interchangeable across database systems. Translate the required precision, rather than copying a familiar name. MySQL's plain float is single precision, while double is double precision. Real normally aliases double, unless the real as float mode changes that choice. SQL Server uses real, also called float twenty-four, for single precision. Plain float defaults to float fifty-three, its double precision format. SQL Server also recognizes double precision as a synonym, but not the lone word double. These declarations are labeled non-run comparisons. SQLite's real storage class uses binary sixty-four. Adding a precision argument here, does not create a single precision storage format. Choose the format deliberately, and record the target dialect in migrations. A type name that parses can still describe a different storage decision.

Binary Fractions

Halves Are Exact but Tenths Are Not

Why can one half be exact while one tenth is not? Binary fractions terminate at halves, quarters, eighths, and further powers of two. After reducing a fraction, its denominator must be a power of two for finite binary representation. It must also fit the format's available capacity. Run this comparison, using SQLite's extended numeric formatter to expose more digits than the normal display. The halves and quarters compare exactly, while the two long decimal strings reveal different stored values for the sum and its target. Formatting changes what Leo can read, not the stored numbers. Rounding a screen display to a few places does not repair a comparison. Think of representable values as marks on a ruler. A value between marks is stored at a nearby mark; real formats have very fine spacing.

Comparison Technique

Comparing Measurements With a Tolerance

For approximate measurements, replace accidental exact equality with a question about acceptable error. Absolute difference measures how far two values are apart. The absolute value function removes the sign. Compare that distance with a tolerance, a small allowed difference chosen for the application. Run this test with an illustrative tolerance of one millionth. It is a demonstration threshold, not a universal recommendation. The difference is tiny, and the close-enough test returns one. Both values fall within the chosen allowance. A sensor's units, calibration, and decision boundaries should guide its tolerance. Values across very different magnitudes may need relative error limits too. Less-than and greater-than comparisons can also be sensitive near a boundary. Decide whether the possible approximation could change the business decision.

Accumulated Error

Ten Tenths Fall Slightly Below One

Another risk appears when many small errors accumulate. This recursive demonstration adds one tenth repeatedly, starting at zero. You do not need to master recursive queries yet. Follow the repeated step: increase the counter, then add the same approximate value. Run the file after ten additions. Mathematically, ten tenths should make exactly one. The detailed total is slightly below one. Every addition uses the finite representation, so repeated arithmetic can introduce further rounding. Larger totals and different addition orders can produce different final errors. Neither a particular error direction nor a fixed accumulation rate is guaranteed. Use decimal arithmetic for an exact financial total, or a suitable numerical method for scientific work. Printing fewer digits only conceals the discrepancy.

Large Values

Gaps Between Large Floating-Point Values

The ruler's marks also spread apart as numbers grow. A large floating-point value cannot necessarily respond to a small increment. Single precision represents every integer through two to the twenty-four. Beyond that boundary, some consecutive integers are unavailable. Run the corresponding double precision experiment, at two to the fifty-three. Add one, then compare with the starting value. The result is one: the rounded sum is unchanged. The required intermediate integer falls between available representable values. This is why identifiers, stock counts, and exact counters belong in integer types with adequate range. Extra display digits cannot create missing capacity. For an approximate measurement, double precision is often appropriate. For a count, choosing floating point changes the meaning of accuracy in an unacceptable way.

Quick Check

A Calibration Reading Within One Hundredth

A calibration accepts readings within one hundredth of its target. A new reading is twelve point zero zero seven, against a target of twelve. The query uses a strict less-than comparison. What value will it return? Pause and predict. It returns one, so this reading passes the stated tolerance test. Run the query: the absolute difference is about seven thousandths, which is below one hundredth. Zero would mean the test failed, while null would indicate a missing or unknown result. Neither applies to these supplied numeric values.

Key Takeaways

Match the Numeric Type to the Purpose

Use integers for exact whole values, decimal types for exact decimal amounts, and floating point when its error fits the measurement. Recognize that formatting and extra precision cannot make every decimal exact. Check dialect names, magnitudes, and accumulation before choosing storage. For Leo's comparison, the right fix follows the purpose: a justified tolerance for measurements, or exact arithmetic when agreement must be exact.

About Database Programming at UPI Study

Database Programming (Computer Science 381) is a self-paced online course of 76 video lessons in 12 chapters, from database applications, SQL setup, schema planning and normalization to keys and constraints, data types, filtering and sorting, joins, subqueries, views, indexes, ROLLUP and PIVOT, regular expressions and database security. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Compare: Database Fundamentals vs Database Programming

Why take it here

Why students take Database Programming with UPI Study

Write the SQL behind every appDatabase Programming teaches SQL in MySQL and SQL Server side by side: designing and normalizing tables, keys and constraints, data types, inserting, updating and deleting rows, filtering, grouping, joins, subqueries, views, indexes, transactions, and protecting data from SQL injection. It suits computer science and information technology degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

Database Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Floating-Point Data Types: REAL, FLOAT, and DOUBLE

Why does a floating-point sum of 0.1 and 0.2 not equal 0.3?
A floating-point sum of 0.1 and 0.2 differs from 0.3 because none of these decimal fractions has an exact binary representation. Each is stored as a nearby binary value, and the rounded sum lands on a slightly different value than the stored 0.3, so an equality test returns false even when the display shows 0.3. SQLite treats such literals as REAL, while MySQL and SQL Server treat 0.1 as an exact decimal literal.
What is the difference between FLOAT and DOUBLE in MySQL and SQL Server?
In MySQL, FLOAT is a 4-byte single-precision type and DOUBLE is an 8-byte double-precision type, and REAL normally means DOUBLE. SQL Server differs: REAL is single precision, equal to FLOAT(24), while plain FLOAT defaults to FLOAT(53), which is double precision. SQL Server accepts DOUBLE PRECISION but not DOUBLE alone. Single precision gives roughly 7 significant digits and double roughly 15, so translate the precision rather than copying a type name.
How should you compare floating-point values in a WHERE clause?
Compare floating-point values with a tolerance instead of exact equality, by testing whether their absolute difference is below an allowed error. For example, WHERE ABS(reading - 12.0) < 0.01 accepts a reading of 12.007 because the difference is about 0.007. Choose the tolerance from the measurement's units, calibration, and decision rules, and consider relative limits when values vary widely in size. Comparisons near a boundary can also be sensitive.
When should you avoid FLOAT or DOUBLE in a database?
Avoid floating-point types for money, counts, identifiers, and other values that must be exact. Repeated arithmetic accumulates rounding error, so adding 0.1 ten times gives a total slightly below 1. Large values also lose detail: in double precision, adding 1 to 2 to the 53rd power leaves the value unchanged. Use DECIMAL for exact decimal amounts, integers for whole counts, and floating point for measurements whose error is acceptable.

About the Database Programming course

How many college credits is Database Programming?
Database Programming (Computer Science 381) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does Database Programming cost and what is included?
$250, paid once, with lifetime access: all 76 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does Database Programming take?
It is self-paced with no deadlines. The 76 lessons total about 11 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in Database Programming.

76 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

Updated

Database Programming
3 credits · NCCRS
Enroll →