• 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: Strange behaviour of scanFloat
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange behaviour of scanFloat


  • Subject: Re: Strange behaviour of scanFloat
  • From: Ryan Britton <email@hidden>
  • Date: Sat, 5 Nov 2005 17:12:40 -0800

Floating point numbers in computers are imprecise. The value is stored as a mantissa and an exponent. Because of this, floats and doubles can only represent a finite number of values and guarantee exact precision only to a certain point.

http://www.petebecker.com/js200006.html


On Nov 5, 2005, at 4:51 PM, Marco Michieli wrote:

I'm using scanFloat to read float numbers from a text file, but I realized that for example the string "273.4501" is converted to the value "273.450104" and not "273.450100".

What's going on?

Here below the code and the correspondent log:



- (IBAction)test:(id)sender
{
	NSScanner *theScanner;
	NSString *scanString = @"237.4501";
	float floatValue;
    theScanner = [NSScanner scannerWithString:scanString];
	if ([theScanner scanFloat:&floatValue]) {
		NSLog(@"scanString:%@  scanned value:%f", scanString, floatValue);
	} else {
		NSLog(@"Scan Error");
	}
}


2005-11-06 01:33:39.556 Test[622] scanString:237.4501 scanned value:237.450104




Thanks for your help.


Best regards

Marco Michieli
_______________________________________________
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
References: 
 >Strange behaviour of scanFloat (From: Marco Michieli <email@hidden>)

  • Prev by Date: NSTask and unicode arguments
  • Next by Date: Re: Core Data Predicates with Relationships and Objects
  • Previous by thread: Strange behaviour of scanFloat
  • Next by thread: Core Data Predicates with Relationships and Objects
  • Index(es):
    • Date
    • Thread