在Python中确定特定数字的精度和比例 [英] Determine precision and scale of particular number in Python

查看:1172
本文介绍了在Python中确定特定数字的精度和比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有一个包含浮点数的变量(例如 num = 24654.123 ),我想确定数字的精度和刻度值Oracle感觉),所以123.45678应该给我(8,5),12.76应该给我(4,2)等等。代表(通过 str repr ),但那些失败的大数字(虽然我现在明白这是浮动的限制这就是问题所在):

 >>> num = 1234567890.0987654321 
>>> str(num)= 1234567890.1
>>>编辑:



好点以下。我应该澄清。这个数字已经是一个浮点数,正通过cx_Oracle被推送到数据库。我想尽我所能在Python中处理对于相应的数据库类型来说太大的浮动,因为执行INSERT和处理Oracle错误(因为我想处理一个字段,而不是一个记录,在一次)。我想 map(len,repr(num).split('。'))是否最接近float的精度和比例?

解决方案

获取小数点左边的位数很容易:

  int(log10(x))+ 1 

由于浮点值固有的不准确性,所以小数点右边的位数比较棘手。我需要再花几分钟时间才能找出答案。



编辑:基于这个原则,下面是完整的代码。 p>

 导入数学

def precision_and_scale(x):
max_digits = 14
int_part如果int_part == 0 else int(math.log10(int_part))+ 1
如果数值> = max_digits:
返回值(数值)= int(abs(x))
数值= ,0)
frac_part = ABS(X) - int_part
乘法器= 10 **(max_digits - 幅度)
frac_digits =乘数+ INT(乘数* frac_part + 0.5)
而frac_digits%10 == 0:
frac_digits / = 10
标度= INT(math.log10(frac_digits))
返回(量值+刻度,刻度)


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.

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

Edit:

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.

Edit: 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中确定特定数字的精度和比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆