views:

41

answers:

4

Is it possible to change the

float *pointer

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?

EDIT:

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.

+2  A: 

Maybe use fixed-point math? It all depends on value and precision you want to achieve.

http://www.eetimes.com/discussion/other/4024639/Fixed-point-math-in-C

For C there is a lot of code that makes fixed-point easy and I'm pretty sure there are also many C++ classes that make it even easier, but I don't know of any, I'm more into C.

GDR
First one from google: http://wiki.yak.net/675
GDR
+1  A: 

The first, obvious, memory optimization would be to try and get rid of the pointer. If you can store just the float, that may, depending on the larger context, reduce your memory consumption from eight to four bytes already. (On a 64-Bit system, from twelve to four.)

Whether you can get by with a short depends on what your program does with the values. You may be able to use fix point arithmetic using an integral type such as a short, yes but your questions shows way too little context to judge that.

Christopher Creutzig
A: 

The code you posted and the text in the question do not deal with actual float, but with pointers to float. In all architectures I know of, the size of a pointer is the same regardless of the pointed type, so there would be no improvement in changing that to a short or char pointer.

Now, about the actual pointed elements, what is the range that you expect in your application? What is the precision you need? How many of those elements do you have? What are the memory constraints of your target platform? Unless the range and precision are small and the number of elements huge, just use floats. Also note that if you need floating point operations, storing any other type will require conversions before and after each operation, and you might be impacting performance.

Without greater knowledge of what you are doing, the ranges for short in many architectures are [-32k, 32k), where k stands for 1024. If your data ranges is [-32,32) and you can do with roughly 3 decimal digits you could use fixed point arithmetic with shorts, but there are few such situation.

David Rodríguez - dribeas
+1  A: 

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.

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

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

Luther Blissett