博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Division Operators
阅读量:4031 次
发布时间:2019-05-24

本文共 5318 字,大约阅读时间需要 17 分钟。

Division Operators

In general, the data type of an expresion depends on the types of the arguments. This rule meets our expectations for most operators: when we add two integers, the result should be an integer. However, this doesn't work out well for division because there are two different expectations. Sometimes we expect division to create precise answers, usually the floating-point equivalents of fractions. Other times, we want a rounded-down integer result.

The classical Python definition of / depended entirely on the arguments. 685/252 was 2 because both arguments where integers. However, 685./252. was 2.7182539682539684 because the arguments were floating point.

This definition often caused problems for applications where data types were used that the author hadn't expected. For example, a simple program doing Celsius to Fahrenheit conversions will produce different answers depending on the input. If one user provides 18 and another provides 18.0, the answers were different, even though all of the inputs all had the equal numeric values.

>>> print 18*9/5+3264>>> print 18.0*9/5 + 3264.4>>> 18 == 18.0True>>>

This unexpected inaccuracy was generally due to the casual use of integers where floating-point numbers were more appropriate. (This can also occur using integers where complex numbers were implictly expected.) An explicit conversion function (like float( x )) can help prevent this. The idea, however, is for Python be a simple and sparse language, without a dense clutter of conversions to cover the rare case of an unexpected data type.

Starting with Python 2.2, a new division operator was added to clarify what was expectred. The ordinary / operator will, in the future, return floating-point results. A special division operator, //, will return rounded-down results. Generally, the new /operator is what most mathematical processing will use.

In additional two tools were made avialable that will end with Python 3.0. These tools will allow the division operator, /, to operate either under the old (classical, prior to version 2.2) rules or the new rules. This gives programmers a way to keep older applications running; it also gives them a way to explicitly declare that a program uses the newer operator definition. There are two parts to this: a statememt that can be placed in a program, as well as a command-line option that can be used when starting the Python interpreter.

Program Statements. To ease the transition from older to newer language features, there is a __future__ module available. This module includes a division definition that changes the definition of the / operator from classical to future. You can include the following from statement to state that your program depends on the future definition of division.

from __future__ import divisionprint 18*9/5+32print 18*9//5+32

This produces the following output. The first line shows the new use of the / operator to produce floating point results, even if both arguments are integers. The second line shows the // operator, which produces rounded-down results.

64.464

The from __future__ statement will set the expectation that your script uses the new-style floating-point division operator. This allows you to start writing programs with version 2.2 that will work correctly with all future versions. By version 3.0, this import statement will no longer be necessary, and these will have to be removed from the few modules that used them.

Programmers should put the from __future__ statement in every Python script file, to be sure that what they are writing will work correctly. Eventually, these can be removed, but for the forseeable future, they are a necessary part of each Python script file.

Command Line Options. Another tool to ease the transition is a command-line option used when running the Python interpreter. This can force old-style interpretation of the / operator or to warn about old-style use of the / operator between integers. It can also force new-style use of the / operator and report on all potentially incorrect uses of the / operator.

The Python interpreter command-line option of -Q will force the / operator to be treated classically (“old”), or with the future (“new”) semantics. If you run Python with -Qold, the / operator's result depends on the arguments. If you run Python with -Qnew, the / operator's result will be floating point. In either case, the // operator returns a rounded-down integer result.

You can use -Qold to force old modules and programs to work with version 2.2 and higher. When Python 3.0 is released, however, this transition will no longer be supported; by that time you should have fixed your programs and modules.

To make fixing easier, the -Q command-line option can take two other values: warn and warnall. If you use -Qwarn, then the /operator applied to integer arguments will generate a run-time warning. This will allow you to find and fix situations where the // operator might be more appropriate. If you use -Qwarnall, then all instances of the / operator generate a warning; this will give you a close look at your programs.

You can include the command line option when you run the Python interpreter. For Linux and MacOS users, you can also put this on the #! line at the beginning of your script file.

#!/usr/local/bin/python -Qnew

转载地址:http://gihbi.baihongyu.com/

你可能感兴趣的文章
Java 2019 面试宝典
查看>>
Redis面试题之持久化和五种部署方式
查看>>
搞定Spring Cloud断路器组件 Hystrix 的舱壁模式
查看>>
mybatis之大于、小于、大于等于和小于等于的写法
查看>>
如何实现 Oracle 的自增序列,两步轻松搞定
查看>>
不要等到Oracle磁盘空间满了,再去查表空间使用情况
查看>>
Hive SQL常用命令总结,大数据开发人员按需收藏
查看>>
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils
查看>>
使用 Springboot 对 Kettle 进行调度开发
查看>>
Kettle链接MySQL报错:Driver class 'org.gjt.mm.mysql.Driver' could not be found
查看>>
Python的Turtle库非常实用,绘制图形竟然这么简单
查看>>
JVM面试要点:G1 垃圾收集器和如何做到可预测的停顿
查看>>
阿里巴巴倡导的数据中台,到底是什么东东
查看>>
揭底JVM,怎么能不了解G1垃圾收集器
查看>>
如何优雅的编程,lombok你怎么这么好用
查看>>
@RequestParam、@QueryParam等Spring常见参数注解区别,你知道吗
查看>>
玩转远程Debug,两步轻松开启IDEA远程调试
查看>>
Jmeter压测错误,Address already in use: connect
查看>>
Intellij IDEA常用快捷键,最全总结
查看>>
前端干货,超实用的JQuery小技巧
查看>>