python外卷(10)--取整

"取整"那些事

  • 1.python 內置函數
    • 1.1int()--向下取整
    • 1.2round()--四舍五入
  • 2.math模塊取整函數
    • 2.1 math.floor()--向下取整
    • 2.2 math.ceil()--向上取整
    • 2.3 math.modf()--分別取小數部分和整數部分
  • 3.numpy模塊取整函數
    • 3.1 numpy.floor()--向下取整
    • 3.2 numpy.ceil()--向上取整
    • 3.3 numpy.trunc()/numpy.fix()--截取整數部分
    • 3.4 numpy.rint()--四舍五入
    • 3.5 numpy.around()--四舍五入保留指定位數的小數
  • 4.保留有效數字
    • 4.1格式化字符串保留有效數字
  • 5.溫馨提示

1.python 內置函數

1.1int()–向下取整

>>> int(3.6)
3

1.2round()–四舍五入

可取整.可保留指定位小數

>>> round(3.3)
3
>>> round(3.5)
4
>>> round(3.678,2)
3.68

2.math模塊取整函數

2.1 math.floor()–向下取整

>>> math.floor(3.6)
3

2.2 math.ceil()–向上取整

>>> math.ceil(3.4)
4

2.3 math.modf()–分別取小數部分和整數部分

返回一個元祖

>>> math.modf(3.79)
(0.79, 3.0)

參考博文:https://www.cnblogs.com/sen-c7/p/9473224.html

3.numpy模塊取整函數

3.1 numpy.floor()–向下取整

>>> numpy.floor(3.4)
3.0

3.2 numpy.ceil()–向上取整

>>> numpy.ceil(3.4)
4.0

3.3 numpy.trunc()/numpy.fix()–截取整數部分

對正數來說是向下取整,對負數來說是向上取整

>>> numpy.trunc(3.5)
3.0
>>> numpy.trunc(-3.5)
-3.0>>> numpy.fix(3.5)
3.0

3.4 numpy.rint()–四舍五入

>>> numpy.rint(3.5)
4.0
>>> numpy.rint(3.4)
3.0
>>> 

3.5 numpy.around()–四舍五入保留指定位數的小數

>>> numpy.around(3.678,1)
3.7
>>> numpy.around(3.678,2)
3.68
>>> numpy.around(3.678,0)
4.0

參考博文:https://blog.csdn.net/runmin1/article/details/89174511

4.保留有效數字

4.1格式化字符串保留有效數字

在打印輸出時會經常用到

>>> a="%.2f"%3.678
>>> a
'3.68'
>>> b="%.3f"%3.6789
>>> b
'3.679'
>>> c="{0:.2f}".format(3.1415)
>>> c

科學計數法可以保留三位有效數字

>>> d="%.2e"%0.00345
>>> d
'3.45e-03'>>> e="%.2f"%0.00345
>>> e
'0.00'

5.溫馨提示

1.Python內置函數,math取整函數 可以對numpy數組進行操作

>>> a=numpy.array([3.6])
>>> math.floor(a)
3
>>> int(a)
3

2.numpy函數取完整數,并不是int 而是float.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445101.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445101.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445101.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

模擬銀行家算法

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h>#define ElemType PCB #define Status int #define true 1 #define false 0 #define OK 1 #define ERROR 0 #define RESOURCE_NUM …

Lua 與 C混合編程 .

本文通過程序實例說明C調用lua腳本和lua調用C的方法: 先建立一個 test.c文件: #include <stdio.h> #include <stdlib.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" #pragma comment(lib, "lua5.1.lib&qu…

模擬固定分區分配

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PCB #define Status int…

Linux下的lua和boost c++的搭建和安裝

先下載lua &#xff0c;boost c http://www.lua.org/versions.html#5.2 http://www.boost.org/ http://sourceforge.net/projects/luabind/ 1. 安裝lua [rootlocalhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local [rootlocalhost ~]# cd /usr/local/ [rootlocalhost lo…

模擬基本分頁存儲

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>#define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PC…

常用正則表達式和shell命令列表

取當前目錄下普通文件的后綴名列表&#xff1a; ls -l | awk /^-/{print $NF} |awk -F. {print $NF}|awk !/^$/ 匹配0和正整數的正則表達式&#xff08;除0以外&#xff0c;其它數字不能以0開頭&#xff09;&#xff1a; (^0$)|(^[0-9]\d*$) 匹配中文字符的正則表達式&#xff…

無限踩坑系列(7)-Latex使用Tips

Latex常用命令1.latex注釋2.圖片左邊對齊3.字母頭上加聲調4.腳注5.公式中加空格6.字體加粗體7.公式換行8.\textsuperscript{*}9.\begin{itemize}10.\operatorname{trace}11.\noindent12.\textcircled{}圓圈數字一些TIPs1.latex注釋 單行使用百分號%注釋 多行使用如下命令,在編…

在CentOS6.2下安裝DNS服務軟件Bind并快速配置簡單實例

[實踐Ok]在CentOS6.2下安裝DNS并快速配置實例&#xff0c;共八步&#xff0c;心路歷程如下&#xff1a;背景介紹&#xff1a;在日常的開發中&#xff0c;往往會在測試機和外網的Http的Url實際接口是不一樣的&#xff0c;在測試機一個Url地址&#xff0c;在外網中又是一個地址。…

模擬動態分區分配

介紹 list.h #ifndef _List_h_ #define _List_h_#include "Data.h"//******* 鏈表 *******// Status InitLinkList(LinkList *L); void PCBAssign(PCBType *e1, PCBType e2); Status GetElemt_L(LinkList L,int i,PCBType *e); Status ListIn…

python模塊(4)-Collections

collections1.collection.counter(list)2.collections.defaultdict()3.collection.dequecollections是Python內建的一個集合模塊&#xff0c;提供了許多有用的集合類。collections在python官方文檔中的解釋是High-performance container datatypes1.collection.counter(list) …

js知識點匯總

1.本門課的作用&#xff08;JavaScript的作用&#xff09;所有基于Web的程序開發基礎 2.一種計算機客戶端腳本語言&#xff0c;主要在Web瀏覽器解釋執行。 3.瀏覽器中Javascript&#xff0c;用于與用戶交互&#xff0c;以及實現頁面中各種動態特效 4.在HTML文件中&#xff0…

redis——內存概述

Redis通過自己的方法管理內存,&#xff0c;主要方法有zmalloc(),zrealloc()&#xff0c; zcalloc()和zfree(), 分別對應C中的malloc(), realloc()、 calloc()和free()。相關代碼在zmalloc.h和zmalloc.c中。 Redis自己管理內存的好處主要有兩個&#xff1a;可以利用內存池等手段…

Windows下如何用C語言清空特定文件夾中的所有文件

#include "iostream.h" //由于該博客系統發布是不能顯示正常&#xff0c;代碼如需調試&#xff0c;只需將改成""即可 #include "string.h" #include "stdlib.h" #include "time.h" #include "math.h" #include…

MachineLearning(5)-去量綱:歸一化、標準化

去量綱&#xff1a;歸一化、標準化1.歸一化(Normalization)1.1 Min-Max Normalization1.2 非線性Normalization2.標準化(Standardlization)2.1 Z-score Normalization3.標準化在梯度下降算法中的重要性本博文為葫蘆書《百面機器學習》閱讀筆記。去量綱化 可以消除特征之間量綱的…

GDB調試技術(一)

啟動GDB的方法有以下幾種: 1、gdb <program> program也就是你的執行文件,一般在當然目錄下。 2、gdb <program> core 用gdb同時調試一個運行程序和core文件,core是程序非法執行后core dump后產生的文件。 3、

GDB調試技術(二)

1) 恢復程序運行和單步調試 當程序被停住了,你可以用continue命令恢復程序的運行直到程序結束,或下一個斷點到來。也可以使用step或next命令單步跟蹤程序。 continue [ignore-count] c [ignore-count] fg [ignore-count] 恢復程序運行,直到程序結束,或是下一個斷點到…

關于Java中String的問題

String 對象的兩種創建方式&#xff1a; String str1 "abcd";//先檢查字符串常量池中有沒有"abcd"&#xff0c;如果字符串常量池中沒有&#xff0c;則創建一個&#xff0c;然后 str1 指向字符串常量池中的對象&#xff0c;如果有&#xff0c;則直接將 st…

學點數學(3)-函數空間

函數空間1.距離&#xff1a;從具體到抽象2.范數3.內積4.拓撲本博文為觀看《上海交通大學公開課-數學之旅-函數空間 》所整理筆記&#xff0c;公開課視頻連接&#xff1a;http://open.163.com/newview/movie/free?pidM8PTB0GHI&midM8PTBUHT0數學中的空間 是 大家研究工作的…

Makefile編寫詳解--項目開發

預備知識&#xff1a; gcc 的3個參數&#xff1a; 1. -o 指定目標文件 gcc sources/main.c -o bin/main 2. -c 編譯的時候只生產目標文件不鏈接 gcc -c sources/main.c -o obj/main.o 3. -I 主要指定頭文件的搜索路徑 gcc -I headers -c main.c -o main.o 4. -l 指定靜…

如何判斷對象已經死亡

引用計數 給對象中添加一個引用計數器&#xff0c;每當有一個地方引用它&#xff0c;計數器就加 1&#xff1b;當引用失效&#xff0c;計數器就減 1&#xff1b;任何時候計數器為 0 的對象就是不可能再被使用的。 這個方法實現簡單&#xff0c;效率高&#xff0c;但是目前主流…