Python中文
首页
教程
问答
标签
搜索
登录
注册
Python 3浮点(X)*i=int(Z)
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个非常大的数字,无论是小数点前还是小数点后,但对于这个,我就称它为4.58。在</p> <p>我想知道一个数字,Y,如果乘以X,它会得到一个整数,而不是任何浮点数。在</p> <p>这是我的代码:</p> <pre><code>from decimal import * setcontext(ExtendedContext) getcontext().prec = 300 x=Decimal('4.58') while True: i=1 a=Decimal(i*x) if float(a).is_integer(): print(i*x) break else: i=+1 </code></pre> <p>然而,这种方法速度非常慢而且效率低下。我想知道如何实现连分式或其他方法来预测Y的值?在</p> <p><em>编辑</em></p> <p>十进制模块更精确地存储浮点数(以字符串形式),因此0.5<em>不会变成0.49999999。在</p> <p><em>编辑2</em></p> <p>我有X(4.58)。在</p> <p>我想知道什么数乘以X得到一个整数;尽可能有效。在</p> <p><strong>编辑3</strong></p> <p>好吧,也许还不是我最好的问题。在</p> <p>这是我的困境。在</p> <p>我从我做的一个琐碎的节目中得到了一个数字。这个数字是一个十进制数,1.5。在</p> <p>我要做的就是找出哪个整数乘以我的十进制数得到另一个整数。在</p> <p>对于1.5,最好的答案是2。(1.5*2=3)(浮点*整数=整数)</p> <p>我上面的while循环最终会做到这一点,但我只想知道是否有更好的方法来实现这一点,比如连分式;如果有,我如何实现它呢。在</p> <p><em>编辑4</em></p> <p>这是感谢用户6794072的代码。它很长,但功能强大。在</p> ^{pr2}$
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>我不是Python程序员,但是<a href="https://docs.python.org/3/library/functions.html#round" rel="nofollow">round</a>函数呢?在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
当用户用PYTHON设置一个或一个不带值的URL时,他们怎么能输入一个/a的代码呢?
1 回答
当用户登录到站点时,如何显示不同的导航栏
5 回答
当用户登录时,在Flask中向用户显示处理结果
8 回答
当用户的Flask会话结束时,我如何从Redis后端中移除所有Celery结果?
5 回答
当用户的Okta配置文件字段当前为blan时,更新该字段
7 回答
当用户的付款逾期2天时,从Django模型检索数据
6 回答
当用户的消息以问号结尾时,如何让机器人说些什么?
6 回答
当用户的系统上可能也安装了Python 2.7时,如何在用户的系统上运行Python 3脚本?
3 回答
当用户确定打印数量时,使用Matplotlib打印动画
5 回答
当用户离开时是否可以删除整个网页?
10 回答
当用户给出一个单词时如何打印?
4 回答
当用户继续更改TKin中的值(使用trace方法)时,使用Entry并更新输入的条目
8 回答
当用户编辑表单字段时,从Django时间字段中删除秒数
3 回答
当用户被更改时,消息不会来自web套接字
6 回答
当用户访问表单时,如何使表单为只读,而不具有更改权限
6 回答
当用户试图更改对象的值时,使用描述符类引发RuntimeError
1 回答
当用户调整GUI的大小时,是否有方法更改GUI内容的大小?
1 回答
当用户调整风的大小时,pythontkinter小部件的大小会不均匀
9 回答
当用户购买某个类别时,是否查找其他类别的销售?
5 回答
当用户转到上一页时,Django和芹菜插入操作
9 回答