在 MATLAB 中将双精度数转换为单精度而不损失精度
我认为这是不可能的,因此我决定在这里询问,因为谷歌搜索没有返回任何表明我可以这样做的结果。
特别是在阅读此内容之后:
尽管我的数字可以保存为 32 位,如下例所示。
但是 MATLAB 中有没有办法将双精度值转换为单精度值而不丢失信息?
例如在 MATLAB 中
> a = 103364148
a =
103364148
> single(a)
ans =
103364144
或者也许有另一种语言(例如 Python)中的另一种方法?
我正在使用 GPUMat,但只能使用 GPUSingle,因此我试图找到一种方法来处理与 MATLAB 相同的内容,而与 GPU 的单倍内容相同。
谢谢,
I don't think this is possible, hence I decided to ask here to see as googling around hasn't returned any results that hint that I can do so.
Especially after reading this:
Can doubles be used to represent a 64 bit number without loss of precision
Though my numbers can be held in 32bit as the example below shows.
But is there any way in MATLAB to convert a double precision value to single without loosing information?
e.g. in MATLAB
> a = 103364148
a =
103364148
> single(a)
ans =
103364144
Or maybe there is another way in another language, e.g. Python?
I'm working with GPUMat where I can only use GPUSingle, so I'm trying to find a way to work with stuff that is double to MATLAB in single to the GPU.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Single 可以容纳最多 2^24 (16,777,216) 的整数,而不会损失精度 - 符号和指数需要一些位。
换句话说,不,没有办法可以让大于2^24的数字无错误地放入一个单一的(注意它可以容纳一些更大的数字,只要它们可以写成一个数字的乘积)较小的 2^24 和 2 的某个幂)。
但是,您确定您的计算需要这种精度吗?只要所有整数都小于 2^24,就应该没问题。
A single can hold integer numbers up to 2^24 (16,777,216) without loss of precision - some bits are required for the sign and the exponent .
In other words, no, there is no way that you can make a number larger than 2^24 fit into a single without error (note that it can hold some larger numbers, as long as they can be written as the product of a number smaller 2^24 and some power of 2).
However, are you sure you need that kind of precision for your calculations? As long as all your integers are less than 2^24, you should be fine.
当您进行此类实验时,您应该打开电源,
以便可以看到更多的小数值。例如,
When you're doing these kinds of experiments, you should turn on
so you can see more decimal values. For example,
如果您唯一关心的是整数,您可以使用 int32 代替
If your only concern are integers, you could use int32 instead