在 MATLAB 中将双精度数转换为单精度而不损失精度

发布于 2024-10-09 07:37:07 字数 566 浏览 0 评论 0原文

我认为这是不可能的,因此我决定在这里询问,因为谷歌搜索没有返回任何表明我可以这样做的结果。

特别是在阅读此内容之后:

可以使用双精度数来表示 64 位数字而不损失精度

尽管我的数字可以保存为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌梦深 2024-10-16 07:37:07

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.

森林散布 2024-10-16 07:37:07

当您进行此类实验时,您应该打开电源,

format long

以便可以看到更多的小数值。例如,

>> pi     
ans =
    3.1416
>> format long
>> ans
ans =
   3.141592653589793

When you're doing these kinds of experiments, you should turn on

format long

so you can see more decimal values. For example,

>> pi     
ans =
    3.1416
>> format long
>> ans
ans =
   3.141592653589793
何以心动 2024-10-16 07:37:07

如果您唯一关心的是整数,您可以使用 int32 代替

If your only concern are integers, you could use int32 instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文