0

I have several questions regarding floating points and iOS devices:

Are floating points deterministic on one given iOS device?
Are floating points deterministic across all iOS devices?

If not, is there a way to make them deterministic? What I am thinking of is: change of compiler settings, usage of reduced set of math operations, etc.

If there isn't any way to do so, what would be the best alternative?
Could I use fixed points instead? Would it mean using NSDecimalNumber?

Cheers.

  • Can you clarify what you mean by deterministic? What problem are you having? Note that compiler settings will affect IEEE floating point compliance so you will need to check the Xcode settings to ensure the correct flags are set when compiling. – Robotic Cat Oct 18 '14 at 15:16
  • I want my code to be deterministic. I want an operation returning a float to return the exact same value on different os and devices. I don't care much about the precision of the result, it just needs to be stable. http://randomascii.wordpress.com/2013/07/16/floating-point-determinism/ – Poulet numéro 1687547878234 Oct 18 '14 at 15:31
  • 1
    Given the exact same inputs (including system settings) you will get the exact same outputs, for any IEEE float operation. For iOS you'd only risk a difference between versions of iOS that have different size values for the integer types, and this could only happen if overflow was occurring (ie, an error) with the smaller values. – Hot Licks Oct 18 '14 at 16:39
  • @Hot Licks What do you mean by "system settings"? Also, "for any IEEE float operation" meaning an operation that follows the IEEE standard? – Poulet numéro 1687547878234 Oct 18 '14 at 16:45
  • There is the possibility of settings which change the rounding rules used by the floating-point processing unit. I have never heard of these being manipulated on an iPhone, though. And yes, by "IEEE float operation" I mean the IEEE 754 standard. – Hot Licks Oct 18 '14 at 19:08

1 Answers1

1

The results from the same data size should be identical. The IEEE standard for floating point dictates the results for different float operations on different sizes of floating point numbers.

Where you might get into trouble is if the definition of different data types varies across architectures.

The size of "float" is not formally defined in ANSI C. It can be 4 bytes on some platforms and 8 on others. (NSInteger, for example, is 32 bits on a 32 bit device and 64 bits on a 64 bit device. I know it's an integer type, but its an example of a type that changes size based on the platform.)

I don't know if Apple changed the size of any of the data types between their 32 bit and 64 bit platforms. Perhaps somebody who knows conclusively can chime in here.

The newest iOS devices are 64 bit. I would suggest checking the size of float/CGFloat (using sizeof().) If you don't have access to one of the new 64 bit devices you should be able to test it using the 64 bit simulator.

It's lik

Duncan C
  • 128,072
  • 22
  • 173
  • 272