-2

If I create an NSNumber with a double value of 0.085, it seems extra precision gets added to the number. However, this isn't the case with 0.85. I don't understand why this is the case, but I'm sure there's a really obvious answer. Does anyone know why?

For example:

NSNumber *n1 = [NSNumber numberWithDouble:0.85];
NSNumber *n2 = [NSNumber numberWithDouble:0.085];

NSLog(@"Number: %@", n1);     // Number: 0.85
NSLog(@"Number: %@", n2);     // Number: 0.08500000000000001
Airsource Ltd
  • 32,379
  • 13
  • 71
  • 75
Ian
  • 7,480
  • 2
  • 47
  • 51
  • 3
    http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Martin R Jul 10 '14 at 12:20
  • @HotLicks There is nothing wrong with this question and there is no requirement that someone has to know the entirety of a language before posting a question. – Airsource Ltd Jul 10 '14 at 12:20
  • @AirsourceLtd - Maybe not, but he should have checked one of the 20 or so dupes that the "Ask Question" page presented him when he typed in his question. – Hot Licks Jul 10 '14 at 12:34
  • @HotLicks - sure, but flag that then, not the OP's lack of knowledge. – Airsource Ltd Jul 10 '14 at 12:37
  • @AirsourceLtd - Someone who does not have a working knowledge of C should not be using Objective-C (at least not with any serious intent). – Hot Licks Jul 10 '14 at 12:40
  • Incidentally, the dupe I reported above is the first dupe that "Ask Question" presents when you type in the original title. – Hot Licks Jul 10 '14 at 12:41
  • @AirsourceLtd a down vote seems a tad harsh - surely marking as a duplicate is sufficient. Bear in mind that the title of my question has been changed by another user so I wouldn't have seen the same suggestions by SO when entering the question. In any case, you clearly know more about C so I'll take this on board and brush up in this area. Thanks. – Ian Jul 10 '14 at 13:11
  • @Ian - I think you're talking to HotLicks. I didn't down vote you. – Airsource Ltd Jul 10 '14 at 13:13
  • @AirsourceLtd, sorry, yes I meant to direct that to HotLicks. Thanks for your help. – Ian Jul 10 '14 at 13:19
  • I looked up the **original title** and typed that into Ask Question. The above dupe is the first entry in the list that was presented. A downvote is for "This question **does not show any research effort**; it is unclear or **not useful**." Given that your question appears several times a week here, having it appear one more time is clearly "not useful", even if you had done some research (which you clearly didn't). – Hot Licks Jul 10 '14 at 16:15
  • @HotLicks fair enough, you're entitled to your own opinion. – Ian Jul 10 '14 at 16:39

1 Answers1

3

This is related to double rather than NSNumber. double is a floating-point number value, and it does not provide precise decimal point numbers at specific level by design.

eonil
  • 83,476
  • 81
  • 317
  • 516