Jump to content
  • Advertisement
Sign in to follow this  
jamesleighe

Floating Point Accuracy

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

Ok, so in some places in my code I have things like this:

(where SCUBE_SIZE = 1.5f and SECTOR_BSIZE = 128)

for (uint32 y = 0; y < uint32(depth_ / SCUBE_SIZE) / SECTOR_BSIZE; ++y)


These SHOULD always come out as whole numbers... But am I setting myself up for trouble?? (It seems like I am)

==EDIT==
I guess I'm asking, if I multiply A (a float, representing a whole number) by B (a float) and then devide A by B, am I going to get my original number back always?
(This is assuming B has few significant digits, like 3 or less)

==EDIT2==
It HAS worked so far... But I sure don't like the looks of it.

Share this post


Link to post
Share on other sites
Advertisement
This is a remarkably unreliable thing to do.

Instead, if you can afford the risk of overflow, multiply by 3 and divide by 2. Then all your arithmetic is integers and safely accurate.

Share this post


Link to post
Share on other sites
Or rather the other way around in this case :wink:

All the code really needs to do is to divide by 192, which you could write like this:const uint32 Divisor = (SECTOR_BSIZE + SECTOR_BSIZE / 2);

for (uint32 y = 0; y < depth_ / Divisor; ++y):
Note that I very much don't care for the trailing underscores :o

Share this post


Link to post
Share on other sites
Yeah, I'm not really surprised this is a bad idea.

As for the trailing underscores, I picked that up a while back as a succinct way to differinciate private variables while still preserving readability.

e.g.

class VertexShader: virtual public BaseShader, virtual public IVertexShader
{
public:
VertexShader (const char* name, IDirect3DVertexShader9* dxShader, VertexFormat format);
~VertexShader ();

IDirect3DVertexShader9* GetDXShader ();
VertexFormat GetFormat ();
IDirect3DVertexDeclaration9* GetDeclaration ();

private:
IDirect3DVertexShader9* dxShader_;
VertexFormat format_;
IDirect3DVertexDeclaration9* declaration_;
};


I like it better than m_variableName but if there is something fundamentally bad here I will rethink things.

EDIT: I wish whitespace wasn't so broken in 'code'

Share this post


Link to post
Share on other sites
As for the trailing underscores, I picked that up a while back as a succinct way to differinciate private variables while still preserving readability.
I like it better than m_variableName but if there is something fundamentally bad here I will rethink things.

There's no problem with using trailing underscores.

Share this post


Link to post
Share on other sites
I guess I'm asking, if I multiply A (a float, representing a whole number) by B (a float) and then devide A by B, am I going to get my original number back always?[/quote]

As long as A, B, and A*B are all representable exactly, then (A * B) / A = B

I'm not sure why you're using floats when you're intending to store integers inside them. FYI casting from a float to an integer is a relatively expensive operation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!