Jump to content
  • Advertisement

Archived

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

Puzzler183

Values from 0.0 to 1.0

This topic is 5456 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

I have a need for values in the range 0.0 to 1.0 and I''m stuck on one thing: should I use a float or unsigned char/short? Either would probably work for my purpose (fuzzy logic)... So does anyone have anything ideas?

Share this post


Link to post
Share on other sites
Advertisement
easiest to start with float and move to a fixed point representation later on when(if) it becomes neccessary.

Share this post


Link to post
Share on other sites
The most logical would be to use float .

However floats are incapable of storing certain values absolutely. Also the soundness of your numerical methods becomes important to prevent accumulation/rounding error being a problem (depending on what you''re doing).

Fixed point numbers are of course also limited in what they can store - however they may make better use of the binary layout for your purposes (e.g. 1.31 fixed point gives you more fractional bits than 1.8.23 IEEE float).


I''d go for float unless you have a reason not to.

Share this post


Link to post
Share on other sites
.......


Uhm, there is no way I''m making it a double. I don''t need very high precision with this and I''ve talked to some other people about it. I think I''ll end up using an unsigned short to speed up the math (it will need to be fast because it''s in a game). That is unless I find some easy cool ways to do what I need with floats.

Share this post


Link to post
Share on other sites
a lot of floating point math these days is actually faster than integer math. The slow down only occurs when you''re converting between the two.

Share this post


Link to post
Share on other sites
quote:
Original post by daerid
a lot of floating point math these days is actually faster than integer math. The slow down only occurs when you''re converting between the two.


O_____________________________________o

/me benchmarks it, brb

Share this post


Link to post
Share on other sites
Using the following source code:



#include <stdlib.h>
#include <iostream.h>
#include <windows.h>

int main()
{
unsigned int t1, t2, dt, i;

float temp1 = 0;
unsigned int temp2 = 0;

t1 = GetTickCount();
for (i = 0; i < 4200000000; i++)
{
temp1 += .000000001;
}

t2 = GetTickCount();
dt = t2 - t1;

cout << "Floating Point-\n Total Time: " << dt << "ms\n";
cout << " Average Time: " << dt / 4200000000.0 << "ms\n\n";

t1 = GetTickCount();
for (i = 0; i < 4200000000; i++)
{
temp2 += 1;
}

t2 = GetTickCount();
dt = t2 - t1;

cout << "Integer-\n Total Time: " << dt << "ms\n";
cout << " Average Time: " << dt / 4200000000.0 << "ms\n\n";

system("pause");
}


I got the following results:


Floating Point-
Total Time: 5625ms
Average Time: 1.33929e-06ms

Integer-
Total Time: 5547ms
Average Time: 1.32071e-06ms

----------------------------------------------------------

Floating Point-
Total Time: 5656ms
Average Time: 1.34667e-06ms

Integer-
Total Time: 5609ms
Average Time: 1.33548e-06ms

----------------------------------------------------------

Floating Point-
Total Time: 6000ms
Average Time: 1.42857e-06ms

Integer-
Total Time: 5609ms
Average Time: 1.33548e-06ms



So as three tests show, with 4.2 billion additions per test, integer addition is faster than floating point addition. I''ll assume this of other operations because my experience with assembly language says I should.

Share this post


Link to post
Share on other sites
Notice, though, that the difference isn''t very significant. And if you were to take advantage of SIMD or 3DNow! features...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!