Skip to content

Instantly share code, notes, and snippets.

@dbgrandi
Created December 19, 2014 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbgrandi/9632d2776e20dcc2bf0a to your computer and use it in GitHub Desktop.
Save dbgrandi/9632d2776e20dcc2bf0a to your computer and use it in GitHub Desktop.
NSDecimalNumber for Euler
2014-12-19 12:03:03.985 Untitled[96114:507] The maximum NSDecimalNumber is 3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2014-12-19 12:03:03.987 Untitled[96114:507] Making an NSDecimalNumber 1 less than that (via an NSString) will be interpreted as 3402823669209384634633746074317682114540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
#import <Foundation/Foundation.h>
//
// I did this in CodeRunner (https://coderunnerapp.com), which is super handy for tests like this.
//
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *maxDecimalAsString = [NSString stringWithFormat:@"%@", [NSDecimalNumber maximumDecimalNumber]];
NSLog(@"The maximum NSDecimalNumber is %@", maxDecimalAsString);
//
// max is known to be 3402823669209384634633746074317682114550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
// if we subtract 1...
//
NSString *knownMaxMinusOne = @"3402823669209384634633746074317682114549999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999";
NSDecimalNumber *maxMinusOne = [NSDecimalNumber decimalNumberWithString:knownMaxMinusOne];
//
// We end up with a number that quite different. The limit here is the mantissa supports accuracy to 38 places.
//
NSLog(@"Making an NSDecimalNumber 1 less than that (via an NSString) will be interpreted as %@", maxMinusOne);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment