简体   繁体   English

unsigned short to custom float 16位

[英]unsigned short to custom float 16 bit

I have some big floats array, but I dont need all the 32bit precision and need save ram, so I use unsigned short type, since I only need 2 decimals an max dont excedd 654. 我有一些大的float数组,但是我不需要所有的32位精度,也不需要保存ram,所以我使用了unsigned short类型,因为我只需要2个小数位,最大不超过654。

So the only thing I have to do for each ushort is to multiply with 0.01; 因此,对于每个ushort,我唯一要做的就是乘以0.01。

Is there a way I can create a type for this, using typedef or union, or struct, without wasting ram and save maximum cpu? 有没有一种方法可以使用typedef或union或struct为此创建类型,而又不浪费ram并节省最大的cpu?

what I have now is this, not cpu friendly I guess 我现在所拥有的是这个,我猜对cpu不友好

vector<unsigned short> vect(bigvalue);
for(int i=0;i<bigvalue;i++)(vect[i]*0.01)*some expression...

What I want is some type, wich return already multiplied by 0.01 not wasting ram 我想要的是某种类型,其中返回值已经乘以0.01而不浪费ram

vector<float16bit> vect(bigvalue);
for(int i=0;i<bigvalue;i++)(vect[i])*some expression...

Is there a better way? 有没有更好的办法?

Thanks 谢谢

You can create class (witch stores unsigned short number) with overloaded arithmetic operators (+ - * /, etc). 您可以使用重载算术运算符(+-* /等)创建类(女巫存储无符号短号)。 These operators should consider factor of 100 while doind their job. 这些操作员在工作时应考虑100的因数。

class FastFloat
{
    unsigned short n;

    ...

    FastFloat(const unsigned short n): n(n){}

    FastFloat operator+(const FastFloat &ff)
    {
        return FastFloat(this->n + ff.n);
    }

    // same for other operators
};

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

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