Trip
Trip

Reputation: 27114

Why don't these numbers add in Xcode Debugger?

I'm using the Xcode debugger, and these numbers don't seem to add. I was curious as to why :

(lldb) p height
(CGFloat) $R0 = 2.1815627849240522E-314 
(lldb) p frame.size
(CGSize) $R1 = (width = 375, height = 1000)
(lldb) p frame.size.height
(CGFloat) $R2 = 1000
(lldb) p height + frame.size.height
(CGFloat) $R3 = 1000

I have 3 questions about this..

  1. What is 2.1815627849240522E-314 ? What kind of number is that?
  2. Is it possible to instantiate temporary variables in debugger like you would in Ruby console, Chrome console etc.? For example, let temp_x = 4 ?
  3. Why does my $R3 return 1000 and not.. something bigger?

Upvotes: 2

Views: 160

Answers (1)

Sulthan
Sulthan

Reputation: 130172

2.1815627849240522E-314 is essentially zero. 1000 + 0 = 1000.

Floating point numbers have two parts - mantissa and exponent (position of the decimal point). e-341 means "move the decimal point 341 places to the right"

Adding 1000 to that very very small number creates a number that would be equal

1000.000...(> 300 zeros here)...2181567849240522

Even double mantissa can't represent numbers like this (double can represent up to 17 decimal digits) so it takes only the more important numbers in the beginning.

I encourage you to read through What Every Programmer Should Know About Floating-Point Arithmetic

Upvotes: 4

Related Questions