TZPike05
TZPike05

Reputation: 1188

Memory used for Float_64

I'm relatively new to using C/C++. I'm currently using Visual Studio 2010 and am analyzing some code written by someone else. I was notified of an issue where I have a function using too much memory. Virtually all of the pointers in it are Float_64 and I was wondering what kind of repercussions there were if I were to change them to Float_32. How much memory is used by Float_64 and Float_32 and is there any data loss by switching? I apologize if this has already been asked however I couldn't seem to find it.

Thank you!

Upvotes: 2

Views: 2240

Answers (3)

huon
huon

Reputation: 102216

How much memory is used by Float_64 and Float_32

8 bytes (64 bits) and 4 bytes (32 bits) respectively.

is there any data loss by switching?

Tonnes! Float_32 is what is known as single-precision (often float), while Float_64 is double-precision (normally double).

This is actually a bit of a misnomer since doubles have more than double the precision of floats: doubles can represent just under 16 decimal places accurately, while floats can only represent just over 7. That is, you loose 9 decimal places of accuracy by switching, which can easily break some algorithms.

Upvotes: 0

ssube
ssube

Reputation: 48307

First of all, Float_32 and Float_64 are not builtin types, so far as I know. That suggests that they're being typedefd, probably to float (32) and double (64).

Assuming that is the case, and probably even if not, your Float_32 likely takes 32 bits of space and your Float_64 is 64 bits wide. That's just going from the names. Since you say they're pointers, then the sizes will be platform dependent (32 bits on 32-bit systems and 64 on 64), but still point to the same size values (so the final memory use will be pointer+type, or 128 bits on a 64-bit system using a Float_64; I'm sure you can do the rest of the math).

By replacing all the Float_64s with Float_32s, you'll be able to cut the memory use by a quarter to half, but how much that saves depends on how many numbers are in use. In most cases, it's not likely to help, but it very well could if you have millions of floats around.

The possible repercussions are a bit more interesting. Floating point numbers have an annoying property of precision loss, particularly near the edges of their range. For 64-bit floats (doubles), this is well past the trillions. For 32-bit, however, you start to lose precision after the billions, and by the trillions, numbers start skipping quite a bit. Depending on what sort of data you're working with, that may effect you.

Upvotes: 2

John Dibling
John Dibling

Reputation: 101484

Float_64 and Float_32 are not C++ types defined in the C++ Standard, so they must be something specific to your application or the libraries you are using.

However, if the names are remotely sane, the size of a Float_64 will be 64 bits (8 Bytes on modern architectures), and a Float_32 will be 32 bits. Therefore, if you switch to using Float_32 instead of Float_64, you will reduce the memory pressure of the floats themselves by half. Now, that's to say nothing of the pointers to the values, which will still be the same size as they were. So, consider:

Float_64 my64;  
Float_32 my32;

Float_64 * my64_ptr = &my64;
Float_32 * my32_ptr = &my32;

In the above code, my64 is half the sizeof() as my32 -- 64 and 32 bits (8 and 4 bytes) respectively. However, my64_ptr and my32_ptr are pointers, and pointers are the same size, regardless of what they point to.

The reprecussions are another consideration. A Float_32 has, presumably, less precision than a Float_64 -- perhaps half as much, or even less. If you need floats with a very high degree of signifigance, you may loose precision when converting to the smaller type. The only way to know for sure is to examine the specifications for both types w.r.t. precesion, and analyze the data you need to manipulate. However, as a general rule of thumb, the lower precision is very possibly sufficient for most uses.

Upvotes: 2

Related Questions