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

在 Python 中確定特定數(shù)字的精度和小數(shù)位數(shù)

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

問(wèn)題描述

我在 Python 中有一個(gè)包含浮點(diǎn)數(shù)的變量(例如 num = 24654.123),我想確定數(shù)字的精度和比例值(在 Oracle 意義上),所以 123.45678應(yīng)該給我 (8,5),12.76 應(yīng)該給我 (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.

我首先考慮使用字符串表示(通過(guò) strrepr),但是對(duì)于大數(shù)來(lái)說(shuō)這些都失敗了(雖然我現(xiàn)在明白這是浮點(diǎn)的限制表示這是這里的問(wèn)題):

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

下面的好點(diǎn).我應(yīng)該澄清一下.該數(shù)字已經(jīng)是一個(gè)浮點(diǎn)數(shù),并且正在通過(guò) cx_Oracle 推送到數(shù)據(jù)庫(kù).我試圖在 Python 中盡我所能來(lái)處理對(duì)于相應(yīng)數(shù)據(jù)庫(kù)類型來(lái)說(shuō)太大的浮點(diǎn)數(shù),而不是執(zhí)行 INSERT 和處理 Oracle 錯(cuò)誤(因?yàn)槲蚁胩幚碜侄沃械臄?shù)字,而不是記錄,在一次).我猜 map(len, repr(num).split('.')) 是最接近浮點(diǎn)數(shù)的精度和比例的?

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?

推薦答案

獲取小數(shù)點(diǎn)左邊的位數(shù)很簡(jiǎn)單:

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

int(log10(x))+1

小數(shù)點(diǎn)右邊的位數(shù)比較棘手,因?yàn)楦↑c(diǎn)值固有的不準(zhǔn)確性.我還需要幾分鐘才能弄清楚這一點(diǎn).

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.

基于這個(gè)原則,這里是完整的代碼.

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)

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

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 国产精品久久久亚洲 | 不卡在线视频 | 福利视频一区二区 | 国产免费自拍 | 欧美一级免费观看 | 欧美一区二区三区免费在线观看 | 中文字幕视频网 | 亚洲视频在线免费观看 | 美女中文字幕视频 | 色综合99 | 免费av观看 | 日韩成人免费视频 | 成人在线免费视频 | 亚洲精品久久久久久首妖 | 成人在线视频免费看 | 国产黄色一级片 | 精品久久1 | 精品国产免费一区二区三区五区 | 成人久久18免费网站 | 久久福利| 久久久久国产一区二区三区四区 | 天天天操| 神马影院一区二区三区 | 91视视频在线观看入口直接观看 | 中文一区二区视频 | 在线看av的网址 | 婷婷国产一区二区三区 | 国产日产欧产精品精品推荐蛮挑 | 亚洲精品视频久久 | 羞羞的视频免费观看 | 精精国产xxxx视频在线播放 | 日韩精品一区二区三区四区视频 | 正在播放国产精品 | 国产91亚洲精品一区二区三区 | 日本不卡一区二区三区在线观看 | 一区二区影院 | 欧美日韩三区 | 成人夜晚看av| 欧美在线国产精品 | 日本免费视频 | 欧美精品网 |