#### Archived

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

# 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.

## 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 on other sites
easiest to start with float and move to a fixed point representation later on when(if) it becomes neccessary.

##### 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 on other sites
Make it a double instead of a float. No reason not to.

I''m quoting from Lakos here.

##### 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 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 on other sites
quote:
Original post by daerid
a lot of floating point math these days is actually faster than integer math

?!

##### 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 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-06msInteger-    Total Time: 5547ms    Average Time: 1.32071e-06ms----------------------------------------------------------Floating Point-   Total Time: 5656ms    Average Time: 1.34667e-06msInteger-    Total Time: 5609ms    Average Time: 1.33548e-06ms----------------------------------------------------------Floating Point-   Total Time: 6000ms    Average Time: 1.42857e-06msInteger-    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 on other sites
Notice, though, that the difference isn''t very significant. And if you were to take advantage of SIMD or 3DNow! features...

1. 1
2. 2
Rutin
21
3. 3
4. 4
frob
16
5. 5

• 9
• 12
• 9
• 33
• 13
• ### Forum Statistics

• Total Topics
632593
• Total Posts
3007276

×