1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 #import <Foundation/NSNumber_double.h>
10 #import <Foundation/NSStringFormatter.h>
12 #import <Foundation/NSNumber_double_const.h>
14 NSNumber *NSNumber_doubleSpecial(double value)
16 switch (fpclassify(value))
19 return signbit(value)?kNSNumberNegativeInfinity:kNSNumberPositiveInfinity;
23 return signbit(value)?kNSNumberNegativeZero:kNSNumberPositiveZero;
25 if(0) {} // Without profiling, I'm assuming no one value is more likely than every other value put together, and the compiler will optimize for the first if() branch.
26 else if (value == 1.0) return kNSNumberPositiveOne;
27 else if (value == -1.0) return kNSNumberNegativeOne;
32 @implementation NSNumber_double
34 NSNumber *NSNumber_doubleNew(NSZone *zone,double value) {
35 NSNumber *result=NSNumber_doubleSpecial(value);
38 NSNumber_double *self=NSAllocateObject([NSNumber_double class],0,zone);
40 self->_type=kCFNumberDoubleType;
48 -(void)getValue:(void *)value {
49 *((double *)value)=_value;
52 -(const char *)objCType {
53 return @encode(double);
60 -(unsigned char)unsignedCharValue {
68 -(unsigned short)unsignedShortValue {
76 -(unsigned int)unsignedIntValue {
77 return (unsigned int)_value;
84 -(unsigned long)unsignedLongValue {
88 -(long long)longLongValue {
92 -(unsigned long long)unsignedLongLongValue {
100 -(double)doubleValue {
105 return _value?YES:NO;
108 -(NSInteger)integerValue {
109 return (NSInteger)_value;
112 -(NSUInteger)unsignedIntegerValue {
113 return (NSUInteger)_value;
116 -(NSString *)descriptionWithLocale:(NSDictionary *)locale {
117 return NSStringWithFormatAndLocale(@"%0.15g",locale,_value);
123 @implementation NSNumber_double_const
125 + (id) allocWithZone:(NSZone *)zone {
126 [NSException raise:NSInternalInconsistencyException format:@"Private class NSNumber_double_const is not intended to be alloced."];
132 [super dealloc]; // Silence compiler warning
140 - (oneway void)release
142 // will never be released
150 -(NSUInteger)retainCount {
151 /* "For objects that never get released (that is, their release method
152 does nothing), this method should return UINT_MAX, as defined in
153 <limits.h>." -- NSObject Protocol Reference
160 // This code needs to be here after the @implementation. If it's not clang is generating the NSNumber_double_const
161 // class reference with a "97" after it. Unknown reasons.
163 NS_DECLARE_CLASS_SYMBOL(NSNumber_double_const);
168 const struct objc_class * const isa;
171 } NSNumber_double_Def;
174 static const NSNumber_double_Def kPositiveInfinityDef =
176 &NS_CLASS_SYMBOL(NSNumber_double_const),
182 static const NSNumber_double_Def kNegativeInfinityDef =
184 &NS_CLASS_SYMBOL(NSNumber_double_const),
190 static const NSNumber_double_Def kNaNDef =
192 &NS_CLASS_SYMBOL(NSNumber_double_const),
198 static const NSNumber_double_Def kPositiveZeroDef =
200 &NS_CLASS_SYMBOL(NSNumber_double_const),
206 static const NSNumber_double_Def kNegativeZeroDef =
208 &NS_CLASS_SYMBOL(NSNumber_double_const),
214 static const NSNumber_double_Def kPositiveOneDef =
216 &NS_CLASS_SYMBOL(NSNumber_double_const),
222 static const NSNumber_double_Def kNegativeOneDef =
224 &NS_CLASS_SYMBOL(NSNumber_double_const),
229 const CFNumberRef kCFNumberPositiveInfinity = (CFNumberRef)&kPositiveInfinityDef;
230 const CFNumberRef kCFNumberNegativeInfinity = (CFNumberRef)&kNegativeInfinityDef;
231 const CFNumberRef kCFNumberNaN = (CFNumberRef)&kNaNDef;
233 NS_CONSTOBJ_DEF NSNumber * const kNSNumberPositiveZero = (NSNumber *)&kPositiveZeroDef;
234 NS_CONSTOBJ_DEF NSNumber * const kNSNumberNegativeZero = (NSNumber *)&kNegativeZeroDef;
235 NS_CONSTOBJ_DEF NSNumber * const kNSNumberPositiveOne = (NSNumber *)&kPositiveOneDef;
236 NS_CONSTOBJ_DEF NSNumber * const kNSNumberNegativeOne = (NSNumber *)&kNegativeOneDef;