bugGNUstep - Bugs: bug #43722, Parsing of strings to double can...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #43722: Parsing of strings to double can produce erroneous results

Submitted by:  Sam Thompson <sthompso>
Submitted on:  Fri 28 Nov 2014 05:19:30 PM UTC  
 
Category: Base/FoundationSeverity: 3 - Normal
Item Group: BugStatus: Fixed
Privacy: PublicAssigned to: None
Open/Closed: In Test

Tue 02 Dec 2014 10:43:41 AM UTC, comment #2:

Fantastic, thanks.

After applying the change, the provided test code now generates the following output, which looks correct:

String 50.6468746467461646 -> doubleValue = 50.646874646746163
String 50.64687464674616461 -> doubleValue = 50.646874646746163
String 0.646874646746164616898211 -> doubleValue = 0.64687464674616468
String 502.6468746467461646 -> doubleValue = 502.6468746467462
String 502.646874646746164 -> doubleValue = 502.6468746467462

Sam Thompson <sthompso>
Sat 29 Nov 2014 11:04:52 AM UTC, comment #1:

Thanks ... I have made a change which should have fixed that in trunk now ... please give it a try and let me know how it looks.

Richard Frith-Macdonald <CaS>
Project Member
Fri 28 Nov 2014 05:19:30 PM UTC, original submission:

When parsing strings representing decimals to doubles, for example via the use of [NSString doubleValue], the results can be completely incorrect if there are a large number of significant digits and there is a non-zero integer part.

Take the following examples, which are generated as stdout from the attached code in DoubleValueTest.m:
String 50.6468746467461646 -> doubleValue = 50.646874646746163
String 50.64687464674616461 -> doubleValue = 7.6972016867461646 ***
String 0.646874646746164616898211 -> doubleValue = 0.64687464674616457
String 502.6468746467461646 -> doubleValue = 73.150145046746161 ***
String 502.646874646746164 -> doubleValue = 502.6468746467462

With the example "0.646874646746164616898211", there are high number of significant digits, but the double value resulting from the parse at least resembles the string representation, I suspect there's truncation of the decimals. However, with the example "50.64687464674616461", it has less significant digits but the resulting double from the parse does not even closely represent the string representation.

GNUStep version: 1.24.6
Platform: Windows
GCC: 4.7.2

Sam Thompson <sthompso>

 

Attached Files
file #32567:  DoubleValueTest.m added by sthompso (1KiB - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by CaS (Posted a comment)
  • -unavailable- added by sthompso (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 3 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Sat 29 Nov 2014 11:04:52 AM UTCCaSStatusNone=>Fixed
      Open/ClosedOpen=>In Test
    Fri 28 Nov 2014 05:19:30 PM UTCsthompsoAttached File-=>Added DoubleValueTest.m, #32567

    Back to the top


    Powered by Savane 3.1-cleanup1