久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Python 中確定特定數字的精度和小數位數

Determine precision and scale of particular number in Python(在 Python 中確定特定數字的精度和小數位數)
本文介紹了在 Python 中確定特定數字的精度和小數位數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 Python 中有一個包含浮點數的變量(例如 num = 24654.123),我想確定數字的精度和比例值(在 Oracle 意義上),所以 123.45678應該給我 (8,5),12.76 應該給我 (4,2),等等.

I have a variable in Python containing a floating point number (e.g. num = 24654.123), and I'd like to determine the number's precision and scale values (in the Oracle sense), so 123.45678 should give me (8,5), 12.76 should give me (4,2), etc.

我首先考慮使用字符串表示(通過 strrepr),但是對于大數來說這些都失敗了(雖然我現在明白這是浮點的限制表示這是這里的問題):

I was first thinking about using the string representation (via str or repr), but those fail for large numbers (although I understand now it's the limitations of floating point representation that's the issue here):

>>> num = 1234567890.0987654321
>>> str(num) = 1234567890.1
>>> repr(num) = 1234567890.0987654

下面的好點.我應該澄清一下.該數字已經是一個浮點數,并且正在通過 cx_Oracle 推送到數據庫.我試圖在 Python 中盡我所能來處理對于相應數據庫類型來說太大的浮點數,而不是執行 INSERT 和處理 Oracle 錯誤(因為我想處理字段中的數字,而不是記錄,在一次).我猜 map(len, repr(num).split('.')) 是最接近浮點數的精度和比例的?

Good points below. I should clarify. The number is already a float and is being pushed to a database via cx_Oracle. I'm trying to do the best I can in Python to handle floats that are too large for the corresponding database type short of executing the INSERT and handling Oracle errors (because I want to deal with the numbers a field, not a record, at a time). I guess map(len, repr(num).split('.')) is the closest I'll get to the precision and scale of the float?

推薦答案

獲取小數點左邊的位數很簡單:

Getting the number of digits to the left of the decimal point is easy:

int(log10(x))+1

小數點右邊的位數比較棘手,因為浮點值固有的不準確性.我還需要幾分鐘才能弄清楚這一點.

The number of digits to the right of the decimal point is trickier, because of the inherent inaccuracy of floating point values. I'll need a few more minutes to figure that one out.

基于這個原則,這里是完整的代碼.

Based on that principle, here's the complete code.

import math

def precision_and_scale(x):
    max_digits = 14
    int_part = int(abs(x))
    magnitude = 1 if int_part == 0 else int(math.log10(int_part)) + 1
    if magnitude >= max_digits:
        return (magnitude, 0)
    frac_part = abs(x) - int_part
    multiplier = 10 ** (max_digits - magnitude)
    frac_digits = multiplier + int(multiplier * frac_part + 0.5)
    while frac_digits % 10 == 0:
        frac_digits /= 10
    scale = int(math.log10(frac_digits))
    return (magnitude + scale, scale)

這篇關于在 Python 中確定特定數字的精度和小數位數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 99热这里都是精品 | 国产91在线观看 | 日韩一区二区三区视频在线观看 | 欧美中文字幕一区 | julia中文字幕久久一区二区 | 精品一区二区三区在线观看国产 | 国产精品国产三级国产aⅴ中文 | 又爽又黄axxx片免费观看 | 亚洲视频一区在线观看 | 欧美成人a | 日本中文在线 | av三级| 91免费视频观看 | 精品三级在线观看 | 丝袜美腿一区二区三区动态图 | 午夜在线小视频 | 久久五月婷| 日韩毛片网 | 日韩高清中文字幕 | 国产日韩欧美激情 | 国产精品美女久久久久aⅴ国产馆 | 免费看的av | 中文字幕在线一区二区三区 | 久久不卡 | 日韩aⅴ片| 在线观看h视频 | 久久久久久久97 | 国产99在线 | 欧美 | 中文字幕在线一区 | 午夜精品一区二区三区在线观看 | 久久久久久久电影 | 天天综合干| 亚洲精品国产电影 | 国产一区 在线视频 | 日韩欧美国产一区二区三区 | 综合久久综合久久 | 男人的天堂avav | 精品亚洲一区二区三区 | 成人国产精品入口免费视频 | 国产一区三区在线 | 国产精品精品视频一区二区三区 |