Round-off Errors — Python Numerical Methods (2024)

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

In the previous section, we talked about how the floating point numbers are represented in computers as base 2 fractions. This has a side effect that the floating point numbers can not be stored with perfect precision, instead the numbers are approximated by finite number of bytes. Therefore, the difference between an approximation of a number used in computation and its correct (true) value is called round-off error. It is one of the common errors usually in the numerical calculations. The other one is truncation error which we will introduce in Chapter 18. The difference is that truncation error is the error made by truncating an infinite sum and approximating it by a finite sum.

Representation error

The most common form round-off error is the representation error in the floating point numbers. A simple example will be to represent \(\pi\). We know that \(\pi\) is an infinite number, but when we use it, we usually only use a finite digits. For example, if you only use 3.14159265, there will be an error between this approximation and the true infinite number. Another example will be 1/3, the true value will be 0.333333333…, no matter how many decimal digits we choose, there is an round-off error as well.

Besides, when we rounding the numbers multiple times, the error will accumulate. For instance, if 4.845 is rounded to two decimal places, it is 4.85. Then if we round it again to one decimal place, it is 4.9, the total error will be 0.55. But if we only round one time to one decimal place, it is 4.8, which the error is 0.045.

Round-off error by floating-point arithmetic

From the above example, the error between 4.845 and 4.8 should be 0.055. But if you calculate it in Python, you will see the 4.9 - 4.845 is not equal to 0.055.

4.9 - 4.845 == 0.055
False

Why does this happen? If we have a look of 4.9 - 4.845, we can see that, we actually get 0.055000000000000604 instead. This is because the floating point can not be represented by the exact number, it is just approximation, and when it is used in arithmetic, it is causing a small error.

4.8 - 4.845
-0.04499999999999993

Another example shows below that 0.1 + 0.2 + 0.3 is not equal 0.6, which has the same cause.

0.1 + 0.2 + 0.3 == 0.6
False

Though the numbers cannot be made closer to their intended exact values, the round function can be useful for post-rounding so that results with inexact values become comparable to one another:

round(0.1 + 0.2 + 0.3, 5) == round(0.6, 5)
True

Accumulation of round-off error

When we are doing a sequence of calculations on an initial input with round-off error due to inexact representation, the errors can be magnified or accumulated. The following is an example, that we have the number 1 add and subtract 1/3, which gives us the same number 1. But what if we adding 1/3 for many times and subtract the same number of times 1/3, do we still get the same number 1? No, you can see the example below, the more times you doing this, the more errors you are accumulating.

# If we only do once1 + 1/3 - 1/3
1.0
def add_and_subtract(iterations): result = 1 for i in range(iterations): result += 1/3 for i in range(iterations): result -= 1/3 return result
# If we do this 100 timesadd_and_subtract(100)
1.0000000000000002
# If we do this 1000 timesadd_and_subtract(1000)
1.0000000000000064
# If we do this 10000 timesadd_and_subtract(10000)
1.0000000000001166

< 9.2 Floating Point Numbers | Contents | 9.4 Summary and Problems >

Round-off Errors — Python Numerical Methods (2024)
Top Articles
RFID Credit Cards: Should You Worry About Protection? | Bankrate
Outstanding Balance: What It Means and How It Affects You? | IIFL Finance
Lairson Enterprises Rv Sales
Whispering Oaks In Battle Creek Michigan
The 10 Craigslist Guys You’ll Live With in DC
Walmart Takes on Abercrombie with Relaunch of No Boundaries
Argyle Skyward
Mccommons Funeral Home Obituaries
Central Craigslist Pets
Rblxwild Affiliate Codes
Tj Maxx Sugarhouse
That Is No Sword Tanjiro X Kakushi
Hca Gulf Coast Scheduler
Njb Tinder
Skyrim Isabelle
Argus911
Evangelist buys Tyler Perry's mansion for $17.5million
Haul auf deutsch: Was ist das? Übersetzung, Bedeutung, Erklärung - Bedeutung Online
What Is The Value Of X 105O 115O 125O 135O
Tw's Bait And Tackle Fishing Report
Suoiresnu Kemono Party
90 Days From February 28
Ics 200 Answers
Burley Id Recent Bookings
Mike Temara
Weld County Sheriff Daily Arrests
Www Craigslist Com Corpus Christi
Oral-B iO 8N Elektrische Tandenborstel Zwart
Norton Immediate Care Brownsboro
Vip Market Vetsource
Marcus Roberts 1040 Answers
Dan Pfeiffer Message Box
Second Chance Maryland Lottery
Studentvue Stockton Ca
Results, presentations and reports
H12 Weidian
Www. Lowe's.com
Bedford Barbers Nyc
Milwaukee County Tickets
Ame Bibabi Net Worth
Gina Wilson All Things Algebra Unit 2 Homework 8
H&R Block Review 2024: Pricing, Features, Ease of Use - NerdWallet
Harris Teeter Path
Devotion Showtimes Near O'neil Cinemas - Brickyard Square 12
Trivago Walt Disney World
Iowa State Map Campus
Basketball Stars Unblocked Games Premium
Penn State E Wall
Creepshotorg
Number of Kwik Trip Stores Location in the USA - 2024 | LocationsCloud
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5563

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.