将float类型更改为short,但具有与float类型变量相同的行为 [英] changing float type to short but with same behaviour as float type variable

查看:933
本文介绍了将float类型更改为short,但具有与float类型变量相同的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以更改

float *pointer

类型在VS c ++项目
中用于其他类型,所以它仍然表现为一个浮动类型,但范围较小?
我知道浮点值在该项目中不会超过一个固定值,所以我想通过它使用的内存优化程序。它不需要4个字节为'float *指针'的每个元素,2个字节就足够了我想。如果我将一个float改为short并模拟浮点行为,那么它将使用两倍更短的内存。怎么做?

type that is used in the VS c++ project to some other type, so that it will still behave as a floating type but with less range? I know that the floating point values never exceed some fixed value in that project, so I want to optimize the program by memory it uses. It doesn't need 4 bytes for each element of the 'float *pointer', 2 bytes will be enough I think. If I change a float to short and imitate the floating point behaviour, then it will use twice shorter memory. How to do it?

编辑:

它计算概率。因此,有诸如
A / B
的分区,其中A < B,
还有B(和A)可以从1到10 000.

It calculates the probabilities. So there are divisions like A / B Where A < B, And also B (and A) can be from 1 to 10 000.

推荐答案

在IEEE 754-2008中描述的称为binary16的16位浮点格式。它被指定为以减小的精度存储浮点值的格式。几乎没有编译器支持(我认为GCC支持某些ARM平台),但它是很容易滚动自己的例程。此同事:

There is a standard 16-bit floating point format described in IEEE 754-2008 called "binary16". It is specified as a format to store floating point values with reduced precisions. There is almost no compiler support for that yet (I think GCC supports it for certain ARM platforms), but it is quite easy to roll your own routines. This fellow:

http://blog.fpmurphy.com/2008/12/half-precision-floating-point-format_14.html

写关于它的位并且还呈现用于转换半浮动浮点的例程。

wrote a bit about it and also presents a routine to convert half-float <-> float.

此外,这里似乎是一个半浮动的 C ++ wrapper 类:

Also, here seems to be a half-float C++ wrapper class:

half.h:
http://www.koders.com/cpp/fidABD00D95DE84C73BF0218AC621E400E07AA77B53。 aspx
half.cpp
http://www.koders .com / cpp / fidF0DD0510FAAED03817A956D251787609BEB5989E.aspx

提供HalfFloat作为可能的替代类型。

which supplies "HalfFloat" as a possible drop-in replacement type.

这篇关于将float类型更改为short,但具有与float类型变量相同的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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