Archived

This topic is now archived and is closed to further replies.

Floats vs. Doubles

This topic is 5265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I use floaters hundreds of times in my program, vs. doubles hundreds of times, will this noticably affect the amount of memory that is used? Just a quickie I was pondering... Thanks, Ryan Buhr Reactor Interactive, LLC. www.reactorinteractive.net

Share this post


Link to post
Share on other sites
sizeof(float) typically is 32bits
sizeof(double) typically is 64bits

64-32 = 32

32 * hundreds = 32 hundreds


Doesn''t seem like a lot to me. Now if you say millions per second as a game may access, then the difference is it takes twice as long to load a double as it does a float because it''s twice as big.

Most 3D hardware uses 32bit floats as well, so a down converion would be wasteful.



Make a typedef, then you can try both easily.

/*
typedef float float_t;
/*/
typedef double float_t;
//*/

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Magmai Kai Holmlor
Make a typedef, then you can try both easily.

/*
typedef float float_t;
/*/
typedef double float_t;
//*/

My suggestion is on the name "Real" as in a real number.
/*
typedef float Real;
/*/
typedef double Real;
//*/
YMMV on whether it should be PascalCase or not.

Share this post


Link to post
Share on other sites
Depends what you use it for. If you are using it in OpenGL calls or the like then float is probably what you want, since the video card will use the float natively. For normal operations both are faster, as it depends entirely on what system you run it on. Unless you need double accuracy then just use float, since it will be easier on system memory either way.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''ve done a software-renderer and just swapping from floats to doubles increased the speed. It was compiled with VC++ 6 and tested on pII and pIII. I have really no clue on why it was faster. I actually belive it should have been slower. So...

/__fold

Share this post


Link to post
Share on other sites
doubles are faster than floats cause floats are converted into doubles.the only limitation is the size

"Tonight we strike,there is thunder in the sky,together we''ll fight,some of us will die,but they''ll always remember that we''ve made a stand and many will die by hand!" - ManOwaR

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sometimes using doubles can alow you to get rig of code that detects and takes care of round-off errors, and it can be a bust, i use floats for rendering, storage, and some real-time calculations that does need to be precise (tex coord or vectors), other stuff runs on doubles; polygon windings ops, BSP and such

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
quote:
Original post by Magmai Kai Holmlor
Make a typedef, then you can try both easily.

/*
typedef float float_t;
/*/
typedef double float_t;
//*/

My suggestion is on the name "Real" as in a real number.
/*
typedef float Real;
/*/
typedef double Real;
//*/
YMMV on whether it should be PascalCase or not.


...but it''s anything but a real number. A floating point number with infinite precision would be a real number.

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
...but it''s anything but a real number. A floating point number with infinite precision would be a real number.


Captain Pedantic strikes again!

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
quote:
Original post by Anonymous Poster
quote:
Original post by Magmai Kai Holmlor
Make a typedef, then you can try both easily.

/*
typedef float float_t;
/*/
typedef double float_t;
//*/

My suggestion is on the name "Real" as in a real number.
/*
typedef float Real;
/*/
typedef double Real;
//*/
YMMV on whether it should be PascalCase or not.


...but it''s anything but a real number. A floating point number with infinite precision would be a real number.


just like C/C++ ''int'' is anything but an integer. An integer number with infinite possible range would be a integer number.

Oh, and to counter-knit pick: the value contained by the proposed ''Real'' typedef would only hold ''Real'' values . (actually, that''s not true...but I''ll pretend it is).

(personally I''d vote ''real'' since it''s in style with ''int'', unless you''re using typedefs for ints too ).

Share this post


Link to post
Share on other sites
1, 13, 25, 2, 3, 6, 17, 22, 29, 32, 5, 8, 10, 14, 19, 20, 21, 23, 24, 7, 9, 11, and 18 are Real numbers.

In any case, OpenGL uses 32 bits for glFloat (at the moment) and if you want to conform to whatever lib you may be using to render, use their typedef or #defines.

gl hf

Share this post


Link to post
Share on other sites
quote:
Original post by Mihail121
doubles are faster than floats cause floats are converted into doubles.the only limitation is the size


Interesting assertion.

You may find the following quote from Intel''s own optimisation guide interesting:
quote:
Do not use too much precision when it is not necessary. Single precision (32-bits) is faster on some operations and consumes only half the memory space as double precision (64-bits) or double extended (80-bits).


Do remember to take the memory and cache fetch bandwidth into account too...

You may also notice that Direct3D deliberately uses floats internally rather than doubles (although of course MS probably know nothing about software development or optimisation so made that decision based on a false assumption)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
But, to get the FPU performance gain from using only float precision, you have to set the fpu mode. Details are in some article (too lazy to find it) at www.stereopsis.com

Share this post


Link to post
Share on other sites
There are two *potential* gains from using floats rather than doubles.

1) Less memory bandwidth. This doesn''t require you to do anything special. The 32bit representation simply gets expanded to its 80bit (or 128bit) internal form on load.

2) Using _controlfp(), _control87() to change **internal** precision - as long as nothing changes the setting back to doubles you just do that once at initialisation time.


Of course it all depends on your particular application, what the profiler is telling you etc.

Share this post


Link to post
Share on other sites
quote:
Original post by Doc
quote:
Original post by Magmai Kai Holmlor
...but it's anything but a real number. A floating point number with infinite precision would be a real number.


Captain Pedantic strikes again!


It's not pedantic, the fact that it's a limited precision floating-point number causes no end of grief when you try to write robust floating-point code. Demoralization, truncation, and round-off errors crop up all over the place, xsp with 32bit floats.

if(1.1f==2.1f-1.0f) won't [always] resolve true (I'd wager it'll rarely resolve true, if ever).

Poppets pretending it's a real expect it too, and calling it a real reinforces the illusion.


[edited by - Magmai Kai Holmlor on August 19, 2003 1:31:25 AM]

[edited by - Magmai Kai Holmlor on August 19, 2003 1:35:37 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by MaulingMonkey
just like C/C++ 'int' is anything but an integer. An integer number with infinite possible range would be a integer number.


Very true, and it causes it's own set of problems (heh, there's a 'program test' thread that has one)

quote:
Original post by MaulingMonkey
(personally I'd vote 'real' since it's in style with 'int', unless you're using typedefs for ints too ).


MKH::u8
MKH::u16
//...
MKH::i16
MKH::i32
MKH::i64
//...
MKH::f32
etc...

[edited by - Magmai Kai Holmlor on August 19, 2003 1:33:22 AM]

Share this post


Link to post
Share on other sites