简体   繁体   English

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

[英]Convert Double to Single without loss of precision in MATLAB

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 可以使用双精度来表示64位数字而不会损失精度

Though my numbers can be held in 32bit as the example below shows. 虽然我的数字可以保持在32位,如下例所示。

But is there any way in MATLAB to convert a double precision value to single without loosing information? 但是MATLAB中有什么方法可以将双精度值转换为单精度而不丢失信息吗?

eg in MATLAB 例如在MATLAB中

> a = 103364148
a =
   103364148
> single(a)
ans =
   103364144

Or maybe there is another way in another language, eg Python? 也许还有另一种语言,例如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. 我正在与只能使用GPUSingle的GPUMat合作,所以我试图找到一种方法来处理与MATLAB相同的对GPU而言是双重的东西。

Thanks, 谢谢,

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 . 单身人士最多可容纳2 ^ 24(16,777,216)个整数,而不会损失精度-符号和指数需要一些位。

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). 换句话说,没有,您不可能使大于2 ^ 24的数字适合一个整数而不会出错(请注意,只要可以将它们写为数字的乘积,它就可以容纳更大的数字。较小的2 ^ 24和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. 只要您所有的整数都小于2 ^ 24,就可以了。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM