Basic approximations in Python

| | | | | | | | | | | |

👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!

Approximation means estimating the value of something that is not quite accurate, but almost correct. It plays a vital role in science and technology. Let’s start with the most common example. Have you ever used the exact pi value? Of course not. It is an infinite irrational number with a very long meaning. If we continue to write the exact value of Pi , perhaps even this article will not be enough for this:

 3.14159 26535 89793 23846 26433 83279 ... 

So here where approximation plays a role. We usually approximate Pi as 3.14 or in rational terms 22/7 . When you got to high school, you probably saw a wider application of approximations in mathematics, which use differentials to approximate the values ‚Äã‚Äãof quantities, such as (36.6) ^ 1/2 or (0.009) ^ 1/3. In computer science, we can use approximation to find the value or approximate the value of something using loops.

For example: approximation of the cube root of any number. Take a look at the process below:

# Python program to approximate
# cube root of 27

guess = 0.0

cube = 27

increment = 0.0001

epsilon = 0.1


# Find an approximate value

while abs (guess * * 3 - cube) & gt; = epsilon:

guess + = increment


# Check approximate value

if abs (guess * * 3 - cube) & gt; = epsilon:

print ( "Failed on the cube root of" , cube)

else :

print (guess, " is close to the cube root of " , cube)

Output of the above code:

 2.9963000000018987 is close to the cube root of 27 

As we can see, 2.99 is not the exact value of (27) ^ 1/3 but is very close to the exact value 3. This is what we call approximation. Here we have used a series of calculations to approximate the value. First, we declare a variable guess = 0.0 which we will keep incrementing in a loop until it approaches the cube root of 27. Another variable epsilon is chosen as little as possible to get more accurate meaning. The line while abs (guess ** 3 - cube) & gt; = epsilon: will take care of this. If it breaks out of the loop with a value greater than epsilon , it means that we have already crossed the approximate value and failed in the test. Otherwise, it will return the guess value.

👻 Read also: what is the best laptop for engineering students?

We hope this article has helped you to resolve the problem. Apart from Basic approximations in Python, check other code Python module-related topics.

Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.

By the way, this material is also available in other languages:



Javier Williams

California | 2023-03-26

Simply put and clear. Thank you for sharing. Basic approximations in Python and other issues with io Python module was always my weak point 😁. Will get back tomorrow with feedback

Carlo Galleotti

Berlin | 2023-03-26

Maybe there are another answers? What Basic approximations in Python exactly means?. I just hope that will not emerge anymore

Marie Lehnman

Shanghai | 2023-03-26

re Python module is always a bit confusing 😭 Basic approximations in Python is not the only problem I encountered. I am just not quite sure it is the best method

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically