Post

Replies

Boosts

Views

Activity

Reply to App hangs when setting constraint to a very specific value (a width divided by 2).
This answer comes from Programming in Objective-C by Stephen Kochan, pg. 52-53, if you don't want to read that, here is the text and explanation from the book, with some commentary of my own, and since most of UIKit is written in Obj-C I surmise these limitations apply here as well Program 4.1 uses the basic Objective-C data types. Program 4.1 #import <Foundation/Foundation.h> int main (int argc, char * argv[]) { @autoreleasepool { int integerVar = 100; float floatingVar = 331.79; double doubleVar = 8.44e+11; char charVar = 'W'; NSLog (@"integerVar = %i", integerVar); NSLog (@"floatingVar = %f", floatingVar); NSLog (@"doubleVar = %e", doubleVar); NSLog (@"doubleVar = %g", doubleVar); NSLog (@"charVar = %c", charVar); } return 0; } Program 4.1 Output integerVar = 100 floatingVar = 331.790009 doubleVar = 8.440000e+11 doubleVar = 8.44e+11 charVar = W In the second line of the program’s output, notice that the value of 331.79 , which is assigned to floatingVar , is actually displayed as 331.790009 . The reason for this inaccuracy is the particular way in which numbers are internally represented inside the computer. You have probably come across the same type of inaccuracy when dealing with numbers on your calculator. If you divide 1 by 3 on your calculator, you get the result .33333333, with perhaps some additional 3s tacked on at the end. The string of 3s is the calculator’s approximation to one third. Theoretically, there should be an infinite number of 3s. But the calculator can hold only so many digits, thus the inherent inaccuracy of the machine. The same type of inaccuracy applies here: Certain floating-point values cannot be exactly represented inside the computer’s memory. If by any chance you know PHP, this ties in with PHP_FLOAT_EPSILON. So in a nutshell, the computer enters into an infinite loop when you try to divide by 2 because it does not have an exact value for your constraint, which it is trying to calculate with its inherent precision limitation without success, and the low-level raw data type implementation demands that it have an exact value before program execution continues. Hope this helps
Feb ’22