A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)

Round 2.675 to 3 decimal places

According to python3, this answer seems to be ** 2.67 **. Hmm, stupid. ..

When I thought about it, there were many other examples like this.

test.py



2.2 + 2.1 #4.300000000000001

7.6 + 8.7 #16.299999999999997

The reason is that there is C language in the base of python

It seems that python is written in C language, and as a result, it seems that such an unclear calculation result is returned.

The procedure on the back side when calculating float is like this.

  1. Convert python Float to Fraction in C
  2. Calculation
  3. Convert the fractional value to Float
  4. Display results with python

So, there seems to be a slight error in the timing of conversion from fractions to decimals.

I had the option of doubting this if something strange happened around the numbers.

I wonder if I'm addicted to a bug in a system I made without knowing this lol

Recommended Posts

A simple reason why the return value of round (2.675,2) is 2.67 in python (it should be 2.68 in reality ...)
Delete a particular character in Python if it is the last
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
[python] [meta] Is the type of python a type?
Get the caller of a function in Python
Make a copy of the list in Python
Find the divisor of the value entered in python
Output in the form of a python array
[Python] Summary of functions that return the index that takes the closest value in the array
In Python, change the behavior of the method depending on how it is called
Check if the string is a number in python
[Python Data Frame] When the value is empty, fill it with the value of another column.
Get the value of a specific key in a list from the dictionary type in the list with Python
The result of making a map album of Italy honeymoon in Python and sharing it
How to check in Python if one of the elements of a list is in another list
A reminder about the implementation of recommendations in Python
I thought about why Python self is necessary with the feeling of a Python interpreter
A simple cache of values in the property decorator. Read only. Note that it keeps caching until the object is deleted.
The value of meta when specifying a function with no return value in Dask dataframe apply
Get the value of a specific key up to the specified index in the dictionary list in Python
What seems to be a template of the standard input part of the competition pro in python3
I tried to create a Python script to get the value of a cell in Microsoft Excel
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
Find out the apparent width of a string in python
Get the number of specific elements in a python list
Master the type in Python? (When should type check be done)
[Note] Import of a file in the parent directory in Python
How to get the last (last) value in a list in Python
Find the eigenvalues of a real symmetric matrix in Python
Play a sound in Python assuming that the keyboard is a piano keyboard
Be careful when specifying the default argument value in Python3 series
How to determine the existence of a selenium element in Python
A simple data analysis of Bitcoin provided by CoinMetrics in Python
[Python] Execution time when a function is entered in a dictionary value
Read the standard output of a subprocess line by line in Python
How to check the memory size of a dictionary in Python
A function that measures the processing time of a method in python
[Python] A simple function to find the center coordinates of a circle
[Python / Jupyter] Translate the comment of the program copied to the clipboard and insert it in a new cell
About the return value of pthread_mutex_init ()
About the return value of the histogram.
Implementing a simple algorithm in Python 2
Run a simple algorithm in Python
How to input a character string in Python and output it as it is or in the opposite direction.
[CleanArchitecture with Python] Apply CleanArchitecture step by step to a simple API and try to understand "what kind of change is strong" in the code base.
Get a capture of the entire web page in Selenium Python VBA
[Python] What is a formal argument? How to set the initial value
What is the XX file at the root of a popular Python project?
If you want a singleton in python, think of the module as a singleton
[Python] Sweet Is it sweet? About suites and expressions in the official documentation
[Python] A program that calculates the number of socks to be paired
Check the in-memory bytes of a floating point number float in Python
Test & Debug Tips: Create a file of the specified size in Python
Receive a list of the results of parallel processing in Python with starmap
What kind of book is the best-selling "Python Crash Course" in the world?
Find out the name of the method that called it from the method that is python
Get a datetime instance at any time of the day in Python
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
Get the return value of an external shell script (ls) with python3