Discussion:
Comparing NSDecimalNumber to "notANumber"
Jean-Michel Cazaux
1998-08-25 15:26:46 UTC
Permalink
Hi folks,

I'm encountering a little problem with NSDecimalNumber (don't think this the best Apple-Next did...)

1) At the opposite of what is written is the doc, an arithmetic operation with a NSDecimalNumber whose value is notANumber produce an overflow exception.
Ex : [[NSDecimalNumber notANumber] decimalNumberBySubtracting:[NSDecimalNumber one]] produce "NSDecimalNumber overflow exception"

2) To prevent the exception, I'd like to check NSDecimalNumbers I'm applying an operation on...
But, the following test always returns YES, whatever is the value of 'anyNumber' :
[anyNumber isEqual:[NSDecimalNumber notANumber]]
I also tried compare and isEqualTo methods, and they gave the same results...

Any idea on how to workaround this...

Thanks in advance for any help...

PS : we are using OpenStep Enterprise 4.2 on NT
__________________________________________________________________________________________

Jean-Michel CAZAUX

FININDEV, Conseil en Finances pour les Collectivités Locales.
204 Rue Michel Teule - ZAC d'Alco
34080 Montpellier - FRANCE - EU.
Tél. +33 (0)4 67 63 66 25 - Fax +33 (0)4 67 63 35 45
e-mail ***@finindev.com
Edward K. Ream
1998-08-25 18:49:31 UTC
Permalink
Hi Jean-Michel,
Post by Jean-Michel Cazaux
2) To prevent the exception, I'd like to check NSDecimalNumbers I'm
applying an operation on...
But, the following test always returns YES, whatever is the value of
[anyNumber isEqual:[NSDecimalNumber notANumber]]
I also tried compare and isEqualTo methods, and they gave the same
results...
Any idea on how to workaround this...
I was poking around the example code the other day, and this very issue
came up.

The trick is to compare two different numbers, say 0 and 1, with the number
that might be a NaN. If both compare equal to the number, then the
"number" must be a Nan.

BTW, I doubt if this is an Apple problem. Probably inherent in the way
NaN's are to be treated.

Edward


--------------------------------------------------
Edward K. Ream Owner: Sherlock Software tm
***@mailbag.com (608)-231-0766 (voice)
http://www.mailbag.com/users/edream/front.html
Apple Computer, right down there with Mercedes Benz.
--------------------------------------------------
Mark Tarbell
1998-08-25 22:21:59 UTC
Permalink
Post by Edward K. Ream
BTW, I doubt if this is an Apple problem. Probably inherent in
the way NaN's are to be treated.
I think there is something wrong. The canonical way of determining the
presence of a NaN is to test it against itself; i.e., a NaN is equal to
nothing, including itself:

if (x != x) {
puts("x is a NaN\n");
}

But if you try this with NSDecimalNumber:

NSDecimalNumber *anyNumber = [NSDecimalNumber notANumber];
if (![anyNumber isEqual:anyNumber]) {
NSLog(@"anyNumber is notANumber");
}

...the isEqual check always matches, even if anyNumber is notANumber!

Maybe someone from Apple can elucidate NSDecimalNumber's notANumber logic?

Mark

--
Mark Tarbell (***@jpl.nasa.gov)
Applications Development Section
Information Systems Development and Operations Division
Engineering and Science Directorate
Jet Propulsion Laboratory
4800 Oak Grove Drive
MS 502-500
Pasadena, CA 91109

Opinions expressed herein do not reflect those of JPL.
Jean-Michel Cazaux
1998-08-26 13:47:54 UTC
Permalink
Thanks for the solution, the correct idea seems to compare it to both "one " and "zero", it works fine and as my fingers are lazy, I defined this small 'operator' :

#define isNotANumber(a) (([a isEqual:[NSDecimalNumber one]]) && ([a isEqual:[NSDecimalNumber zero]]))

Thanks again.

__________________________________________________________________________________________

Jean-Michel CAZAUX

FININDEV, Conseil en Finances pour les Collectivités Locales.
204 Rue Michel Teule - ZAC d'Alco
34080 Montpellier - FRANCE.
Tél. +33 (0)4 67 63 66 25 - Fax +33 (0)4 67 63 35 45
e-mail ***@finindev.com
s***@easynet.fr
1998-08-26 18:18:57 UTC
Permalink
Post by Jean-Michel Cazaux
Thanks for the solution, the correct idea seems to compare it to both
"one
Post by Jean-Michel Cazaux
" and "zero", it works fine and as my fingers are lazy, I defined this
#define isNotANumber(a) (([a isEqual:[NSDecimalNumber one]]) && ([a
isEqual:[NSDecimalNumber zero]]))
Thanks again.
In my app I did something a bit different

/*
#### This category supposes that [NSDecimalNumber notANumber]
returns always the same instance
This is in contradiction with the documentation, but there is
no - (BOOL)isNotANumber in NSNumber/NSDecimalNumber class, so
NeXT^H^H^H^HApple probably wanted it to be used this way...
This assumption is hidden in the FSIsNAN category
*/
@interface NSNumber(FSIsNAN)
// Return YES if number is NaN
- (BOOL)fsIsNan;
@end

@implementation NSNumber(FSIsNAN)

- (BOOL)fsIsNan
{ return self==[NSDecimalNumber notANumber];
}

@end

Even if you don't buy notANumber being a singleton, you'd better use a
category instead of a macro. (And if you don't want to use a category,
use at last a function instead of a macro.)

Cheers,

--fred

PS: Note that [a isEqual:[NSDecimalNumber one]] being YES with NaNs
is in total contradiction with NaN usage everywhere, so you can almost
'safely' guess that your code will stop working in next release.

Loading...