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
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Database Programming.
Lesson complete
Earn the 3 college credits for Database Programming
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Working with BOOLEAN Values in SQL →← 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 →
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.
Enroll now → Course details Download the syllabus All Computer Science courses Every credit course with PRO →
Why take it here
Why students take Database Programming with UPI Study
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.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →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?
What is the difference between FLOAT and DOUBLE in MySQL and SQL Server?
How should you compare floating-point values in a WHERE clause?
When should you avoid FLOAT or DOUBLE in a database?
About the Database Programming course
How many college credits is Database Programming?
How much does Database Programming cost and what is included?
Which universities accept UPI Study credits?
How long does Database Programming take?
How are the exams done?
How do my credits reach my college?
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
3 credits · NCCRSEnroll →