Created
December 19, 2014 17:06
-
-
Save dbgrandi/9632d2776e20dcc2bf0a to your computer and use it in GitHub Desktop.
NSDecimalNumber for Euler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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