Python hand play (division)

What is this article?

I thought I'd post just one word and wrote it. In a nutshell, if you want an integer solution for division, use "//" or "divmod".

Case study

Divide the number by 100 to get an integer.

Code before modification

result = int(value / 100)

Well, in most cases this works. However, it is NG with a large number.

Case of NG

>>> 38030123828366199/100
380301238283662.0

It's easy to understand.

Modified code

result = value // 100

OK case

>>> 38030123828366199 // 100
380301238283661

So, I wanted to see the boundary.

>>> 38030123828366199/100
380301238283662.0
>>> 38030123828366198/100
380301238283662.0
>>> 38030123828366197/100
380301238283662.0
>>> 38030123828366196/100
380301238283661.94
>>> 38030123828366195/100
380301238283661.94
>>> 38030123828366194/100
380301238283661.94
>>> 38030123828366193/100
380301238283661.94
>>> 38030123828366192/100
380301238283661.94
>>> 38030123828366191/100
380301238283661.94
>>> 38030123828366190/100
380301238283661.9
>>> 38030123828366189/100
380301238283661.9

I see, it's easy to understand. Depending on the mood of the last bit, it takes a discrete value. of course it is.

It's the same as the number of floating point numbers that can be held just before the limit is even. Let's be aware of the calculation accuracy. .. ..

Recommended Posts

Python hand play (division)
Python hand play (argparse minimum code)
Python hand play (Pandas / DataFrame beginning)
Play Python async
Play with 2016-Python
Python hand play (calculated full of mordred)
Python hand play (descriptor calculation: serious version)
Python hand play (let's get started with AtCoder?)
Python hand play (one line notation of if)
Play youtube in python
Python hand play (interoperability between CSV and PostgreSQL)
Python> floor division> Truncation division?
Python hand play (get column names from CSV file)
[Python] Play with Discord's Webhook.
Play RocketChat with API / Python
Python
Division of timedelta in Python 2.7 series
Let's play with Excel with Python [Beginner]
Python hand play (RDKit descriptor calculation: SDF to CSV using Pandas)
Play video with sound with python !! (tkinter / imageio)
Install Pyaudio to play wave in python
Play audio files from Python with interrupts
Play handwritten numbers with python Part 2 (identify)
Continuously play the first Python Sukusta MV
Search and play YouTube videos in Python
Fractal to make and play with Python
I want to play with aws with python