Skip to Content
Python数据处理
book

Python数据处理

by Jacqueline Kazil, Katharine Jarmul
July 2017
Intermediate to advanced
398 pages
11h 54m
Chinese
Posts & Telecom Press
Content preview from Python数据处理
18
2
Python
会返回
0.3
,这也正是我们本来希望看到的值(而不是
0.30000000000000004
)。
我们来一行一行地阅读这三行代码:
from
decimal
import
getcontext, Decimal
getcontext().prec = 1
Decimal(0.1) + Decimal(0.2)
decimal
模块中导入
getcontext
Decimal
将舍入精度设定为一位小数。
decimal
模块将大部分的舍入和精度设置保存在默认的
下文
context
)中。本行代码将上下文的精度改成只保留一位小数。
对两个小数(一个值为
0.1
,一个值为
0.2
)求和。
如果修改
getcontext().prec
的值会怎么样?动手试一下,然后重新运行最后一行代码。
你应该会看到不一样的结果,这取决于你设置的是保留几位小数。
如前文所述,在数据处理过程中你会遇到许多数学上的细节问题。你可能需要完成的数学
运算也有许多不同的方法,但在用到非整数时,使用小数型数据可以使计算精度更高。
Python
中的数字
Python
中的数字类型有不同的精度水平,这是
Python
语言的缺点之一。随着对数据
处理的深入学习,我们将在本书中学到更多
Python
中与数字和数学相关的库。要是
你现在就想知道,并且打算做一些不那么基础的数学运算,下面是一些你需要熟悉的
Python
库。
decimal
https://docs.python.org/2/library/decimal.html ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

数据科学中的实用统计学(第2版)

数据科学中的实用统计学(第2版)

Peter Bruce, Andrew Bruce, Peter Gedeck
Java持续交付

Java持续交付

Daniel Bryant, Abraham Marín-Pérez
解密金融数据

解密金融数据

Justin Pauley

Publisher Resources

ISBN: 9787115459190