• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Accurate decimal numbers?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accurate decimal numbers?


  • Subject: Re: Accurate decimal numbers?
  • From: John Stiles <email@hidden>
  • Date: Sat, 1 Jan 2005 15:53:09 -0800

Why are you dodging around NaN/inf's like they're made of Kryptonite? They're easy to check for. I'm just trying to show you that they do exist and you have to be ready for them.

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;
}

Once you've got your double or float, if you want to convert it to integer, just figure out the minimum units you want--say, 1/1000--and round to it. i.e.:

int i = rint( (x * 1000) + 0.5 );

You're overanalyzing and making things more complicated than they have to be.


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: 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: This email sent to email@hidden
  • Follow-Ups:
    • Re: Accurate decimal numbers?
      • From: mark <email@hidden>
References: 
 >Re: Accurate decimal numbers? (From: mark <email@hidden>)

  • Prev by Date: Re: Accurate decimal numbers?
  • Next by Date: Re: Accurate decimal numbers?
  • Previous by thread: Re: Accurate decimal numbers?
  • Next by thread: Re: Accurate decimal numbers?
  • Index(es):
    • Date
    • Thread