以浮点数形式向下移动点
首先,感谢您的关注,我是这个网站的新手 ^^ 所以如果我做错了什么请原谅...
我的 Python 代码有一个很大的问题...我是编程新手,而且我我也是 Python 新手。
我需要取一个浮点数并将该点向右移动,使其成为一个整数,例如取 60.27 并得到 6027。
我使用的算法是递归乘以 num*10
直到 num%2==0
,然后获取int(num)
。
问题是,当我相乘(例如)602.47*10
时,它返回6024.700000000001
并且它显然不起作用:-)
有什么方法可以修复它,或者任何其他技术或其他方式可以递归地执行此操作?我可以使用我需要的任何东西,但它必须是递归的:没有 for
或 while
...
感谢您的帮助!我的母语不是英语,所以如果很难阅读,请原谅......
First off all, thanks for the attention, I'm new to this site ^^ so excuse me if I do something wrong...
I have a huge problem with my Python code... I'm new to programming, and I'm new to Python as well.
I need to take a floating point number and move the point right so it becomes an integer, like taking 60.27 and getting 6027.
The algorithm that I'm using is recursively multiplying num*10
until num%2==0
, then getting the int(num)
.
The problem is, when I multiply (for example) 602.47*10
it returns 6024.700000000001
and it obviously doesn't work :-)
Is there any way to fix it, or any other technique or other way to do this recursively?? I'm allowed to use anything I need, but its got to be recursive: no for
or while
...
Thanks for the help!! My first language is not english, so I beg your pardon if it's hard to read...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
lstrip('0')
防止小数低于 1。来自文档:
Use
lstrip('0')
to guard against decimals below 1.From the docs:
浮点表示有这个问题。
您是否想要更改:
全部更改为 12345?
对于没有精确表示的浮点数(您提到 6024.70),您是否期望得到 6024700000000001,因为这是可以存储在浮点数中的最接近 6024.70 的输出?
Floating point representations have that issue.
Are you looking to change:
all to 12345?
For floats which have no exact representation (you mention 6024.70), do you expect to get 6024700000000001, since that's the output of the closest thing to 6024.70 which can be stored in float?
您可以尝试以下操作:
编辑: 作为旁注,字符串
.replace
和.translate
对于各种大小的浮点数具有相似的性能You could try something like:
Edit: as a side note, the string
.replace
and.translate
have similar performance for various sized floats将数字解析为字符串并进行字符串操作的算法会更可靠。正如您所见,任何涉及浮点数的数值计算都必然不准确。这是没有办法解决的。
It would be more reliable an algorithm to just parse the number as a string and do a string manipulation. Any numerical calculation involving floating-point numbers are bound to inaccuracy, as you've witnessed. There's no going around that.
由于您无法(可靠地)使用浮点来完成您想要的操作,因此一个简单的技巧是将数字转换为字符串,然后去掉小数点:
这将适用于任何未用科学记数法表示的数字。如果您的数字足够大(或小),以至于最终以科学计数法表示,请查看 这个。
Since you can't (reliably) use floating point to do what you want, an easy hack is to convert the number to a string then rip out the decimal point:
That will work with any number that isn't represented in scientific notation. If your numbers are big (or small) enough that they do end up represented in scientific notation, have a look at this.
只是在黑暗中大胆尝试,但是您的数字是否代表您试图在(例如)美元和美分之间转换的金额?如果是这样,您需要停止正在做的事情并将所有内容转换为美分,并且在实际向用户展示内容时仅使用“美元”值。使用浮点数表示货币值是一个非常非常糟糕的主意。
如果没有,请忽略我:-)
Just taking a wild stab in the dark here, but do your numbers represent amounts of money that you're trying to convert between (say) dollars and cents? If so, you need to stop what you are doing and convert everything to cents, and only use "dollar" values when actually presenting things to the user. Using floating point numbers for money values is a very, very bad idea.
If not, ignore me :-)