• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
DvDmanDT

Floats and determinism

10 posts in this topic

Hi,

 

I'm experimenting some with determinism. I know the basics of floats work and why they are unable to represent certain numbers as well as why you should never use equality to compare floats due to rounding errors etc in computation. What I'm not as certain about is how deterministic they are. If I do the same computation using floats/doubles on two different machines or even different architectures, will the output be the same?

0

Share this post


Link to post
Share on other sites

There's no guarantee that floats will have the same physical representation in a different architecture. The point is not to reply on its representation. If you need to be absolutely sure of an operation's result, then you can use an arbitrary precision library, but beware that there will be numbers it cannot represent. Just like all numeric systems. For instance, how do you represent (10 / 3) in decimal?

If you do the same computations on different machines, they'll be in the ballpark of each other; 2 * 2 won't suddenly become 5. However, if you're relying on it being the same number down to the last bit, then you're doing something wrong.

0

Share this post


Link to post
Share on other sites

The problem is that I need to do hashing on the binary data, so I'm indeed relying on the results being equal down to the last bit. I don't technically need to use floats, it's just that it would make my life easier in some cases.

0

Share this post


Link to post
Share on other sites
IEEE floating point is well defined and deterministic. On a conforming implementation, The result of any specific computation has a /specific result.

High level languages often do not require IEEE floating point, however the IEEE standard for floating point numbers does dominate actual computer architectures.

ARM with NEON, does not comply with IEEE. It uses a faster floating point implementation, with slightly different rules. As far as I know, this is the only modern outlier.

The inaccuracy of floating point numbers comes from the fact that they are effectively encoding all numbers as 1.x * 2^y, and that allows imprecision in the representation of the result of any particular operation. And you rarely need to do only one operation on a number. So the error of doing a series of operation quickly grows to make using the equality operator useless for floating point numbers.

In fact the determinism of floating point calculations has a consequence: it means compilers cannot reorder floating point calculations as freely as they can integer calculations. This is doubly true when floating point variables can be non-finite.
0

Share this post


Link to post
Share on other sites

[edit] I missed the ".NET" tag on this originally. My summary changes thusly: don't fucking bother. You can't even reliably control the machine control-word register in .NET code, which means you can't guarantee consistency of any significant nature. I'll leave the rest of this here for posterity; read it under the assumption that I'm talking about real systems languages like C, C++, Rust, etc.[/edit]

 
.Net requires strict width restrictions on floating point mathematical operations. Or, in other words, it will always force a truncation of a floating point operation to the minimum bitsize required to store it in the destination. For 64bit .net code, this really has no meaning, as its all done using SIMD registers. For 32 bit .net though, where you're dealing with the FPU it means that most operations are accompanied by a store and load. Edited by Washu
1

Share this post


Link to post
Share on other sites

Yes, but .Net doesn't guarantee (AFAIK) how certain floating-point code is lowered to machine instructions, especially when JITting, so you can still get drift because of order-of-operations when those truncations occur.

0

Share this post


Link to post
Share on other sites

We do binary hashing of assets (which includes lots and lots of floating point data) as part of our content build system, and we usually don't have any problems since it's restricted to x64 builds. The only problems we've had has been with debug builds emitting different code due to optimizations, which resulted in different results compared to release version in some rare cases. We ended up just coming up with a simple test framework to test for differences, and then fixed the problem cases manually using SSE intrinsics. Of course this is all in C++, so I don't know how you could fix such problems in a .NET language.

 

The real problem we had was with compiler-inserted padding in structures. That happened all over the place, and fixing it was a nightmare.

1

Share this post


Link to post
Share on other sites

FWIW, I achieved deterministic floating point computations in .net between x86 PCs and the Xbox 360 (Power PC architecture). There were a few gotchas that were difficult to diagnose. But in the end, I was able to record player inputs for my physics-based game on the Xbox, and "replay" them perfectly on the PC. So it's possible.

0

Share this post


Link to post
Share on other sites

There is a fairly lengthy and detailed article on the subject at http://randomascii.wordpress.com/2013/07/16/floating-point-determinism/ which is C/C++ based.

 

Here's a wonderful demonstration from there of one of the potential pitfalls:

// CPUID is for wimps:
__m128 input = { -997.0f };
input = _mm_rcp_ps(input);
int platform = (input.m128_u32[0] >> 16) & 0xf;
switch (platform)
{
   case 0×0: printf("Intel.\n"); break;
   case 0×7: printf("AMD Bulldozer.\n"); break;
   case 0×8: printf("AMD K8, Bobcat, Jaguar.\n"); break;
   default: printf("Dunno\n"); break;
}

I suspect .NET would be even more tricky to make deterministic. For example the jitter might decide to generate different instructions based on what instructions your CPU supports, which may give slightly different results.

1

Share this post


Link to post
Share on other sites
My first thought would be fixed-point arithmetic. With 64-bit integers, a 32.32 format is good for the vast majority of situations. If you need special functions (trigonometric, exponential...) you can write your own implementation, perhaps using methods like CORDIC.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0