octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: str2double behavior


From: Rik
Subject: Re: str2double behavior
Date: Mon, 31 Oct 2011 10:30:44 -0700

On 10/31/2011 10:00 AM, address@hidden wrote:
> Message: 2
> Date: Mon, 31 Oct 2011 07:19:18 -0400
> From: Ben Abbott <address@hidden>
> To: PhilipNienhuis <address@hidden>
> Cc: "address@hidden" <address@hidden>
> Subject: Re: Question about str2double
> Message-ID: <address@hidden>
> Content-Type: text/plain; CHARSET=US-ASCII
>
> On Oct 30, 2011, at 10:26 AM, PhilipNienhuis <address@hidden> wrote:
>
>> > octave:11> str2double ('1 2 3 4')
>> > ans =  1234
>> > 
>> > Is it intended behaviour that str2double ignores spaces?
> Matlab returns NaN. Octave's behavior appears to be buggy. 
>
> Ben
10/31/11

Yes, this is buggy.  Octave takes a shortcut at the beginning of parsing. 
See the code segment below from str2double.cc in src/DLD-FUNCTIONS:

--- CODE ---
  std::string::iterator se = str.end ();

  // Remove commas (thousand separators) and spaces.
  se = std::remove (str.begin (), se, ',');
  se = std::remove (str.begin (), se, ' ');
  str.erase (se, str.end ());
--- END CODE ---

This code collapses the unwanted characters instead of discarding them
during the parsing loop.  The result is that not only "1 2 3 4" but also
"1,2,3,4" will be converted to 1234.  I don't think this is what is desired
when the documentation says it accepts the comma as a thousands separator.

--Rik


reply via email to

[Prev in Thread] Current Thread [Next in Thread]