Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Converting float to Fix64 consistent across CPU architectures? #23

Closed
benlabbeus opened this issue Apr 5, 2021 · 5 comments
Closed

Comments

@benlabbeus
Copy link

Probably a dumb question but -

Would converting from a float to a Fix64 be consistent across CPU architectures?

float z = 0.123456789123f;
Fix64 x = (Fix64)z; // Always the same
@asik
Copy link
Owner

asik commented Apr 5, 2021

This is the code that does it:

        public static explicit operator Fix64(float value)
        {
            return new Fix64((long)(value * ONE));
        }

Two things happen:

  • the float is multiplied by an integer constant (this is floating point multiplication)
  • then it is cast to a 64-bit integer

The multiplication must be deterministic because the constant is a power of two (so it amounts to incrementing the exponent on the result).
The float->integer cast behavior is specified as per the CLI spec.

So, I don't see how that wouldn't be deterministic.

@asik
Copy link
Owner

asik commented Apr 5, 2021

Well, except maybe for #9

It's been so long I don't really recall what the exact issue was. Just because that behavior is unspecified doesn't mean it actually varies across architectures.

@benlabbeus
Copy link
Author

Totally makes sense. Thanks for the prompt response!

@jpgordon00
Copy link

I needed this information too. I'm going to ship my product with FixedMath assuming that casting to and from floats are deterministic.

@asik
Copy link
Owner

asik commented Apr 22, 2021

There's an open issue about this, but I'm not willing to put in the time to research how to best address it.
I'll just archive this repo so people understand that this is not actively maintained anymore. Feel free to fork and improve it.

@asik asik closed this as completed Apr 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants