| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
bool NumberIsOK( float x )
{
return !isnan(x) && (x > -INFINITY) && (x < INFINITY);
}...
x = NSScanner or atof or whatever;
if( !NumberIsOK( x ) )
{
NSAlert( @"Invalid number entered" ... ); // <-- do something nicer
return false;
}int i = rint( (x * 1000) + 0.5 );
On Jan 1, 2005, at 3:13 PM, mark wrote:
Unless I'm missing something, there's no easy way to avoid starting out with
doubles or floats--the user sends me an input string with something like
"2.5". I need to covert that into a decimal number before I can convert it
to a integer. What scanDouble does for me is to handle the decimal digits
and decimal separators for me. I could, in theory, write something that
scans a decimal myself, bypassing doubles, but I'd have to handle all of the
localization the Cocoa provides for me (i.e., decimal separators other than
'.' and ',' and digits other than '0'-'9'). Once I get past that conversion
of string to double to int unit, I can keep a base unit like I have
implemented and you and Ricky suggested. The problem is that initial
scanning and converting of a decimal string…
As far as I can tell, there's no NSString or NSScanner function that can
scan an input string, returning a decimal broken up into integers (and
expondents, I guess, because 1.001 needs to have the .001 called out as a
1x10^-3). I could use NSDecimal for this, but I think a double works OK. I
could, as an extra case (given I know my user's accuracy), limit the # of
characters that they could enter, which would put a fence around the user
entering 100's of 0's to cause the overflow/NaN problems…
mark
From: Shawn Erickson <email@hidden> Date: Sat, 01 Jan 2005 12:44:54 -0800 To: mark <email@hidden> Cc: Dev Cocoa <email@hidden> Subject: Re: Accurate decimal numbers?
On Jan 1, 2005, at 12:17 PM, mark wrote:
I can't duplicate this result using NSScanner's scanDouble. Maybe it catches this case for me?
What you really want to do is avoid the use of floats / doubles all together in this case. As Rick outlined instead use integers using a defined unit.
In other words pick some unit and store your values in that form. Then convert from the form to others as needed. For example say you decide that your normalized form is millimeters (mm).
User inputs 10 cm store it as 100. User inputs 1 m store it as 1000. User inputs 1.555 m store it as 1555. etc.
In other words know the users input units and parse input as needed avoiding the use of floating point math in doing so.
-Shawn
_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/email@hidden
This email sent to email@hidden
_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/email@hidden
| References: | |
| >Re: Accurate decimal numbers? (From: mark <email@hidden>) |
| Home | Archives | FAQ | Terms/Conditions | Contact | RSS | Lists | About |
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE
Contact Apple | Terms of Use | Privacy Policy
Copyright © 2007 Apple Inc. All rights reserved.