0

Please see the pictures:

enter image description hereenter image description here

I'm amazing the result is different.

Paul R
  • 208,748
  • 37
  • 389
  • 560
huixing
  • 321
  • 2
  • 11
  • Floats only have about 7 decimal digits of precision. You are computing `price2` by dividing two floats. (What you do with a result doesn't change how that result is computed. That you're storing it in a `CGFloat` does not cause the division to keep that much precision.) – David Schwartz Nov 17 '17 at 09:20
  • 7
    Is it really too hard to show pure text as text instead of a picture? – Gerhardh Nov 17 '17 at 09:30
  • 3
    Your image of text [isn't very helpful](//meta.unix.stackexchange.com/q/4086). It can't be read aloud or copied into an editor, and it doesn't index very well, meaning that other users with the same problem are less likely to find the answer here. Please [edit] your post to incorporate the relevant text directly (preferably using copy+paste to avoid transcription errors). – Toby Speight Nov 17 '17 at 14:48
  • 2
    Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Toby Speight Nov 17 '17 at 14:49
  • Not a duplicate of that one. – Sulthan Nov 21 '17 at 20:20

2 Answers2

6

A CGFloat is actually a double on 64 bit platforms. (It was a float on old 32 bit platforms.)

So here you're dividing a double by a double:

CGFloat price    =    88888736      /     a;
        ^^^^^         ^^^^^^^^            ^
       double        int -> double      double

and here you're dividing a double by a float:

CGFloat price2    =   88888736     /   100.0f;
        ^^^^^^        ^^^^^^^^         ^^^^^^
        double       int -> double      float

Change 100.0f to either 100.0 or (CGFloat)100 and you should be fine.

LIVE DEMO

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • 2
    Maybe the non-obvious part is that the first division is a `double` division because it divides an integer literal and a `double` and the second is `float` division because it divides integer literal and `float` literal. – Sulthan Nov 17 '17 at 09:31
  • @Sulthan: thanks - I've added a more detailed explanation now. – Paul R Nov 17 '17 at 09:38
  • You have not answered a question since last November. What happened? It seems SO has slowed. – Z boson Jan 17 '18 at 14:08
  • 1
    @Zboson: heh - very observant - it's just a combination of factors really: been super-busy with work and domestic stuff + more SIMD experts around these days (Peter Cordes is quite prolific !) + majority of new questions seem to be either low quality or duplicates. Still here though... ;-) – Paul R Jan 17 '18 at 15:30
2

CGFloat is a double on your machine, therefore what you are doing is this:

double a = 100.00f
double price = 88888736 / a

float a2 = 100.00f // `float` type enforced by the trailing `f`
double price2 = 88888736 / a2

The difference is that in the first case the division is a double division while in the second case this is a float division where the result is then assigned to a double. Since float division has less precision, you get the result you are seeing.

Sulthan
  • 128,090
  • 22
  • 218
  • 270