将浮点转换为固定点 [英] Converting floating point to fixed point

查看:230
本文介绍了将浮点转换为固定点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,将任何浮点值(float)转换为 固定点 (int,16:16或24:8)?



编辑部分给它们:整数部分和小数部分。整数部分可以由有符号或无符号整数数据类型表示。小数部分由无符号数据整数数据类型表示。



为了清楚起见,让我们用钱比喻一下。小数部分可以表示美分 - 美元的小数部分。 'cents'数据类型的范围将为0到99.如果8位无符号整数用于定点数学,则小数部分将被拆分为256个可均分的部分。

$

解决方案

这里:

  //已签名的固定点16:16类
类FixedPoint_16_16
{
short intPart;
unsigned short fracPart;

public:
FixedPoint_16_16(double d)
{
* this = d; // calls operator =
}

FixedPoint_16_16& operator =(double d)
{
intPart = static_cast< short>(d);
fracPart = static_cast< unsigned short>
(numeric_limits< unsigned short> + 1.0)* d);
return * this;
}

//其他运算符可以在这里定义
};

编辑:这是一个更常见的类,基于另一种常见的方式来处理固定点数(以及KPexEA指出的):

  template< class BaseType,size_t FracDigits> 
class fixed_point
{
const static BaseType factor = 1< FracDigits;

BaseType数据;

public:
fixed_point(double d)
{
* this = d; // calls operator =
}

fixed_point& operator =(double d)
{
data = static_cast< BaseType>(d * factor);
return * this;
}

BaseType raw_data()const
{
return data;
}

//其他运算符可以在这里定义
};


fixed_point< int,8> fp1; //将被签名24:8(如果int是32位)
fixed_point< unsigned int,16> fp1; //将无符号16:16(如果int是32位)


In C++, what's the generic way to convert any floating point value (float) to fixed point (int, 16:16 or 24:8)?

EDIT: For clarification, fixed-point values have two parts to them: an integer part and a fractional part. The integer part can be represented by a signed or unsigned integer data type. The fractional part is represented by an unsigned data integer data type.

Let's make an analogy with money for the sake of clarity. The fractional part may represent cents -- a fractional part of a dollar. The range of the 'cents' data type would be 0 to 99. If a 8-bit unsigned integer were to be used for fixed-point math, then the fractional part would be split into 256 evenly divisible parts.

I hope that clears things up.

解决方案

Here you go:

// A signed fixed-point 16:16 class
class FixedPoint_16_16
{
    short          intPart;
    unsigned short fracPart;

public:
    FixedPoint_16_16(double d)
    {
        *this = d; // calls operator=
    }

    FixedPoint_16_16& operator=(double d)
    {
        intPart = static_cast<short>(d);
        fracPart = static_cast<unsigned short>
                    (numeric_limits<unsigned short> + 1.0)*d);
        return *this;
    }

    // Other operators can be defined here
};

EDIT: Here's a more general class based on anothercommon way to deal with fixed-point numbers (and which KPexEA pointed out):

template <class BaseType, size_t FracDigits>
class fixed_point
{
    const static BaseType factor = 1 << FracDigits;

    BaseType data;

public:
    fixed_point(double d)
    {
        *this = d; // calls operator=
    }

    fixed_point& operator=(double d)
    {
        data = static_cast<BaseType>(d*factor);
        return *this;
    }

    BaseType raw_data() const
    {
        return data;
    }

    // Other operators can be defined here
};


fixed_point<int, 8> fp1;           // Will be signed 24:8 (if int is 32-bits)
fixed_point<unsigned int, 16> fp1; // Will be unsigned 16:16 (if int is 32-bits)

这篇关于将浮点转换为固定点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆